-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathTicketEnd2EndTest.java
86 lines (74 loc) · 2.8 KB
/
TicketEnd2EndTest.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 org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.http.HttpStatusCode;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest;
import software.amazon.awssdk.services.cloudformation.model.DescribeStacksResponse;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import java.util.ArrayList;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.notNullValue;
// This test requires an AWS account and assumes the SAM template part of this repo is deployed to AWS
// This test runs END 2 END by sending an HTTP request to API Gateway URL and
// verifies if entries are persisted in DDB
public class TicketEnd2EndTest {
//Name of the stack you used when deploying SAM template
private static final String STACK_NAME = "APIGW-Lambda-DDB-Sample";
private static DynamoDbClient ddbClient;
private static CloudFormationClient cloudFormationClient;
private static String apiEndPoint = "";
private List<String> ticketList = new ArrayList<>();
@BeforeAll
static void setUp() {
ddbClient = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build();
cloudFormationClient = CloudFormationClient
.builder().region(Region.US_EAST_1).build();
DescribeStacksResponse response = cloudFormationClient.describeStacks(DescribeStacksRequest.builder()
.stackName(STACK_NAME)
.build());
apiEndPoint = response.stacks().get(0).outputs().get(0).outputValue();
}
@AfterEach
public void cleanup() {
DynamoTestUtil.deleteFromDDBTable(ddbClient, ticketList);
ticketList.clear();
}
@Test
public void testPost() {
String ticketId =
given()
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.body("{\"description\": \"Lambda rocks\", \"userId\": \"testuser\"}")
.when()
.post(apiEndPoint)
.then()
.assertThat().statusCode(HttpStatusCode.OK)
.body(notNullValue())
.extract()
.asString();
ticketList.add(ticketId.substring(1, ticketId.length() - 1));
DynamoTestUtil.validateItems(ticketList, ddbClient);
}
@Test
public void testPostWithBadPayload() {
given()
.header("Content-Type", "application/json")
.header("Accept", "application/json")
.body("{\"description\":, \"userId\": \"testuser\"}")
.when()
.post(apiEndPoint)
.then()
.assertThat().statusCode(HttpStatusCode.BAD_REQUEST);
}
}