Skip to content

Commit 1561632

Browse files
authored
feat: end-to-end tests for core modules and idempotency (#970)
* add e2e tests for logging * add e2e tests for metrics * add e2e tests for idempotency * add e2e tests for tracing
1 parent 482778b commit 1561632

File tree

32 files changed

+2180
-1
lines changed

32 files changed

+2180
-1
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
<module>powertools-validation</module>
3838
<module>powertools-test-suite</module>
3939
<module>powertools-cloudformation</module>
40-
<module>powertools-idempotency</module>
40+
<module>powertools-idempotency</module>
41+
<module>powertools-e2e-tests</module>
4142
<module>examples</module>
4243
</modules>
4344

powertools-e2e-tests/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## End-to-end tests
2+
This module is internal and meant to be used for end-to-end (E2E) testing of Lambda Powertools for Java.
3+
4+
__Prerequisites__:
5+
- An AWS account is needed as well as a local environment able to reach this account
6+
([credentials](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html)).
7+
- [Java 11+](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html)
8+
- [Docker](https://docs.docker.com/engine/install/)
9+
10+
To execute the E2E tests, use the following command: `export JAVA_VERSION=11 && mvn clean verify -Pe2e`
11+
12+
### Under the hood
13+
This module leverages the following components:
14+
- AWS CDK to define the infrastructure and synthesize a CloudFormation template and the assets (lambda function packages)
15+
- The AWS S3 SDK to push the assets on S3
16+
- The AWS CloudFormation SDK to deploy the template
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>software.amazon.lambda</groupId>
7+
<artifactId>e2e-test-handlers-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
11+
<artifactId>e2e-test-handler-idempotency</artifactId>
12+
<packaging>jar</packaging>
13+
<name>A Lambda function using powertools idempotency</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>software.amazon.lambda</groupId>
18+
<artifactId>powertools-idempotency</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
</dependency>
24+
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.codehaus.mojo</groupId>
31+
<artifactId>aspectj-maven-plugin</artifactId>
32+
<configuration>
33+
<source>${maven.compiler.source}</source>
34+
<target>${maven.compiler.target}</target>
35+
<complianceLevel>${maven.compiler.target}</complianceLevel>
36+
<aspectLibraries>
37+
<aspectLibrary>
38+
<groupId>software.amazon.lambda</groupId>
39+
<artifactId>powertools-idempotency</artifactId>
40+
</aspectLibrary>
41+
</aspectLibraries>
42+
</configuration>
43+
<executions>
44+
<execution>
45+
<goals>
46+
<goal>compile</goal>
47+
</goals>
48+
</execution>
49+
</executions>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-shade-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
6+
import software.amazon.awssdk.regions.Region;
7+
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
8+
import software.amazon.lambda.powertools.idempotency.Idempotency;
9+
import software.amazon.lambda.powertools.idempotency.IdempotencyConfig;
10+
import software.amazon.lambda.powertools.idempotency.Idempotent;
11+
import software.amazon.lambda.powertools.idempotency.persistence.DynamoDBPersistenceStore;
12+
13+
import java.time.Duration;
14+
import java.time.Instant;
15+
import java.time.format.DateTimeFormatter;
16+
import java.time.temporal.ChronoUnit;
17+
import java.util.TimeZone;
18+
19+
20+
public class Function implements RequestHandler<Input, String> {
21+
22+
public Function() {
23+
this(DynamoDbClient
24+
.builder()
25+
.httpClient(UrlConnectionHttpClient.builder().build())
26+
.region(Region.of(System.getenv("AWS_REGION")))
27+
.build());
28+
}
29+
30+
public Function(DynamoDbClient client) {
31+
Idempotency.config().withConfig(
32+
IdempotencyConfig.builder()
33+
.withExpiration(Duration.of(10, ChronoUnit.SECONDS))
34+
.build())
35+
.withPersistenceStore(
36+
DynamoDBPersistenceStore.builder()
37+
.withDynamoDbClient(client)
38+
.withTableName(System.getenv("IDEMPOTENCY_TABLE"))
39+
.build()
40+
).configure();
41+
}
42+
43+
@Idempotent
44+
public String handleRequest(Input input, Context context) {
45+
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME.withZone(TimeZone.getTimeZone("UTC").toZoneId());
46+
return dtf.format(Instant.now());
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
public class Input {
4+
private String message;
5+
6+
public Input(String message) {
7+
this.message = message;
8+
}
9+
10+
public Input() {
11+
}
12+
13+
public String getMessage() {
14+
return message;
15+
}
16+
17+
public void setMessage(String message) {
18+
this.message = message;
19+
}
20+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>software.amazon.lambda</groupId>
7+
<artifactId>e2e-test-handlers-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
11+
<artifactId>e2e-test-handler-logging</artifactId>
12+
<packaging>jar</packaging>
13+
<name>A Lambda function using powertools logging</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>software.amazon.lambda</groupId>
18+
<artifactId>powertools-logging</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
</dependency>
24+
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.codehaus.mojo</groupId>
31+
<artifactId>aspectj-maven-plugin</artifactId>
32+
<configuration>
33+
<source>${maven.compiler.source}</source>
34+
<target>${maven.compiler.target}</target>
35+
<complianceLevel>${maven.compiler.target}</complianceLevel>
36+
<aspectLibraries>
37+
<aspectLibrary>
38+
<groupId>software.amazon.lambda</groupId>
39+
<artifactId>powertools-logging</artifactId>
40+
</aspectLibrary>
41+
</aspectLibraries>
42+
</configuration>
43+
<executions>
44+
<execution>
45+
<goals>
46+
<goal>compile</goal>
47+
</goals>
48+
</execution>
49+
</executions>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-shade-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import org.apache.logging.log4j.LogManager;
6+
import org.apache.logging.log4j.Logger;
7+
import software.amazon.lambda.powertools.logging.Logging;
8+
import software.amazon.lambda.powertools.logging.LoggingUtils;
9+
10+
public class Function implements RequestHandler<Input, String> {
11+
12+
private static final Logger LOG = LogManager.getLogger(Function.class);
13+
14+
@Logging
15+
public String handleRequest(Input input, Context context) {
16+
17+
LoggingUtils.appendKeys(input.getKeys());
18+
LOG.info(input.getMessage());
19+
20+
return "OK";
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import java.util.Map;
4+
5+
public class Input {
6+
private String message;
7+
private Map<String, String> keys;
8+
9+
public Input() {
10+
}
11+
12+
public String getMessage() {
13+
return message;
14+
}
15+
16+
public void setMessage(String message) {
17+
this.message = message;
18+
}
19+
20+
public Map<String, String> getKeys() {
21+
return keys;
22+
}
23+
24+
public void setKeys(Map<String, String> keys) {
25+
this.keys = keys;
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
<Appenders>
4+
<Console name="JsonAppender" target="SYSTEM_OUT">
5+
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Root level="INFO">
10+
<AppenderRef ref="JsonAppender"/>
11+
</Root>
12+
<Logger name="JsonLogger" level="INFO" additivity="false">
13+
<AppenderRef ref="JsonAppender"/>
14+
</Logger>
15+
</Loggers>
16+
</Configuration>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>software.amazon.lambda</groupId>
7+
<artifactId>e2e-test-handlers-parent</artifactId>
8+
<version>1.0.0</version>
9+
</parent>
10+
11+
<artifactId>e2e-test-handler-metrics</artifactId>
12+
<packaging>jar</packaging>
13+
<name>A Lambda function using powertools metrics</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>software.amazon.lambda</groupId>
18+
<artifactId>powertools-metrics</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-lambda-java-events</artifactId>
23+
</dependency>
24+
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.codehaus.mojo</groupId>
31+
<artifactId>aspectj-maven-plugin</artifactId>
32+
<configuration>
33+
<source>${maven.compiler.source}</source>
34+
<target>${maven.compiler.target}</target>
35+
<complianceLevel>${maven.compiler.target}</complianceLevel>
36+
<aspectLibraries>
37+
<aspectLibrary>
38+
<groupId>software.amazon.lambda</groupId>
39+
<artifactId>powertools-metrics</artifactId>
40+
</aspectLibrary>
41+
</aspectLibraries>
42+
</configuration>
43+
<executions>
44+
<execution>
45+
<goals>
46+
<goal>compile</goal>
47+
</goals>
48+
</execution>
49+
</executions>
50+
</plugin>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-shade-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package software.amazon.lambda.powertools.e2e;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.RequestHandler;
5+
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
6+
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
7+
import software.amazon.cloudwatchlogs.emf.model.Unit;
8+
import software.amazon.lambda.powertools.metrics.Metrics;
9+
import software.amazon.lambda.powertools.metrics.MetricsUtils;
10+
11+
public class Function implements RequestHandler<Input, String> {
12+
13+
MetricsLogger metricsLogger = MetricsUtils.metricsLogger();
14+
15+
@Metrics(captureColdStart = true)
16+
public String handleRequest(Input input, Context context) {
17+
18+
DimensionSet dimensionSet = new DimensionSet();
19+
input.getDimensions().forEach((key, value) -> dimensionSet.addDimension(key, value));
20+
metricsLogger.putDimensions(dimensionSet);
21+
22+
input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT));
23+
24+
return "OK";
25+
}
26+
}

0 commit comments

Comments
 (0)