@@ -93,6 +93,8 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar
93
93
requestHandler = new PowertoolsLogEnabled ();
94
94
requestStreamHandler = new PowertoolsLogEnabledForStream ();
95
95
resetLogLevel (Level .INFO );
96
+ writeStaticField (LambdaLoggingAspect .class , "LOG_EVENT" , null , true );
97
+ writeStaticField (LambdaLoggingAspect .class , "SAMPLING_RATE" , null , true );
96
98
try {
97
99
FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
98
100
} catch (NoSuchFileException e ) {
@@ -186,58 +188,66 @@ void shouldClearStateWhenClearStateIsTrue() {
186
188
@ Test
187
189
void shouldLogDebugWhenSamplingEqualsOne () {
188
190
PowertoolsLogSamplingEnabled handler = new PowertoolsLogSamplingEnabled ();
191
+
189
192
Boolean debugEnabled = handler .handleRequest (new Object (), context );
193
+
190
194
assertThat (debugEnabled ).isTrue ();
191
195
}
192
196
193
197
@ Test
194
- void shouldLogDebugWhenSamplingEnvVarEqualsOne () {
195
- try ( MockedStatic < SystemWrapper > mocked = mockStatic ( SystemWrapper . class )) {
196
- mocked . when (() -> getenv ( "POWERTOOLS_LOGGER_SAMPLE_RATE" ))
197
- . thenReturn ( "1" );
198
+ void shouldLogDebugWhenSamplingEnvVarEqualsOne () throws IllegalAccessException {
199
+ // GIVEN
200
+ writeStaticField ( LambdaLoggingAspect . class , "SAMPLING_RATE" , "1" , true );
201
+ PowertoolsLogEnabled handler = new PowertoolsLogEnabled ( );
198
202
199
- PowertoolsLogEnabled handler = new PowertoolsLogEnabled ();
200
- handler .handleRequest (new Object (), context );
201
- File logFile = new File ("target/logfile.json" );
202
- assertThat (contentOf (logFile )).contains ("Test debug event" );
203
- }
203
+ // WHEN
204
+ handler .handleRequest (new Object (), context );
205
+
206
+ // THEN
207
+ File logFile = new File ("target/logfile.json" );
208
+ assertThat (contentOf (logFile )).contains ("Test debug event" );
204
209
}
205
210
206
211
@ Test
207
- void shouldNotLogDebugWhenSamplingEnvVarIsTooBig () {
208
- try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
209
- mocked .when (() -> getenv ("POWERTOOLS_LOGGER_SAMPLE_RATE" ))
210
- .thenReturn ("42" );
212
+ void shouldNotLogDebugWhenSamplingEnvVarIsTooBig () throws IllegalAccessException {
213
+ // GIVEN
214
+ writeStaticField (LambdaLoggingAspect .class , "SAMPLING_RATE" , "42" , true );
211
215
212
- requestHandler .handleRequest (new Object (), context );
213
- File logFile = new File ("target/logfile.json" );
214
- assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
215
- }
216
+ // WHEN
217
+ requestHandler .handleRequest (new Object (), context );
218
+
219
+ // THEN
220
+ File logFile = new File ("target/logfile.json" );
221
+ assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
216
222
}
217
223
218
224
@ Test
219
- void shouldNotLogDebugWhenSamplingEnvVarIsInvalid () {
220
- try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
221
- mocked .when (() -> getenv ("POWERTOOLS_LOGGER_SAMPLE_RATE" ))
222
- .thenReturn ("NotANumber" );
225
+ void shouldNotLogDebugWhenSamplingEnvVarIsInvalid () throws IllegalAccessException {
226
+ // GIVEN
227
+ writeStaticField (LambdaLoggingAspect .class , "SAMPLING_RATE" , "NotANumber" , true );
223
228
229
+ // WHEN
224
230
requestHandler .handleRequest (new Object (), context );
225
- File logFile = new File ("target/logfile.json" );
226
- assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
227
- assertThat (contentOf (logFile )).contains (
228
- "Skipping sampling rate on environment variable configuration because of invalid value" );
229
- }
231
+
232
+ // THEN
233
+ File logFile = new File ("target/logfile.json" );
234
+ assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
235
+ assertThat (contentOf (logFile )).contains (
236
+ "Skipping sampling rate on environment variable configuration because of invalid value" );
230
237
}
231
238
232
239
@ Test
233
240
void shouldNotLogDebugWhenSamplingEqualsZero () {
234
241
PowertoolsLogSamplingDisabled handler = new PowertoolsLogSamplingDisabled ();
242
+
235
243
Boolean debugEnabled = handler .handleRequest (new Object (), context );
244
+
236
245
assertThat (debugEnabled ).isFalse ();
237
246
}
238
247
239
248
@ Test
240
249
void shouldHaveNoEffectIfNotUsedOnLambdaHandler () {
250
+ // GIVEN
241
251
PowertoolsLogEnabled handler = new PowertoolsLogEnabled ();
242
252
243
253
handler .anotherMethod ();
@@ -247,24 +257,31 @@ void shouldHaveNoEffectIfNotUsedOnLambdaHandler() {
247
257
248
258
@ Test
249
259
void shouldLogServiceNameWhenEnvVarSet () throws IllegalAccessException {
260
+ // GIVEN
250
261
writeStaticField (LambdaHandlerProcessor .class , "SERVICE_NAME" , "testService" , true );
262
+
263
+ // WHEN
251
264
requestHandler .handleRequest (new Object (), context );
252
265
266
+ // THEN
253
267
assertThat (MDC .getCopyOfContextMap ())
254
268
.hasSize (EXPECTED_CONTEXT_SIZE )
255
269
.containsEntry (SERVICE .getName (), "testService" );
256
270
}
257
271
258
272
@ Test
259
273
void shouldLogxRayTraceIdEnvVarSet () {
274
+ // GIVEN
260
275
String xRayTraceId = "1-5759e988-bd862e3fe1be46a994272793" ;
261
276
262
277
try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
263
278
mocked .when (() -> getenv ("_X_AMZN_TRACE_ID" ))
264
279
.thenReturn ("Root=1-5759e988-bd862e3fe1be46a994272793;Parent=53995c3f42cd8ad8;Sampled=1\" " );
265
280
281
+ // WHEN
266
282
requestHandler .handleRequest (new Object (), context );
267
283
284
+ // THEN
268
285
assertThat (MDC .getCopyOfContextMap ())
269
286
.hasSize (EXPECTED_CONTEXT_SIZE + 1 )
270
287
.containsEntry (FUNCTION_TRACE_ID .getName (), xRayTraceId );
@@ -273,35 +290,41 @@ void shouldLogxRayTraceIdEnvVarSet() {
273
290
274
291
@ Test
275
292
void shouldLogEventForHandlerWithLogEventAnnotation () {
293
+ // GIVEN
276
294
requestHandler = new PowertoolsLogEvent ();
277
295
296
+ // WHEN
278
297
requestHandler .handleRequest (Collections .singletonList ("ListOfOneElement" ), context );
279
298
299
+ // THEN
280
300
File logFile = new File ("target/logfile.json" );
281
301
assertThat (contentOf (logFile )).contains ("[\" ListOfOneElement\" ]" );
282
302
}
283
303
284
304
@ Test
285
- void shouldLogEventForHandlerWithLogEventEnvVar () {
305
+ void shouldLogEventForHandlerWithLogEventEnvVar () throws IllegalAccessException {
306
+ // GIVEN
307
+ writeStaticField (LambdaLoggingAspect .class , "LOG_EVENT" , "true" , true );
286
308
requestHandler = new PowertoolsLogEnabled ();
287
309
288
- try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
289
- mocked .when (() -> getenv ("POWERTOOLS_LOGGER_LOG_EVENT" ))
290
- .thenReturn ("true" );
291
-
292
- requestHandler .handleRequest (Collections .singletonList ("ListOfOneElement" ), context );
310
+ // WHEN
311
+ requestHandler .handleRequest (Collections .singletonList ("ListOfOneElement" ), context );
293
312
294
- File logFile = new File ( "target/logfile.json" );
295
- assertThat ( contentOf ( logFile )). contains ( "[ \" ListOfOneElement \" ] " );
296
- }
313
+ // THEN
314
+ File logFile = new File ( "target/logfile.json " );
315
+ assertThat ( contentOf ( logFile )). contains ( "[ \" ListOfOneElement \" ]" );
297
316
}
298
317
299
318
@ Test
300
319
void shouldLogEventForStreamHandler () throws IOException {
320
+ // GIVEN
301
321
requestStreamHandler = new PowertoolsLogEventForStream ();
302
322
ByteArrayOutputStream output = new ByteArrayOutputStream ();
323
+
324
+ // WHEN
303
325
requestStreamHandler .handleRequest (new ByteArrayInputStream (new ObjectMapper ().writeValueAsBytes (Collections .singletonMap ("key" , "value" ))), output , context );
304
326
327
+ // THEN
305
328
assertThat (new String (output .toByteArray (), StandardCharsets .UTF_8 ))
306
329
.isNotEmpty ();
307
330
@@ -312,9 +335,13 @@ void shouldLogEventForStreamHandler() throws IOException {
312
335
@ ParameterizedTest
313
336
@ Event (value = "apiGatewayProxyEventV1.json" , type = APIGatewayProxyRequestEvent .class )
314
337
void shouldLogCorrelationIdOnAPIGatewayProxyRequestEvent (APIGatewayProxyRequestEvent event ) {
338
+ // GIVEN
315
339
RequestHandler <APIGatewayProxyRequestEvent , Object > handler = new PowertoolsLogApiGatewayRestApiCorrelationId ();
340
+
341
+ // WHEN
316
342
handler .handleRequest (event , context );
317
343
344
+ // THEN
318
345
assertThat (MDC .getCopyOfContextMap ())
319
346
.hasSize (EXPECTED_CONTEXT_SIZE + 1 )
320
347
.containsEntry ("correlation_id" , event .getRequestContext ().getRequestId ());
@@ -323,9 +350,13 @@ void shouldLogCorrelationIdOnAPIGatewayProxyRequestEvent(APIGatewayProxyRequestE
323
350
@ ParameterizedTest
324
351
@ Event (value = "apiGatewayProxyEventV2.json" , type = APIGatewayV2HTTPEvent .class )
325
352
void shouldLogCorrelationIdOnAPIGatewayV2HTTPEvent (APIGatewayV2HTTPEvent event ) {
353
+ // GIVEN
326
354
RequestHandler <APIGatewayV2HTTPEvent , Object > handler = new PowertoolsLogApiGatewayHttpApiCorrelationId ();
355
+
356
+ // WHEN
327
357
handler .handleRequest (event , context );
328
358
359
+ // THEN
329
360
assertThat (MDC .getCopyOfContextMap ())
330
361
.hasSize (EXPECTED_CONTEXT_SIZE + 1 )
331
362
.containsEntry ("correlation_id" , event .getRequestContext ().getRequestId ());
@@ -334,35 +365,47 @@ void shouldLogCorrelationIdOnAPIGatewayV2HTTPEvent(APIGatewayV2HTTPEvent event)
334
365
@ ParameterizedTest
335
366
@ Event (value = "albEvent.json" , type = ApplicationLoadBalancerRequestEvent .class )
336
367
void shouldLogCorrelationIdOnALBEvent (ApplicationLoadBalancerRequestEvent event ) {
368
+ // GIVEN
337
369
RequestHandler <ApplicationLoadBalancerRequestEvent , Object > handler = new PowertoolsLogAlbCorrelationId ();
370
+
371
+ // WHEN
338
372
handler .handleRequest (event , context );
339
373
374
+ // THEN
340
375
assertThat (MDC .getCopyOfContextMap ())
341
376
.hasSize (EXPECTED_CONTEXT_SIZE + 1 )
342
377
.containsEntry ("correlation_id" , event .getHeaders ().get ("x-amzn-trace-id" ));
343
378
}
344
379
345
380
@ Test
346
381
void shouldLogCorrelationIdOnStreamHandler () throws IOException {
382
+ // GIVEN
347
383
RequestStreamHandler handler = new PowertoolsLogEventBridgeCorrelationId ();
348
384
String eventId = "3" ;
349
385
String event = "{\" id\" :" + eventId + "}" ; // CorrelationIdPath.EVENT_BRIDGE
350
386
ByteArrayInputStream inputStream = new ByteArrayInputStream (event .getBytes ());
387
+
388
+ // WHEN
351
389
handler .handleRequest (inputStream , new ByteArrayOutputStream (), context );
352
390
391
+ // THEN
353
392
assertThat (MDC .getCopyOfContextMap ())
354
393
.hasSize (EXPECTED_CONTEXT_SIZE + 1 )
355
394
.containsEntry ("correlation_id" , eventId );
356
395
}
357
396
358
397
@ Test
359
398
void shouldLogCorrelationIdOnAppSyncEvent () throws IOException {
399
+ // GIVEN
360
400
RequestStreamHandler handler = new PowertoolsLogAppSyncCorrelationId ();
361
401
String eventId = "456" ;
362
402
String event = "{\" request\" :{\" headers\" :{\" x-amzn-trace-id\" :" + eventId + "}}}" ; // CorrelationIdPath.APPSYNC_RESOLVER
363
403
ByteArrayInputStream inputStream = new ByteArrayInputStream (event .getBytes ());
404
+
405
+ // WHEN
364
406
handler .handleRequest (inputStream , new ByteArrayOutputStream (), context );
365
407
408
+ // THEN
366
409
assertThat (MDC .getCopyOfContextMap ())
367
410
.hasSize (EXPECTED_CONTEXT_SIZE + 1 )
368
411
.containsEntry ("correlation_id" , eventId );
0 commit comments