Skip to content

Commit a68201e

Browse files
committed
chore(unit-tests): address review comments
1 parent 4c8bf08 commit a68201e

File tree

16 files changed

+95
-49
lines changed

16 files changed

+95
-49
lines changed

powertools-core/src/test/java/software/amazon/lambda/powertools/core/internal/LambdaHandlerProcessorTest.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import java.util.Optional;
1414

1515
import static org.assertj.core.api.Assertions.assertThat;
16-
import static org.junit.jupiter.api.Assertions.*;
16+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1717
import static org.mockito.Mockito.*;
1818
import static software.amazon.lambda.powertools.core.internal.SystemWrapper.getenv;
1919

@@ -44,7 +44,7 @@ void isHandlerMethod_shouldReturnFalse() {
4444

4545
boolean isHandlerMethod = LambdaHandlerProcessor.isHandlerMethod(pjpMock);
4646

47-
assertFalse(isHandlerMethod);
47+
assertThat(isHandlerMethod).isFalse();
4848
}
4949

5050
@Test
@@ -70,7 +70,7 @@ void placedOnRequestHandler_shouldInvalidateOnWrongNoOfArgs() {
7070

7171
boolean isPlacedOnRequestHandler = LambdaHandlerProcessor.placedOnRequestHandler(pjpMock);
7272

73-
assertFalse(isPlacedOnRequestHandler);
73+
assertThat(isPlacedOnRequestHandler).isFalse();
7474
}
7575

7676
@Test
@@ -90,7 +90,7 @@ void placedOnStreamHandler_shouldInvalidateOnWrongNoOfArgs() {
9090

9191
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
9292

93-
assertFalse(isPlacedOnStreamHandler);
93+
assertThat(isPlacedOnStreamHandler).isFalse();
9494
}
9595

9696
@Test
@@ -100,27 +100,27 @@ void placedOnStreamHandler_shouldInvalidateOnWrongTypeOfArgs() {
100100

101101
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
102102

103-
assertFalse(isPlacedOnStreamHandler);
103+
assertThat(isPlacedOnStreamHandler).isFalse();
104104
}
105105

106106
@Test
107-
void placedOnStreamHandler_shouldInvalidateOnTypeOfArgsOneValid() {
108-
Object[] args = {mock(InputStream.class), new Object(), new Object()};
107+
void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidOutputStreamArg() {
108+
Object[] args = {mock(InputStream.class), new Object(), mock(Context.class)};
109109
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
110110

111111
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
112112

113-
assertFalse(isPlacedOnStreamHandler);
113+
assertThat(isPlacedOnStreamHandler).isFalse();
114114
}
115115

116116
@Test
117-
void placedOnStreamHandler_shouldInvalidateOnTypeOfArgsSomeValid() {
117+
void placedOnStreamHandler_shouldInvalidateOnTypeOfArgs_invalidContextArg() {
118118
Object[] args = {mock(InputStream.class), mock(OutputStream.class), new Object()};
119119
ProceedingJoinPoint pjpMock = mockRequestHandlerPjp(RequestStreamHandler.class, args);
120120

121121
boolean isPlacedOnStreamHandler = LambdaHandlerProcessor.placedOnStreamHandler(pjpMock);
122122

123-
assertFalse(isPlacedOnStreamHandler);
123+
assertThat(isPlacedOnStreamHandler).isFalse();
124124
}
125125

126126
@Test
@@ -131,7 +131,7 @@ void getXrayTraceId_present() {
131131

132132
Optional xRayTraceId = LambdaHandlerProcessor.getXrayTraceId();
133133

134-
assertTrue(xRayTraceId.isPresent());
134+
assertThat(xRayTraceId.isPresent()).isTrue();
135135
assertThat(traceID.split(";")[0].replace(LambdaConstants.ROOT_EQUALS, "")).isEqualTo(xRayTraceId.get());
136136
}
137137
}
@@ -143,7 +143,7 @@ void getXrayTraceId_notPresent() {
143143

144144
boolean isXRayTraceIdPresent = LambdaHandlerProcessor.getXrayTraceId().isPresent();
145145

146-
assertFalse(isXRayTraceIdPresent);
146+
assertThat(isXRayTraceIdPresent).isFalse();
147147
}
148148
}
149149

@@ -174,14 +174,14 @@ void extractContext_notKnownHandler() {
174174

175175
Context context = LambdaHandlerProcessor.extractContext(pjpMock);
176176

177-
assertNull(context);
177+
assertThat(context).isNull();
178178
}
179179

