1818import static org .apache .commons .lang3 .reflect .FieldUtils .writeStaticField ;
1919import static org .assertj .core .api .Assertions .assertThat ;
2020import static org .assertj .core .api .Assertions .contentOf ;
21- import static org .mockito .Mockito .when ;
22- import static org .mockito .MockitoAnnotations .openMocks ;
2321import static software .amazon .lambda .powertools .logging .internal .PowertoolsLoggedFields .FUNCTION_ARN ;
2422import static software .amazon .lambda .powertools .logging .internal .PowertoolsLoggedFields .FUNCTION_COLD_START ;
2523import static software .amazon .lambda .powertools .logging .internal .PowertoolsLoggedFields .FUNCTION_MEMORY_SIZE ;
5452import org .junitpioneer .jupiter .ClearEnvironmentVariable ;
5553import org .junitpioneer .jupiter .SetEnvironmentVariable ;
5654import org .junitpioneer .jupiter .SetSystemProperty ;
57- import org .mockito .Mock ;
5855import org .slf4j .Logger ;
5956import org .slf4j .LoggerFactory ;
6057import org .slf4j .MDC ;
7269import com .fasterxml .jackson .databind .ObjectMapper ;
7370
7471import software .amazon .lambda .powertools .common .internal .LambdaHandlerProcessor ;
72+ import software .amazon .lambda .powertools .common .stubs .TestLambdaContext ;
7573import software .amazon .lambda .powertools .logging .argument .StructuredArgument ;
7674import software .amazon .lambda .powertools .logging .handlers .PowertoolsLogAlbCorrelationId ;
7775import software .amazon .lambda .powertools .logging .handlers .PowertoolsLogApiGatewayHttpApiCorrelationId ;
@@ -99,15 +97,13 @@ class LambdaLoggingAspectTest {
9997 private RequestStreamHandler requestStreamHandler ;
10098 private RequestHandler <Object , Object > requestHandler ;
10199
102- @ Mock
103100 private Context context ;
104101
105102 @ BeforeEach
106103 void setUp () throws IllegalAccessException , NoSuchMethodException , InvocationTargetException , IOException {
107- openMocks (this );
108104 MDC .clear ();
109105 writeStaticField (LambdaHandlerProcessor .class , "IS_COLD_START" , null , true );
110- setupContext ();
106+ context = new TestLambdaContext ();
111107 requestHandler = new PowertoolsLogEnabled ();
112108 requestStreamHandler = new PowertoolsLogEnabledForStream ();
113109 resetLogLevel (Level .INFO );
@@ -124,7 +120,7 @@ void setUp() throws IllegalAccessException, NoSuchMethodException, InvocationTar
124120
125121 @ AfterEach
126122 void cleanUp () throws IOException {
127- //Make sure file is cleaned up before running full stack logging regression
123+ // Make sure file is cleaned up before running full stack logging regression
128124 FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
129125 }
130126
@@ -224,7 +220,8 @@ void shouldLogWarnWhenPowertoolsLevelEnvVarIsWarnAndLambdaLevelVarIsInfo() throw
224220 assertThat (LOG .isInfoEnabled ()).isFalse ();
225221 assertThat (LOG .isWarnEnabled ()).isTrue ();
226222 File logFile = new File ("target/logfile.json" );
227- assertThat (contentOf (logFile )).doesNotContain (" does not match AWS Lambda Advanced Logging Controls minimum log level" );
223+ assertThat (contentOf (logFile ))
224+ .doesNotContain (" does not match AWS Lambda Advanced Logging Controls minimum log level" );
228225 }
229226
230227 @ Test
@@ -241,7 +238,8 @@ void shouldLogInfoWhenPowertoolsLevelEnvVarIsInfoAndLambdaLevelVarIsWarn() throw
241238 assertThat (LOG .isInfoEnabled ()).isTrue ();
242239 File logFile = new File ("target/logfile.json" );
243240 // should log a warning as powertools level is lower than lambda level
244- assertThat (contentOf (logFile )).contains ("Current log level (INFO) does not match AWS Lambda Advanced Logging Controls minimum log level (WARN). This can lead to data loss, consider adjusting them." );
241+ assertThat (contentOf (logFile )).contains (
242+ "Current log level (INFO) does not match AWS Lambda Advanced Logging Controls minimum log level (WARN). This can lead to data loss, consider adjusting them." );
245243 }
246244
247245 @ Test
@@ -265,11 +263,11 @@ void shouldSetLambdaContextWhenEnabled() {
265263
266264 assertThat (MDC .getCopyOfContextMap ())
267265 .hasSize (EXPECTED_CONTEXT_SIZE )
268- .containsEntry (FUNCTION_ARN .getName (), "testArn " )
269- .containsEntry (FUNCTION_MEMORY_SIZE .getName (), "10 " )
266+ .containsEntry (FUNCTION_ARN .getName (), "arn:aws:lambda:us-east-1:123456789012:function:test " )
267+ .containsEntry (FUNCTION_MEMORY_SIZE .getName (), "128 " )
270268 .containsEntry (FUNCTION_VERSION .getName (), "1" )
271- .containsEntry (FUNCTION_NAME .getName (), "testFunction " )
272- .containsEntry (FUNCTION_REQUEST_ID .getName (), "RequestId " )
269+ .containsEntry (FUNCTION_NAME .getName (), "test-function " )
270+ .containsEntry (FUNCTION_REQUEST_ID .getName (), "test-request-id " )
273271 .containsKey (FUNCTION_COLD_START .getName ())
274272 .containsKey (SERVICE .getName ());
275273 }
@@ -278,30 +276,30 @@ void shouldSetLambdaContextWhenEnabled() {
278276 void shouldSetLambdaContextForStreamHandlerWhenEnabled () throws IOException {
279277 requestStreamHandler = new PowertoolsLogEnabledForStream ();
280278
281- requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte []{}), new ByteArrayOutputStream (),
279+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte [] {}), new ByteArrayOutputStream (),
282280 context );
283281
284282 assertThat (MDC .getCopyOfContextMap ())
285283 .hasSize (EXPECTED_CONTEXT_SIZE )
286- .containsEntry (FUNCTION_ARN .getName (), "testArn " )
287- .containsEntry (FUNCTION_MEMORY_SIZE .getName (), "10 " )
284+ .containsEntry (FUNCTION_ARN .getName (), "arn:aws:lambda:us-east-1:123456789012:function:test " )
285+ .containsEntry (FUNCTION_MEMORY_SIZE .getName (), "128 " )
288286 .containsEntry (FUNCTION_VERSION .getName (), "1" )
289- .containsEntry (FUNCTION_NAME .getName (), "testFunction " )
290- .containsEntry (FUNCTION_REQUEST_ID .getName (), "RequestId " )
287+ .containsEntry (FUNCTION_NAME .getName (), "test-function " )
288+ .containsEntry (FUNCTION_REQUEST_ID .getName (), "test-request-id " )
291289 .containsKey (FUNCTION_COLD_START .getName ())
292290 .containsKey (SERVICE .getName ());
293291 }
294292
295293 @ Test
296294 void shouldSetColdStartFlagOnFirstCallNotOnSecondCall () throws IOException {
297- requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte []{}), new ByteArrayOutputStream (),
295+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte [] {}), new ByteArrayOutputStream (),
298296 context );
299297
300298 assertThat (MDC .getCopyOfContextMap ())
301299 .hasSize (EXPECTED_CONTEXT_SIZE )
302300 .containsEntry (FUNCTION_COLD_START .getName (), "true" );
303301
304- requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte []{}), new ByteArrayOutputStream (),
302+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte [] {}), new ByteArrayOutputStream (),
305303 context );
306304
307305 assertThat (MDC .getCopyOfContextMap ())
@@ -346,7 +344,7 @@ void shouldLogDebugWhenSamplingEqualsOne() {
346344 }
347345
348346 @ Test
349- void shouldLogDebugWhenSamplingEnvVarEqualsOne () throws IllegalAccessException {
347+ void shouldLogDebugWhenSamplingEnvVarEqualsOne () {
350348 // GIVEN
351349 LoggingConstants .POWERTOOLS_SAMPLING_RATE = "1" ;
352350 PowertoolsLogEnabled handler = new PowertoolsLogEnabled ();
@@ -360,7 +358,7 @@ void shouldLogDebugWhenSamplingEnvVarEqualsOne() throws IllegalAccessException {
360358 }
361359
362360 @ Test
363- void shouldNotLogDebugWhenSamplingEnvVarIsTooBig () throws IllegalAccessException {
361+ void shouldNotLogDebugWhenSamplingEnvVarIsTooBig () {
364362 // GIVEN
365363 LoggingConstants .POWERTOOLS_SAMPLING_RATE = "42" ;
366364
@@ -378,7 +376,7 @@ void shouldNotLogDebugWhenSamplingEnvVarIsInvalid() {
378376 LoggingConstants .POWERTOOLS_SAMPLING_RATE = "NotANumber" ;
379377
380378 // WHEN
381- requestHandler .handleRequest (new Object (), context );
379+ requestHandler .handleRequest (new Object (), context );
382380
383381 // THEN
384382 File logFile = new File ("target/logfile.json" );
@@ -509,7 +507,7 @@ void shouldNotLogEventForHandlerWhenEnvVariableSetToFalse() {
509507 requestHandler .handleRequest (singletonList ("ListOfOneElement" ), context );
510508
511509 // THEN
512- TestLogger logger = (TestLogger ) ((PowertoolsLogEventEnvVar )requestHandler ).getLogger ();
510+ TestLogger logger = (TestLogger ) ((PowertoolsLogEventEnvVar ) requestHandler ).getLogger ();
513511 assertThat (logger .getArguments ()).isNull ();
514512 }
515513
@@ -521,7 +519,8 @@ void shouldLogEventForStreamHandler() throws IOException {
521519 Map <String , String > map = Collections .singletonMap ("key" , "value" );
522520
523521 // WHEN
524- requestStreamHandler .handleRequest (new ByteArrayInputStream (new ObjectMapper ().writeValueAsBytes (map )), output , context );
522+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new ObjectMapper ().writeValueAsBytes (map )), output ,
523+ context );
525524
526525 // THEN
527526 assertThat (new String (output .toByteArray (), StandardCharsets .UTF_8 ))
@@ -586,7 +585,8 @@ void shouldLogResponseForStreamHandler() throws IOException {
586585 String input = "<user><firstName>Bob</firstName><lastName>The Sponge</lastName></user>" ;
587586
588587 // WHEN
589- requestStreamHandler .handleRequest (new ByteArrayInputStream (input .getBytes (StandardCharsets .UTF_8 )), output , context );
588+ requestStreamHandler .handleRequest (new ByteArrayInputStream (input .getBytes (StandardCharsets .UTF_8 )), output ,
589+ context );
590590
591591 // THEN
592592 assertThat (new String (output .toByteArray (), StandardCharsets .UTF_8 ))
@@ -737,7 +737,8 @@ void testMultipleLoggingManagers_shouldWarnAndSelectFirstOne() throws Unsupporte
737737 String output = outputStream .toString ("UTF-8" );
738738 assertThat (output )
739739 .contains ("WARN. Multiple LoggingManagers were found on the classpath" )
740- .contains ("WARN. Make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies" )
740+ .contains (
741+ "WARN. Make sure to have only one of powertools-logging-log4j OR powertools-logging-logback to your dependencies" )
741742 .contains ("WARN. Using the first LoggingManager found on the classpath: [" + list .get (0 ) + "]" );
742743 }
743744
@@ -757,19 +758,12 @@ void testNoLoggingManagers_shouldWarnAndCreateDefault() throws UnsupportedEncodi
757758 assertThat (output )
758759 .contains ("ERROR. No LoggingManager was found on the classpath" )
759760 .contains ("ERROR. Applying default LoggingManager: POWERTOOLS_LOG_LEVEL variable is ignored" )
760- .contains ("ERROR. Make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies" );
761+ .contains (
762+ "ERROR. Make sure to add either powertools-logging-log4j or powertools-logging-logback to your dependencies" );
761763
762764 assertThat (loggingManager ).isExactlyInstanceOf (DefautlLoggingManager .class );
763765 }
764766
765- private void setupContext () {
766- when (context .getFunctionName ()).thenReturn ("testFunction" );
767- when (context .getInvokedFunctionArn ()).thenReturn ("testArn" );
768- when (context .getFunctionVersion ()).thenReturn ("1" );
769- when (context .getMemoryLimitInMB ()).thenReturn (10 );
770- when (context .getAwsRequestId ()).thenReturn ("RequestId" );
771- }
772-
773767 private void resetLogLevel (Level level )
774768 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException {
775769 Method setLogLevels = LambdaLoggingAspect .class .getDeclaredMethod ("setLogLevels" , Level .class );
0 commit comments