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

feat: end-to-end tests for core modules and idempotency #970

Merged
merged 14 commits into from
Mar 21, 2023
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
<module>powertools-validation</module>
<module>powertools-test-suite</module>
<module>powertools-cloudformation</module>
<module>powertools-idempotency</module>
<module>powertools-idempotency</module>
<module>powertools-e2e-tests</module>
<module>examples</module>
</modules>

Expand Down
16 changes: 16 additions & 0 deletions powertools-e2e-tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## End-to-end tests
This module is internal and meant to be used for end-to-end (E2E) testing of Lambda Powertools for Java.

__Prerequisites__:
- An AWS account is needed as well as a local environment able to reach this account
([credentials](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials.html)).
- [Java 11+](https://docs.aws.amazon.com/corretto/latest/corretto-11-ug/downloads-list.html)
- [Docker](https://docs.docker.com/engine/install/)

To execute the E2E tests, use the following command: `export JAVA_VERSION=11 && mvn clean verify -Pe2e`

### Under the hood
This module leverages the following components:
- AWS CDK to define the infrastructure and synthesize a CloudFormation template and the assets (lambda function packages)
- The AWS S3 SDK to push the assets on S3
- The AWS CloudFormation SDK to deploy the template
jeromevdl marked this conversation as resolved.
Show resolved Hide resolved
57 changes: 57 additions & 0 deletions powertools-e2e-tests/handlers/idempotency/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<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>

<parent>
<groupId>software.amazon.lambda</groupId>
<artifactId>e2e-test-handlers-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>e2e-test-handler-idempotency</artifactId>
<packaging>jar</packaging>
<name>A Lambda function using powertools idempotency</name>

<dependencies>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-idempotency</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
jeromevdl marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>aws-lambda-java-events</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-idempotency</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package software.amazon.lambda.powertools.e2e;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.lambda.powertools.idempotency.Idempotency;
import software.amazon.lambda.powertools.idempotency.IdempotencyConfig;
import software.amazon.lambda.powertools.idempotency.Idempotent;
import software.amazon.lambda.powertools.idempotency.persistence.DynamoDBPersistenceStore;

import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.TimeZone;


public class Function implements RequestHandler<Input, String> {

public Function() {
this(DynamoDbClient
.builder()
.httpClient(UrlConnectionHttpClient.builder().build())
.region(Region.of(System.getenv("AWS_REGION")))
.build());
}

public Function(DynamoDbClient client) {
Idempotency.config().withConfig(
IdempotencyConfig.builder()
.withExpiration(Duration.of(10, ChronoUnit.SECONDS))
.build())
.withPersistenceStore(
DynamoDBPersistenceStore.builder()
.withDynamoDbClient(client)
.withTableName(System.getenv("IDEMPOTENCY_TABLE"))
.build()
).configure();
}

@Idempotent
public String handleRequest(Input input, Context context) {
DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME.withZone(TimeZone.getTimeZone("UTC").toZoneId());
return dtf.format(Instant.now());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package software.amazon.lambda.powertools.e2e;

public class Input {
private String message;

public Input(String message) {
this.message = message;
}

public Input() {
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
57 changes: 57 additions & 0 deletions powertools-e2e-tests/handlers/logging/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<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>

<parent>
<groupId>software.amazon.lambda</groupId>
<artifactId>e2e-test-handlers-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>e2e-test-handler-logging</artifactId>
<packaging>jar</packaging>
<name>A Lambda function using powertools logging</name>

<dependencies>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
jeromevdl marked this conversation as resolved.
Show resolved Hide resolved
<artifactId>aws-lambda-java-events</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-logging</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package software.amazon.lambda.powertools.e2e;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.lambda.powertools.logging.Logging;
import software.amazon.lambda.powertools.logging.LoggingUtils;

public class Function implements RequestHandler<Input, String> {

private static final Logger LOG = LogManager.getLogger(Function.class);

@Logging
public String handleRequest(Input input, Context context) {

LoggingUtils.appendKeys(input.getKeys());
LOG.info(input.getMessage());

return "OK";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package software.amazon.lambda.powertools.e2e;

import java.util.Map;

public class Input {
private String message;
private Map<String, String> keys;

public Input() {
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public Map<String, String> getKeys() {
return keys;
}

public void setKeys(Map<String, String> keys) {
this.keys = keys;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="JsonAppender" target="SYSTEM_OUT">
<JsonTemplateLayout eventTemplateUri="classpath:LambdaJsonLayout.json" />
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="JsonAppender"/>
</Root>
<Logger name="JsonLogger" level="INFO" additivity="false">
<AppenderRef ref="JsonAppender"/>
</Logger>
</Loggers>
</Configuration>
57 changes: 57 additions & 0 deletions powertools-e2e-tests/handlers/metrics/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<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>

<parent>
<groupId>software.amazon.lambda</groupId>
<artifactId>e2e-test-handlers-parent</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>e2e-test-handler-metrics</artifactId>
<packaging>jar</packaging>
<name>A Lambda function using powertools metrics</name>

<dependencies>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<complianceLevel>${maven.compiler.target}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-metrics</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package software.amazon.lambda.powertools.e2e;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.lambda.powertools.metrics.MetricsUtils;

public class Function implements RequestHandler<Input, String> {

MetricsLogger metricsLogger = MetricsUtils.metricsLogger();

@Metrics(captureColdStart = true)
public String handleRequest(Input input, Context context) {

DimensionSet dimensionSet = new DimensionSet();
input.getDimensions().forEach((key, value) -> dimensionSet.addDimension(key, value));
metricsLogger.putDimensions(dimensionSet);

input.getMetrics().forEach((key, value) -> metricsLogger.putMetric(key, value, Unit.COUNT));

return "OK";
}
}
Loading