180180
@Test
181181
void isColdStart() {
182182
boolean isColdStart = LambdaHandlerProcessor.isColdStart();
183183

184-
assertTrue(isColdStart);
184+
assertThat(isColdStart).isTrue();
185185
}
186186

187187
@Test
@@ -190,7 +190,7 @@ void isColdStart_coldStartDone() {
190190

191191
boolean isColdStart = LambdaHandlerProcessor.isColdStart();
192192

193-
assertFalse(isColdStart);
193+
assertThat(isColdStart).isFalse();
194194
}
195195

196196
@Test
@@ -200,7 +200,7 @@ void isSamLocal() {
200200

201201
boolean isSamLocal = LambdaHandlerProcessor.isSamLocal();
202202

203-
assertTrue(isSamLocal);
203+
assertThat(isSamLocal).isTrue();
204204
}
205205
}
206206

@@ -212,7 +212,7 @@ void serviceName() {
212212

213213
String actualServiceName = LambdaHandlerProcessor.serviceName();
214214

215-
assertEquals(expectedServiceName, actualServiceName);
215+
assertThat(actualServiceName).isEqualTo(expectedServiceName);
216216
}
217217
}
218218

@@ -222,7 +222,7 @@ void serviceName_Undefined() {
222222
try (MockedStatic<SystemWrapper> mockedSystemWrapper = mockStatic(SystemWrapper.class)) {
223223
mockedSystemWrapper.when(() -> getenv(LambdaConstants.POWERTOOLS_SERVICE_NAME)).thenReturn(null);
224224

225-
assertEquals(LambdaConstants.SERVICE_UNDEFINED, LambdaHandlerProcessor.serviceName());
225+
assertThat(LambdaHandlerProcessor.serviceName()).isEqualTo(LambdaConstants.SERVICE_UNDEFINED);
226226
}
227227
}
228228

Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
import static software.amazon.lambda.powertools.logging.CorrelationIdPathConstants.APPLICATION_LOAD_BALANCER;
2424

