-
Notifications
You must be signed in to change notification settings - Fork 102
/
TicketFunctionMockTest.java
80 lines (68 loc) · 3.35 KB
/
TicketFunctionMockTest.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
/*
* 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.example.model.Ticket;
import com.example.utils.DDBUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import software.amazon.awssdk.http.HttpStatusCode;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import java.util.UUID;
//This is a Unit test that uses Mockito to mock calls to AWS services
public class TicketFunctionMockTest {
@Mock
private transient DDBUtils testUtils;
private transient String uuid = null;
private TicketFunction function;
@BeforeEach
public void beforeEach() {
MockitoAnnotations.openMocks(this);
function = new TicketFunction(testUtils);
}
@ParameterizedTest
@Event(value = "events/apigw_request_1.json", type = APIGatewayProxyRequestEvent.class)
public void testEventDeserialization(APIGatewayProxyRequestEvent event) {
uuid = UUID.randomUUID().toString();
Mockito.when(testUtils.persistTicket(Mockito.any(Ticket.class))).thenReturn(uuid);
APIGatewayProxyResponseEvent response = function.handleRequest(event, null);
ArgumentCaptor<Ticket> ticketArgumentCaptor = ArgumentCaptor.forClass(Ticket.class);
Mockito.verify(testUtils).persistTicket(ticketArgumentCaptor.capture());
Assertions.assertEquals("Lambda rocks", ticketArgumentCaptor.getValue().getDescription());
Assertions.assertEquals("testuser", ticketArgumentCaptor.getValue().getUserId());
}
@ParameterizedTest
@Event(value = "events/apigw_request_1.json", type = APIGatewayProxyRequestEvent.class)
public void testPutTicketWithEvent(APIGatewayProxyRequestEvent event) {
uuid = UUID.randomUUID().toString();
Mockito.when(testUtils.persistTicket(Mockito.any(Ticket.class))).thenReturn(uuid);
APIGatewayProxyResponseEvent response = function.handleRequest(event, null);
Assertions.assertNotNull(response);
Assertions.assertNotNull(response.getBody());
Assertions.assertEquals(response.getBody(), "\"" + uuid + "\"");
}
@ParameterizedTest
@Event(value = "events/apigw_request_nobody.json", type = APIGatewayProxyRequestEvent.class)
public void testPutTicketWithBadEvent(APIGatewayProxyRequestEvent event) {
APIGatewayProxyResponseEvent response = function.handleRequest(event, null);
Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode());
}
@ParameterizedTest
@Event(value = "events/apigw_request_1.json", type = APIGatewayProxyRequestEvent.class)
public void testDynamoDBError(APIGatewayProxyRequestEvent event) {
Mockito.when(testUtils.persistTicket(Mockito.any(Ticket.class))).thenThrow(DynamoDbException.class);
APIGatewayProxyResponseEvent response = function.handleRequest(event, null);
Assertions.assertNotNull(response);
Assertions.assertEquals(HttpStatusCode.INTERNAL_SERVER_ERROR, response.getStatusCode());
}
}