-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathTicketFunctionIntegrationTest.java
86 lines (76 loc) · 3.79 KB
/
TicketFunctionIntegrationTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*/
package com.example;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.amazonaws.services.lambda.runtime.tests.annotations.Event;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.interceptors.TracingInterceptor;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.EnvironmentVariableCredentialsProvider;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.http.HttpStatusCode;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import java.util.ArrayList;
import java.util.List;
// This is an integration test as it requires an actual AWS account.
// It assumes that a DynamoDB table with name "tickets" exists on AWS in US-EAST-2
// and reads AWS Credentials from ~/.aws/credentials with default profile
@ExtendWith(SystemStubsExtension.class)
public class TicketFunctionIntegrationTest {
private static final Region TEST_REGION = Region.US_EAST_2;
@SystemStub
private static EnvironmentVariables environmentVariables;
private final DynamoDbClient ddbClient = DynamoDbClient.builder()
.region(TEST_REGION)
.build();
private List<String> ticketList = new ArrayList<>();
@BeforeAll
public static void setup() {
ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create("default");
AwsCredentials awsCredentials = credentialsProvider.resolveCredentials();
environmentVariables.set("AWS_ACCESS_KEY_ID", awsCredentials.accessKeyId());
environmentVariables.set("AWS_SECRET_ACCESS_KEY", awsCredentials.secretAccessKey());
environmentVariables.set("AWS_REGION", TEST_REGION.id());
}
@AfterEach
public void cleanup() {
DynamoTestUtil.deleteFromDDBTable(ddbClient, ticketList);
ticketList.clear();
}
@ParameterizedTest
@Event(value = "events/apigw_request_1.json", type = APIGatewayProxyRequestEvent.class)
public void testPutTicket(APIGatewayProxyRequestEvent event, EnvironmentVariables environmentVariables) {
//This line manually adds the X-ray segment
AWSXRay.beginSegment("DynamoDb");
TicketFunction function = new TicketFunction();
APIGatewayProxyResponseEvent response = function.handleRequest(event, null);
Assertions.assertNotNull(response);
Assertions.assertNotNull(response.getBody());
String uuidStr = response.getBody();
Assertions.assertNotNull(uuidStr);
ticketList.add(uuidStr.substring(1, uuidStr.length() - 1));
DynamoTestUtil.validateItems(ticketList, ddbClient);
}
@ParameterizedTest
@Event(value = "events/apigw_request_nobody.json", type = APIGatewayProxyRequestEvent.class)
public void testPutTicketBadRequest(APIGatewayProxyRequestEvent event, EnvironmentVariables environmentVariables) {
TicketFunction function = new TicketFunction();
APIGatewayProxyResponseEvent response = function.handleRequest(event, null);
Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode());
}
}