Skip to content

implement tests with the testcontainers library #10

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

Merged
merged 9 commits into from
Apr 23, 2025
Merged
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
18 changes: 15 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Create Infra on LocalStack
name: Deploy with Terraform

on:
push:
Expand All @@ -15,8 +15,8 @@ on:
workflow_dispatch:

jobs:
infrastructure-check:
name: Setup infrastructure using Terraform
test:
name: Run Integration Tests
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down Expand Up @@ -81,6 +81,18 @@ jobs:
run: |
localstack logs

- name: Stop LocalStack
run: |
localstack stop

- name: Run Testcontainers tests
env:
AWS_ACCESS_KEY_ID: test
AWS_SECRET_ACCESS_KEY: test
AWS_REGION: us-east-1
run: |
mvn test -Dtest=dev.ancaghenade.shipmentlistdemo.integrationtests.ShipmentServiceIntegrationTest

- name: Send a Slack notification
if: failure() || github.event_name != 'pull_request'
uses: ravsamhq/notify-slack-action@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.DS_Store

### STS ###
.apt_generated
Expand Down
42 changes: 42 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven-checkstyle-plugin.version>3.2.0</maven-checkstyle-plugin.version>
<testcontainers.version>1.19.7</testcontainers.version>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand Down Expand Up @@ -69,16 +70,57 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20231013</version>
</dependency>

<!-- LocalStack TestContainers -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>localstack</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<!-- AWS SDK v2 Dependencies -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>iam</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>${testcontainers.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
class ShipmentListDemoApplicationTests {

@Test
void contextLoads() {
}
void contextLoads() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package dev.ancaghenade.shipmentlistdemo.integrationtests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LambdaIntegrationTest extends LocalStackSetupConfigurations {

@BeforeAll
public static void setup() throws IOException, InterruptedException, org.json.JSONException {
LocalStackSetupConfigurations.setupConfig();
localStack.followOutput(logConsumer);

createClients();

createS3Bucket();
createDynamoDBResources();
createIAMRole();
createLambdaResources();
createBucketNotificationConfiguration();
createSNS();
createSQS();
createSNSSubscription();

lambdaClient.close();
snsClient.close();
sqsClient.close();
iamClient.close();

}

@Test
@Order(1)
void testFileAddWatermarkInLambda() {

// prepare the file to upload
var imageData = new byte[0];
try {
imageData = Files.readAllBytes(Path.of("src/test/java/resources/cat.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
var resource = new ByteArrayResource(imageData) {
@Override
public String getFilename() {
return "cat.jpg";
}
};

var originalHash = applyHash(imageData);

var shipmentId = "3317ac4f-1f9b-4bab-a974-4aa9876d5547";
// build the URL with the id as a path variable
var postUrl = "/api/shipment/" + shipmentId + "/image/upload";
var getUrl = "/api/shipment/" + shipmentId + "/image/download";

// set the request headers
var headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// request body with the file resource and headers
MultiValueMap < String, Object > requestBody = new LinkedMultiValueMap < > ();
requestBody.add("file", resource);
HttpEntity < MultiValueMap < String, Object >> requestEntity = new HttpEntity < > (requestBody,
headers);

ResponseEntity < String > postResponse = restTemplate.exchange(BASE_URL + postUrl,
HttpMethod.POST, requestEntity, String.class);

assertEquals(HttpStatus.OK, postResponse.getStatusCode());

// give the Lambda time to start up and process the image
try {
Thread.sleep(15000);

} catch (InterruptedException e) {
e.printStackTrace();
}

ResponseEntity < byte[] > responseEntity = restTemplate.exchange(BASE_URL + getUrl,
HttpMethod.GET, null, byte[].class);

assertEquals(HttpStatus.OK, responseEntity.getStatusCode());

var watermarkHash = applyHash(responseEntity.getBody());

assertNotEquals(originalHash, watermarkHash);

}

@Test
@Order(2)
void testFileProcessedInLambdaHasMetadata() {
var getItemRequest = GetItemRequest.builder()
.tableName("shipment")
.key(Map.of(
"shipmentId",
AttributeValue.builder().s("3317ac4f-1f9b-4bab-a974-4aa9876d5547").build())).build();

var getItemResponse = dynamoDbClient.getItem(getItemRequest);

dynamoDbClient.getItem(getItemRequest);
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(BUCKET_NAME)
.key(getItemResponse.item().get("imageLink").s())
.build();
try {
// already processed objects have a metadata field added, not be processed again
var s3ObjectResponse = s3Client.getObject(getObjectRequest);
assertTrue(s3ObjectResponse.response().metadata().entrySet().stream().anyMatch(
entry -> entry.getKey().equals("exclude-lambda") && entry.getValue().equals("true")));
} catch (NoSuchKeyException noSuchKeyException) {
noSuchKeyException.printStackTrace();
}
dynamoDbClient.close();
s3Client.close();

}

private String applyHash(byte[] data) {
String hashValue = null;
try {
var digest = MessageDigest.getInstance("SHA-256");

// get the hash of the byte array
var hash = digest.digest(data);

// convert the hash bytes to a hexadecimal representation
var hexString = new StringBuilder();
for (byte b: hash) {
var hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
hashValue = hexString.toString();
System.out.println("Hash value: " + hashValue);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return hashValue;
}
}
Loading