Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/multi language integ tests #357

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.myorg</groupId>
<artifactId>unicorn-reservation-system</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<name>unicorn-reservation-system</name>

<properties>
<aws.java.sdk.version>2.26.7</aws.java.sdk.version>
<junit5.version>5.10.2</junit5.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>${aws.java.sdk.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudformation</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>apache-client</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.14</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.myorg.urs;

public class App {

public static void main(String... arguments) {
System.out.println("Hello World!");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package com.myorg.urs.integration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest;
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksResponse;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.PutItemResponse;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;
import software.amazon.awssdk.services.s3.model.PutObjectResponse;

public class TestFileProcessor {

private static S3Client s3Client;

private static String dynamoDbTableName;

private static String unicornInventoryBucket;

private static String idPostfix;

private static String testUnicorn;

private static String testLocation;

@BeforeAll
public static void runBeforeAll() {
s3Client = S3Client.builder()
.httpClient(ApacheHttpClient.create())
.build();

String awsSamStackName = System.getenv("AWS_SAM_STACK_NAME");

Assertions.assertNotNull(awsSamStackName);

DescribeStacksRequest describeStacksRequest = DescribeStacksRequest.builder().stackName(awsSamStackName)
.build();

CloudFormationClient cloudFormationClient = CloudFormationClient.builder().build();

DescribeStacksResponse describeStacksResponse = cloudFormationClient.describeStacks(describeStacksRequest);

Assertions.assertNotNull(describeStacksResponse);

describeStacksResponse.stacks().forEach(stack -> stack.outputs().forEach(output -> {
if (output.outputKey().equals("DynamoDBTableName")) {
dynamoDbTableName = output.outputValue();
} else if (output.outputKey().equals("UnicornInventoryBucket")) {
unicornInventoryBucket = output.outputValue();
}
}));

Assertions.assertNotNull(dynamoDbTableName);
Assertions.assertNotNull(unicornInventoryBucket);

idPostfix = "_%s".formatted(UUID.randomUUID().toString());
testUnicorn = "TEST_UNI%s".formatted(idPostfix);
testLocation = "TEST_LOC%s".formatted(idPostfix);
}

@AfterAll
public static void runAfterAll() {
// Delete testUnicorn
Map<String, AttributeValue> key = Map.of("PK", AttributeValue.builder().s(testUnicorn).build());

GetItemRequest getItemRequest = GetItemRequest.builder().tableName(dynamoDbTableName).key(key).build();

DynamoDbClient dynamoDbClient = DynamoDbClient.builder().build();

GetItemResponse getItemResponse = dynamoDbClient.getItem(getItemRequest);

if (getItemResponse.hasItem()) {
DeleteItemRequest deleteItemRequest = DeleteItemRequest.builder().tableName(dynamoDbTableName).key(key)
.build();

DeleteItemResponse deleteItemResponse = dynamoDbClient.deleteItem(deleteItemRequest);

Assertions.assertNotNull(deleteItemResponse);
}

// Update LOCATION#LIST without testLocation
key = Map.of("PK", AttributeValue.builder().s("LOCATION#LIST").build());

getItemRequest = GetItemRequest.builder().tableName(dynamoDbTableName).key(key).build();

dynamoDbClient = DynamoDbClient.builder().build();

getItemResponse = dynamoDbClient.getItem(getItemRequest);

Assertions.assertNotNull(getItemResponse);

List<AttributeValue> locations = getItemResponse.item().get("LOCATIONS").l();

List<AttributeValue> updatedLocations = new ArrayList<>();

for (AttributeValue location : locations) {
if (!location.s().equals(testLocation)) {
updatedLocations.add(location);
}
}

Map<String, AttributeValue> item = Map.of("PK", AttributeValue.builder().s("LOCATION#LIST").build(),
"LOCATIONS", AttributeValue.builder().l(updatedLocations).build());

PutItemRequest putItemRequest = PutItemRequest.builder().tableName(dynamoDbTableName).item(item).build();

PutItemResponse putItemResponse = dynamoDbClient.putItem(putItemRequest);

Assertions.assertNotNull(putItemResponse);
}

@Test
public void testFileProcessorHappyPath() {
Map<String, AttributeValue> key = Map.of("PK", AttributeValue.builder().s(testUnicorn).build());

GetItemRequest getItemRequest = GetItemRequest.builder().tableName(dynamoDbTableName).key(key).build();

DynamoDbClient dynamoDbClient = DynamoDbClient.builder().build();

GetItemResponse getItemResponse = dynamoDbClient.getItem(getItemRequest);

Assertions.assertFalse(getItemResponse.hasItem());

String testDataCsv = "\"Unicorn Name\",\"Unicorn Location\"\n";
testDataCsv += "\"%s\",\"%s\"\n".formatted(testUnicorn, testLocation);

String testDataKey = "INTEGRATION_TEST/TEST%s.test_file_processor_happy_path.csv".formatted(idPostfix);

PutObjectRequest putObjectRequest = PutObjectRequest.builder().bucket(unicornInventoryBucket)
.key(testDataKey).build();

PutObjectResponse putObjectResponse = s3Client.putObject(putObjectRequest, RequestBody.fromString(testDataCsv));
Assertions.assertNotNull(putObjectResponse);

int pollMaxSeconds = 30;
int pollCompleteSeconds = 0;

while (pollCompleteSeconds < pollMaxSeconds) {
getItemResponse = dynamoDbClient.getItem(getItemRequest);

if (getItemResponse.hasItem()) {
break;
}

try {
Thread.sleep(1000);
} catch (InterruptedException exception) {
Assertions.fail(exception);
}

pollCompleteSeconds++;
}

Assertions.assertTrue(getItemResponse.hasItem(), "Item not found after time allowed for processing.");
Assertions.assertTrue((pollCompleteSeconds <= 2),
"Unicorns should be fast! Took too long: %d sec".formatted(pollCompleteSeconds));
Assertions.assertEquals(getItemResponse.item().get("PK").s(), testUnicorn,
"Table PK is not set to the Unicorn Name: %s expected %s"
.formatted(getItemResponse.item().get("PK").s(), testUnicorn));
Assertions.assertEquals(getItemResponse.item().get("LOCATION").s(), testLocation,
"Table LOCATION is not set to the Unicorn Location: %s expected %s"
.formatted(getItemResponse.item().get("LOCATION").s(), testLocation));
}

}
Loading
Loading