Skip to content

Use JUnit 5 Parameterized Test #319

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 1 commit into from
Apr 4, 2022
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
8 changes: 7 additions & 1 deletion aws-lambda-java-serialization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.7.0</version>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,186 +6,58 @@
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

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


public class LambdaEventSerializersTest {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static final ClassLoader SYSTEM_CLASS_LOADER = ClassLoader.getSystemClassLoader();

@Test
public void testAPIGatewayProxyRequestEvent() throws IOException {
String expected = readEvent("api_gateway_proxy_request_event.json");
String actual = deserializeSerializeJsonToString(expected, APIGatewayProxyRequestEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testAPIGatewayProxyResponseEvent() throws IOException {
String expected = readEvent("api_gateway_proxy_response_event.json");
String actual = deserializeSerializeJsonToString(expected, APIGatewayProxyResponseEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testCloudFrontEvent() throws IOException {
String expected = readEvent("cloud_front_event.json");
String actual = deserializeSerializeJsonToString(expected, CloudFrontEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testCloudWatchLogsEvent() throws IOException {
String expected = readEvent("cloud_watch_logs_event.json");
String actual = deserializeSerializeJsonToString(expected, CloudWatchLogsEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testCodeCommitEvent() throws IOException {
String expected = readEvent("code_commit_event.json");
String actual = deserializeSerializeJsonToString(expected, CodeCommitEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testCognitoEvent() throws IOException {
String expected = readEvent("cognito_event.json");
String actual = deserializeSerializeJsonToString(expected, CognitoEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testConfigEvent() throws IOException {
String expected = readEvent("config_event.json");
String actual = deserializeSerializeJsonToString(expected, ConfigEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testDynamodbEvent() throws IOException {
String expected = readEvent("dynamodb_event.json");
String actual = deserializeSerializeJsonToString(expected, DynamodbEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testIoTButtonEvent() throws IOException {
String expected = readEvent("iot_button_event.json");
String actual = deserializeSerializeJsonToString(expected, IoTButtonEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisAnalyticsFirehoseInputPreprocessingEvent() throws IOException {
String expected = readEvent("kinesis_analytics_firehose_input_preprocessing_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsFirehoseInputPreprocessingEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisAnalyticsInputPreprocessingResponse() throws IOException {
String expected = readEvent("kinesis_analytics_input_preprocessing_response_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsInputPreprocessingResponse.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisAnalyticsOutputDeliveryEvent() throws IOException {
String expected = readEvent("kinesis_analytics_output_delivery_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsOutputDeliveryEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisAnalyticsOutputDeliveryResponse() throws IOException {
String expected = readEvent("kinesis_analytics_output_delivery_response_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsOutputDeliveryResponse.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisAnalyticsStreamsInputPreprocessingEvent() throws IOException {
String expected = readEvent("kinesis_analytics_streams_input_preprocessing_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisAnalyticsStreamsInputPreprocessingEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisEvent() throws IOException {
String expected = readEvent("kinesis_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testKinesisFirehoseEvent() throws IOException {
String expected = readEvent("kinesis_firehose_event.json");
String actual = deserializeSerializeJsonToString(expected, KinesisFirehoseEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testLexEvent() throws IOException {
String expected = readEvent("lex_event.json");
String actual = deserializeSerializeJsonToString(expected, LexEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testS3Event() throws IOException {
String expected = readEvent("s3_event.json");
String actual = deserializeSerializeJsonToString(expected, S3Event.class);

assertJsonEqual(expected, actual);
}

@Test
public void testScheduledEvent() throws IOException {
String expected = readEvent("scheduled_event.json");
String actual = deserializeSerializeJsonToString(expected, ScheduledEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testSNSEvent() throws IOException {
String expected = readEvent("sns_event.json");
String actual = deserializeSerializeJsonToString(expected, SNSEvent.class);

assertJsonEqual(expected, actual);
}

@Test
public void testSQSEvent() throws IOException {
String expected = readEvent("sqs_event.json");
String actual = deserializeSerializeJsonToString(expected, SQSEvent.class);
private static Stream<Arguments> serdeArguments() {
return Stream.of(
Arguments.of("api_gateway_proxy_request_event.json", APIGatewayProxyRequestEvent.class),
Arguments.of("api_gateway_proxy_response_event.json", APIGatewayProxyResponseEvent.class),
Arguments.of("cloud_front_event.json", CloudFrontEvent.class),
Arguments.of("cloud_watch_logs_event.json", CloudWatchLogsEvent.class),
Arguments.of("code_commit_event.json", CodeCommitEvent.class),
Arguments.of("api_gateway_proxy_response_event.json", APIGatewayProxyResponseEvent.class),
Arguments.of("cognito_event.json", CognitoEvent.class),
Arguments.of("config_event.json", ConfigEvent.class),
Arguments.of("dynamodb_event.json", DynamodbEvent.class),
Arguments.of("iot_button_event.json", IoTButtonEvent.class),
Arguments.of("kinesis_analytics_firehose_input_preprocessing_event.json", KinesisAnalyticsFirehoseInputPreprocessingEvent.class),
Arguments.of("kinesis_analytics_input_preprocessing_response_event.json", KinesisAnalyticsInputPreprocessingResponse.class),
Arguments.of("kinesis_analytics_output_delivery_event.json", KinesisAnalyticsOutputDeliveryEvent.class),
Arguments.of("kinesis_analytics_output_delivery_response_event.json", KinesisAnalyticsOutputDeliveryResponse.class),
Arguments.of("kinesis_analytics_streams_input_preprocessing_event.json", KinesisAnalyticsStreamsInputPreprocessingEvent.class),
Arguments.of("kinesis_event.json", KinesisEvent.class),
Arguments.of("kinesis_firehose_event.json", KinesisFirehoseEvent.class),
Arguments.of("lex_event.json", LexEvent.class),
Arguments.of("s3_event.json", S3Event.class),
Arguments.of("scheduled_event.json", ScheduledEvent.class),
Arguments.of("sns_event.json", SNSEvent.class),
Arguments.of("sqs_event.json", SQSEvent.class)
);
}

@ParameterizedTest(name = "Serde {0} Event")
@MethodSource("serdeArguments")
public void testAPIGatewayProxyRequestEvent(final String json,
final Class<?> eventClass) throws IOException {
String expected = readEvent(json);
String actual = deserializeSerializeJsonToString(expected, eventClass);

assertJsonEqual(expected, actual);
}
Expand Down Expand Up @@ -213,5 +85,4 @@ private String bytesToString(byte[] bytes) {
private void assertJsonEqual(String expected, String actual) throws IOException {
assertEquals(OBJECT_MAPPER.readTree(expected), OBJECT_MAPPER.readTree(actual));
}

}