25-
public class PowerLogToolAlbCorrelationId implements RequestHandler<ApplicationLoadBalancerRequestEvent, Object> {
26-
private final Logger LOG = LogManager.getLogger(PowerLogToolAlbCorrelationId.class);
25+
public class PowertoolsLogAlbCorrelationId implements RequestHandler<ApplicationLoadBalancerRequestEvent, Object> {
26+
private final Logger LOG = LogManager.getLogger(PowertoolsLogAlbCorrelationId.class);
2727

2828
@Override
2929
@Logging(correlationIdPath = APPLICATION_LOAD_BALANCER)
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import software.amazon.lambda.powertools.logging.Logging;
2121
import software.amazon.lambda.powertools.logging.LoggingUtils;
2222

23-
public class PowerLogToolEnabledWithClearState implements RequestHandler<Object, Object> {
23+
public class PowertoolsLogEnabledWithClearState implements RequestHandler<Object, Object> {
2424
public static int COUNT = 1;
25-
private static final Logger LOG = LogManager.getLogger(PowerLogToolEnabledWithClearState.class);
25+
private static final Logger LOG = LogManager.getLogger(PowertoolsLogEnabledWithClearState.class);
2626

2727
@Override
2828
@Logging(clearState = true)
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
import static software.amazon.lambda.powertools.logging.CorrelationIdPathConstants.EVENT_BRIDGE;
2727

28-
public class PowerLogToolEventBridgeCorrelationId implements RequestStreamHandler {
28+
public class PowertoolsLogEventBridgeCorrelationId implements RequestStreamHandler {
2929

30-
private final Logger LOG = LogManager.getLogger(PowerLogToolEventBridgeCorrelationId.class);
30+
private final Logger LOG = LogManager.getLogger(PowertoolsLogEventBridgeCorrelationId.class);
3131

3232
@Override
3333
@Logging(correlationIdPath = EVENT_BRIDGE)

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaLoggingAspectTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void shouldLogCorrelationIdOnAPIGatewayV2HTTPEvent(APIGatewayV2HTTPEvent event)
264264
@ParameterizedTest
265265
@Event(value = "albEvent.json", type = ApplicationLoadBalancerRequestEvent.class)
266266
void shouldLogCorrelationIdOnALBEvent(ApplicationLoadBalancerRequestEvent event) {
267-
RequestHandler<ApplicationLoadBalancerRequestEvent, Object> handler = new PowerLogToolAlbCorrelationId();
267+
RequestHandler<ApplicationLoadBalancerRequestEvent, Object> handler = new PowertoolsLogAlbCorrelationId();
268268
handler.handleRequest(event, context);
269269

270270
assertThat(ThreadContext.getImmutableContext())
@@ -274,7 +274,7 @@ void shouldLogCorrelationIdOnALBEvent(ApplicationLoadBalancerRequestEvent event)
274274

275275
@Test
276276
void shouldLogCorrelationIdOnStreamHandler() throws IOException {
277-
RequestStreamHandler handler = new PowerLogToolEventBridgeCorrelationId();
277+
RequestStreamHandler handler = new PowertoolsLogEventBridgeCorrelationId();
278278
String eventId = "3";
279279
String event = "{\"id\":" + eventId + "}"; // CorrelationIdPathConstants.EVENT_BRIDGE
280280
ByteArrayInputStream inputStream = new ByteArrayInputStream(event.getBytes());
@@ -288,7 +288,7 @@ void shouldLogCorrelationIdOnStreamHandler() throws IOException {
288288

289289
@Test
290290
void shouldLogAndClearLogContextOnEachRequest() throws IOException {
291-
requestHandler = new PowerLogToolEnabledWithClearState();
291+
requestHandler = new PowertoolsLogEnabledWithClearState();
292292
S3EventNotification s3EventNotification = s3EventNotification();
293293

294294
requestHandler.handleRequest(s3EventNotification, context);

powertools-parameters/src/test/java/software/amazon/lambda/powertools/parameters/transform/TransformationManagerTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import org.junit.jupiter.api.BeforeEach;
1717
import org.junit.jupiter.api.Test;
18-
import org.mockito.Mock;
1918
import software.amazon.lambda.powertools.parameters.exception.TransformationException;
2019

2120
import java.util.Base64;

powertools-serialization/src/main/java/software/amazon/lambda/powertools/utilities/jmespath/Base64GZipFunction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected <T> T callFunction(Adapter<T> runtime, List<FunctionArgument<T>> argum
5050
}
5151

5252
public static String decompress(byte[] compressed) {
53-
if (compressed.length == 0) {
53+
if (compressed == null || compressed.length == 0) {
5454
return "";
5555
}
5656
if (!isCompressed(compressed)) {

powertools-serialization/src/test/java/software/amazon/lambda/powertools/utilities/EventDeserializerTest.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.junit.jupiter.params.ParameterizedTest;
2020
import software.amazon.lambda.powertools.utilities.model.Basket;
21+
import software.amazon.lambda.powertools.utilities.model.Order;
2122
import software.amazon.lambda.powertools.utilities.model.Product;
2223

2324
import java.util.HashMap;
@@ -134,9 +135,9 @@ public void testDeserializeAPIGatewayEventAsList_shouldThrowException(APIGateway
134135
}
135136

136137
@ParameterizedTest
137-
@Event(value = "apigw_event.json", type = HashMap.class)
138-
public void testDeserializeAPIGatewayMapEventAsList_shouldThrowException(Map<String, APIGatewayProxyRequestEvent> event) {
139-
assertThatThrownBy(() -> extractDataFrom(event).asListOf(Product.class))
138+
@Event(value = "custom_event_map.json", type = HashMap.class)
139+
public void testDeserializeAPIGatewayMapEventAsList_shouldThrowException(Map<String, Order> event) {
140+
assertThatThrownBy(() -> extractDataFrom(event).asListOf(Order.class))
140141
.isInstanceOf(EventDeserializationException.class)
141142
.hasMessage("The content of this event is not a list, consider using 'as' instead");
142143
}
@@ -198,11 +199,12 @@ public void testDeserializeScheduledEventMessageAsObject_shouldReturnObject(Sche
198199
Product product = extractDataFrom(event).as(Product.class);
199200
assertProduct(product);
200201
}
202+
201203
@ParameterizedTest
202204
@Event(value = "alb_event.json", type = ApplicationLoadBalancerRequestEvent.class)
203205
public void testDeserializeALBEventMessageAsObjectShouldReturnObject(ApplicationLoadBalancerRequestEvent event) {
204-
Product product = extractDataFrom(event).as(Product.class);
205-
assertProduct(product);
206+
Product product = extractDataFrom(event).as(Product.class);
207+
assertProduct(product);
206208
}
207209

208210
@ParameterizedTest
@@ -214,7 +216,7 @@ public void testDeserializeCWLEventMessageAsObjectShouldReturnObject(CloudWatchL
214216

215217
@ParameterizedTest
216218
@Event(value = "kf_event.json", type = KinesisFirehoseEvent.class)
217-
public void testDeserializeKFEventMessageAsListShouldReturnList(KinesisFirehoseEvent event) {
219+
public void testDeserializeKFEventMessageAsListShouldReturnList(KinesisFirehoseEvent event) {
218220
List<Product> products = extractDataFrom(event).asListOf(Product.class);
219221
assertThat(products).hasSize(1);
220222
assertProduct(products.get(0));

powertools-serialization/src/test/java/software/amazon/lambda/powertools/utilities/jmespath/Base64GZipFunctionTest.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@
1717
import com.fasterxml.jackson.databind.node.JsonNodeType;
1818
import io.burt.jmespath.Expression;
1919
import io.burt.jmespath.JmesPathType;
20-
import io.burt.jmespath.function.ArgumentConstraints;
2120
import org.junit.jupiter.api.Test;
2221
import software.amazon.lambda.powertools.utilities.JsonConfig;
2322

2423
import java.io.IOException;
2524

2625
import static org.assertj.core.api.Assertions.assertThat;
27-
import static org.junit.jupiter.api.Assertions.assertEquals;
2826

2927
public class Base64GZipFunctionTest {
3028

3129
@Test
3230
public void testConstructor() {
3331
Base64GZipFunction base64GZipFunction = new Base64GZipFunction();
34-
assertEquals(base64GZipFunction.name(), "powertools_base64_gzip");
35-
assertEquals(base64GZipFunction.argumentConstraints().expectedType().toLowerCase(), JmesPathType.STRING.name().toLowerCase());
36-
assertEquals(base64GZipFunction.argumentConstraints().minArity(), 1);
37-
assertEquals(base64GZipFunction.argumentConstraints().minArity(), 1);
32+
assertThat(base64GZipFunction.name()).isEqualTo("powertools_base64_gzip");
33+
assertThat(base64GZipFunction.argumentConstraints().expectedType().toLowerCase()).isEqualTo(JmesPathType.STRING.name().toLowerCase());
34+
assertThat(base64GZipFunction.argumentConstraints().minArity()).isEqualTo(1);
35+
assertThat(base64GZipFunction.argumentConstraints().maxArity()).isEqualTo(1);
3836

3937
}
4038

@@ -56,6 +54,12 @@ public void testPowertoolsGzipEmptyJsonAttribute() throws IOException {
5654
assertThat(result.asText()).isEqualTo("");
5755
}
5856

57+
@Test
58+
public void testBase64GzipDecompress() {
59+
String result = Base64GZipFunction.decompress(null);
60+
assertThat(result).isEqualTo("");
61+
}
62+
5963
@Test
6064
public void testPowertoolsGzipNotCompressedJsonAttribute() throws IOException {
6165
JsonNode event = JsonConfig.get().getObjectMapper().readTree(this.getClass().getResourceAsStream("/custom_event_gzip.json"));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2023 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
package software.amazon.lambda.powertools.utilities.model;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
19+
public class Order {
20+
private Map<String, Product> orders = new HashMap<>();
21+
22+
public Order() {
23+
}
24+
25+
public Map<String, Product> getProducts() {
26+
return orders;
27+
}
28+
29+
public void setProducts(Map<String, Product> products) {
30+
this.orders = products;
31+
}
32+
33+
}

powertools-serialization/src/test/resources/amq_event.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"destination": {
1616
"physicalName": "testQueue"
1717
},
18-
"data":"ewogICJpZCI6IDEyMzQsCiAgIm5hbWUiOiAicHJvZHVjdCIsCiAgInByaWNlIjogNDIKfQ==",
18+
"data": "ewogICJpZCI6IDEyMzQsCiAgIm5hbWUiOiAicHJvZHVjdCIsCiAgInByaWNlIjogNDIKfQ==",
1919
"timestamp": 1598827811958,
2020
"brokerInTime": 1598827811958,
2121
"brokerOutTime": 1598827811959,

powertools-serialization/src/test/resources/cfcr_event.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"LogicalResourceId": "logicalResourceId",
88
"ResourceType": "resourceType",
99
"ResourceProperties": {
10-
"id":1234,
10+
"id": 1234,
1111
"name": "product",
1212
"price": 42
1313
},
1414
"OldResourceProperties": {
15-
"id":1234,
15+
"id": 1234,
1616
"name": "product",
1717
"price": 40
1818
}

powertools-serialization/src/test/resources/custom_event.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"basket": {
3-
"products" : [
3+
"products": [
44
{
55
"id": 43242,
66
"name": "FooBar XY",

powertools-serialization/src/test/resources/custom_event_gzip.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"basket": {
3-
"products" : [
3+
"products": [
44
{
55
"id": 43242,
66
"name": "FooBar XY",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"products": {
3+
"12345": {
4+
"id": 43242,
5+
"name": "FooBar XY",
6+
"price": 258
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)