1616
1717import static org .assertj .core .api .Assertions .assertThat ;
1818import static org .assertj .core .api .Assertions .contentOf ;
19- import static org .mockito .Mockito .when ;
20- import static org .mockito .MockitoAnnotations .openMocks ;
2119
22- import com .amazonaws .services .lambda .runtime .Context ;
23- import com .amazonaws .services .lambda .runtime .events .SQSEvent ;
2420import java .io .File ;
2521import java .io .IOException ;
2622import java .nio .channels .FileChannel ;
2925import java .nio .file .StandardOpenOption ;
3026import java .util .Arrays ;
3127import java .util .Collections ;
28+
3229import org .junit .jupiter .api .AfterEach ;
3330import org .junit .jupiter .api .BeforeEach ;
3431import org .junit .jupiter .api .Order ;
3532import org .junit .jupiter .api .Test ;
36- import org .mockito .Mock ;
3733import org .slf4j .MDC ;
3834
35+ import com .amazonaws .services .lambda .runtime .Context ;
36+ import com .amazonaws .services .lambda .runtime .events .SQSEvent ;
37+
38+ import software .amazon .lambda .powertools .common .stubs .TestLambdaContext ;
3939import software .amazon .lambda .powertools .logging .internal .PowertoolsLoggedFields ;
4040import software .amazon .lambda .powertools .logging .internal .handler .PowertoolsArguments ;
4141
4242@ Order (2 )
4343class PowertoolsResolverArgumentsTest {
4444
45- @ Mock
4645 private Context context ;
4746
4847 @ BeforeEach
4948 void setUp () throws IOException {
50- openMocks (this );
5149 MDC .clear ();
52- setupContext ();
50+ context = new TestLambdaContext ();
5351
5452 try {
5553 FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
@@ -61,7 +59,7 @@ void setUp() throws IOException {
6159
6260 @ AfterEach
6361 void cleanUp () throws IOException {
64- //Make sure file is cleaned up before running full stack logging regression
62+ // Make sure file is cleaned up before running full stack logging regression
6563 FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
6664 FileChannel .open (Paths .get ("target/ecslogfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
6765 }
@@ -74,7 +72,7 @@ void shouldLogArgumentsAsJsonWhenUsingRawJson() {
7472 msg .setMessageId ("1212abcd" );
7573 msg .setBody ("plop" );
7674 msg .setEventSource ("eb" );
77- msg .setAwsRegion ("eu-west -1" );
75+ msg .setAwsRegion ("us-east -1" );
7876 SQSEvent .MessageAttribute attribute = new SQSEvent .MessageAttribute ();
7977 attribute .setStringListValues (Arrays .asList ("val1" , "val2" , "val3" ));
8078 msg .setMessageAttributes (Collections .singletonMap ("keyAttribute" , attribute ));
@@ -86,7 +84,7 @@ void shouldLogArgumentsAsJsonWhenUsingRawJson() {
8684 File logFile = new File ("target/logfile.json" );
8785 assertThat (contentOf (logFile ))
8886 .contains (
89- "\" input\" :{\" awsRegion\" :\" eu-west -1\" ,\" body\" :\" plop\" ,\" eventSource\" :\" eb\" ,\" messageAttributes\" :{\" keyAttribute\" :{\" stringListValues\" :[\" val1\" ,\" val2\" ,\" val3\" ]}},\" messageId\" :\" 1212abcd\" }" )
87+ "\" input\" :{\" awsRegion\" :\" us-east -1\" ,\" body\" :\" plop\" ,\" eventSource\" :\" eb\" ,\" messageAttributes\" :{\" keyAttribute\" :{\" stringListValues\" :[\" val1\" ,\" val2\" ,\" val3\" ]}},\" messageId\" :\" 1212abcd\" }" )
9088 .contains ("\" message\" :\" 1212abcd\" " )
9189 .contains ("\" message\" :\" Message body = plop and id = \\ \" 1212abcd\\ \" \" " );
9290 // Reserved keys should be ignored
@@ -106,7 +104,7 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
106104 msg .setMessageId ("1212abcd" );
107105 msg .setBody ("plop" );
108106 msg .setEventSource ("eb" );
109- msg .setAwsRegion ("eu-west -1" );
107+ msg .setAwsRegion ("us-east -1" );
110108 SQSEvent .MessageAttribute attribute = new SQSEvent .MessageAttribute ();
111109 attribute .setStringListValues (Arrays .asList ("val1" , "val2" , "val3" ));
112110 msg .setMessageAttributes (Collections .singletonMap ("keyAttribute" , attribute ));
@@ -118,7 +116,7 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
118116 File logFile = new File ("target/logfile.json" );
119117 assertThat (contentOf (logFile ))
120118 .contains (
121- "\" input\" :{\" awsRegion\" :\" eu-west -1\" ,\" body\" :\" plop\" ,\" eventSource\" :\" eb\" ,\" messageAttributes\" :{\" keyAttribute\" :{\" stringListValues\" :[\" val1\" ,\" val2\" ,\" val3\" ]}},\" messageId\" :\" 1212abcd\" }" )
119+ "\" input\" :{\" awsRegion\" :\" us-east -1\" ,\" body\" :\" plop\" ,\" eventSource\" :\" eb\" ,\" messageAttributes\" :{\" keyAttribute\" :{\" stringListValues\" :[\" val1\" ,\" val2\" ,\" val3\" ]}},\" messageId\" :\" 1212abcd\" }" )
122120 .contains ("\" message\" :\" 1212abcd\" " )
123121 .contains ("\" message\" :\" Message body = plop and id = \\ \" 1212abcd\\ \" \" " );
124122
@@ -131,11 +129,4 @@ void shouldLogArgumentsAsJsonWhenUsingKeyValue() {
131129 });
132130 }
133131
134- private void setupContext () {
135- when (context .getFunctionName ()).thenReturn ("testFunction" );
136- when (context .getInvokedFunctionArn ()).thenReturn ("testArn" );
137- when (context .getFunctionVersion ()).thenReturn ("1" );
138- when (context .getMemoryLimitInMB ()).thenReturn (10 );
139- when (context .getAwsRequestId ()).thenReturn ("RequestId" );
140- }
141132}
0 commit comments