2626import java .util .stream .Stream ;
2727
2828import org .junit .jupiter .api .AfterAll ;
29- import org .junit .jupiter .api .BeforeAll ;
30- import org .junit .jupiter .api .Test ;
29+ import org .junit .jupiter .api .TestInstance ;
3130import org .junit .jupiter .api .Timeout ;
31+ import org .junit .jupiter .params .ParameterizedTest ;
32+ import org .junit .jupiter .params .provider .ValueSource ;
3233
3334import com .fasterxml .jackson .core .JsonProcessingException ;
3435import com .fasterxml .jackson .databind .JsonNode ;
3738import software .amazon .lambda .powertools .testutils .Infrastructure ;
3839import software .amazon .lambda .powertools .testutils .lambda .InvocationResult ;
3940
41+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
4042class LoggingE2ET {
4143
4244 private static final ObjectMapper objectMapper = new ObjectMapper ();
4345
44- private static Infrastructure infrastructure ;
45- private static String functionName ;
46+ private Infrastructure infrastructure ;
47+ private String functionName ;
4648
47- @ BeforeAll
48- @ Timeout (value = 10 , unit = TimeUnit .MINUTES )
49- static void setup () {
49+ private void setupInfrastructure (String pathToFunction ) {
5050 infrastructure = Infrastructure .builder ()
51- .testName (LoggingE2ET .class .getSimpleName ())
51+ .testName (LoggingE2ET .class .getSimpleName () + "-" + pathToFunction )
5252 .tracing (true )
53- .pathToFunction ("logging" )
53+ .pathToFunction (pathToFunction )
5454 .environmentVariables (
5555 Stream .of (new String [][] {
5656 { "POWERTOOLS_LOG_LEVEL" , "INFO" },
@@ -63,37 +63,50 @@ static void setup() {
6363 }
6464
6565 @ AfterAll
66- static void tearDown () {
66+ void tearDown () {
6767 if (infrastructure != null ) {
6868 infrastructure .destroy ();
6969 }
7070 }
7171
72- @ Test
73- void test_logInfoWithAdditionalKeys () throws JsonProcessingException {
74- // GIVEN
75- String orderId = UUID .randomUUID ().toString ();
76- String event = "{\" message\" :\" New Order\" , \" keys\" :{\" orderId\" :\" " + orderId + "\" }}" ;
77-
78- // WHEN
79- InvocationResult invocationResult1 = invokeFunction (functionName , event );
80- InvocationResult invocationResult2 = invokeFunction (functionName , event );
81-
82- // THEN
83- String [] functionLogs = invocationResult1 .getLogs ().getFunctionLogs (INFO );
84- assertThat (functionLogs ).hasSize (1 );
85-
86- JsonNode jsonNode = objectMapper .readTree (functionLogs [0 ]);
87- assertThat (jsonNode .get ("message" ).asText ()).isEqualTo ("New Order" );
88- assertThat (jsonNode .get ("orderId" ).asText ()).isEqualTo (orderId );
89- assertThat (jsonNode .get ("cold_start" ).asBoolean ()).isTrue ();
90- assertThat (jsonNode .get ("xray_trace_id" ).asText ()).isNotBlank ();
91- assertThat (jsonNode .get ("function_request_id" ).asText ()).isEqualTo (invocationResult1 .getRequestId ());
92-
93- // second call should not be cold start
94- functionLogs = invocationResult2 .getLogs ().getFunctionLogs (INFO );
95- assertThat (functionLogs ).hasSize (1 );
96- jsonNode = objectMapper .readTree (functionLogs [0 ]);
97- assertThat (jsonNode .get ("cold_start" ).asBoolean ()).isFalse ();
72+ @ ParameterizedTest
73+ @ ValueSource (strings = { "logging" })
74+ @ Timeout (value = 15 , unit = TimeUnit .MINUTES )
75+ void test_logInfoWithAdditionalKeys (String pathToFunction ) throws JsonProcessingException {
76+ setupInfrastructure (pathToFunction );
77+
78+ try {
79+ // GIVEN
80+ String orderId = UUID .randomUUID ().toString ();
81+ String event = "{\" message\" :\" New Order\" , \" keys\" :{\" orderId\" :\" " + orderId + "\" }}" ;
82+
83+ // WHEN
84+ InvocationResult invocationResult1 = invokeFunction (functionName , event );
85+ InvocationResult invocationResult2 = invokeFunction (functionName , event );
86+
87+ // THEN
88+ String [] functionLogs = invocationResult1 .getLogs ().getFunctionLogs (INFO );
89+ assertThat (functionLogs ).hasSize (1 );
90+
91+ JsonNode jsonNode = objectMapper .readTree (functionLogs [0 ]);
92+ assertThat (jsonNode .get ("message" ).asText ()).isEqualTo ("New Order" );
93+ assertThat (jsonNode .get ("orderId" ).asText ()).isEqualTo (orderId );
94+ assertThat (jsonNode .get ("cold_start" ).asBoolean ()).isTrue ();
95+ assertThat (jsonNode .get ("xray_trace_id" ).asText ()).isNotBlank ();
96+ assertThat (jsonNode .get ("function_request_id" ).asText ()).isEqualTo (invocationResult1 .getRequestId ());
97+
98+ // second call should not be cold start
99+ functionLogs = invocationResult2 .getLogs ().getFunctionLogs (INFO );
100+ assertThat (functionLogs ).hasSize (1 );
101+ jsonNode = objectMapper .readTree (functionLogs [0 ]);
102+ assertThat (jsonNode .get ("cold_start" ).asBoolean ()).isFalse ();
103+
104+ } finally {
105+ // Clean up infrastructure after each parameter
106+ if (infrastructure != null ) {
107+ infrastructure .destroy ();
108+ infrastructure = null ;
109+ }
110+ }
98111 }
99112}
0 commit comments