14
14
15
15
package software .amazon .lambda .powertools .logging .internal ;
16
16
17
- import static java .util .Collections .emptyMap ;
18
- import static java .util .Collections .singletonList ;
19
- import static java .util .stream .Collectors .joining ;
20
- import static org .apache .commons .lang3 .reflect .FieldUtils .writeStaticField ;
21
- import static org .assertj .core .api .Assertions .assertThat ;
22
- import static org .assertj .core .api .Assertions .fail ;
23
- import static org .mockito .Mockito .mockStatic ;
24
- import static org .mockito .Mockito .when ;
25
- import static org .mockito .MockitoAnnotations .openMocks ;
26
- import static org .skyscreamer .jsonassert .JSONAssert .assertEquals ;
27
- import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getProperty ;
28
- import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getenv ;
29
-
30
17
import com .amazonaws .services .lambda .runtime .Context ;
31
18
import com .amazonaws .services .lambda .runtime .RequestHandler ;
32
19
import com .amazonaws .services .lambda .runtime .RequestStreamHandler ;
37
24
import com .amazonaws .services .lambda .runtime .tests .annotations .Event ;
38
25
import com .fasterxml .jackson .core .JsonProcessingException ;
39
26
import com .fasterxml .jackson .databind .ObjectMapper ;
40
- import java .io .BufferedReader ;
41
- import java .io .ByteArrayInputStream ;
42
- import java .io .ByteArrayOutputStream ;
43
- import java .io .IOException ;
44
- import java .io .InputStreamReader ;
45
- import java .lang .reflect .InvocationTargetException ;
46
- import java .lang .reflect .Method ;
47
- import java .nio .channels .FileChannel ;
48
- import java .nio .charset .StandardCharsets ;
49
- import java .nio .file .Files ;
50
- import java .nio .file .Paths ;
51
- import java .nio .file .StandardOpenOption ;
52
- import java .util .List ;
53
- import java .util .Map ;
54
- import java .util .stream .Collectors ;
55
27
import org .apache .logging .log4j .Level ;
56
28
import org .apache .logging .log4j .ThreadContext ;
57
29
import org .json .JSONException ;
30
+ import org .junit .jupiter .api .Assertions ;
58
31
import org .junit .jupiter .api .BeforeEach ;
59
32
import org .junit .jupiter .api .Test ;
60
33
import org .junit .jupiter .params .ParameterizedTest ;
61
34
import org .mockito .Mock ;
62
35
import org .mockito .MockedStatic ;
63
36
import software .amazon .lambda .powertools .core .internal .LambdaHandlerProcessor ;
64
37
import software .amazon .lambda .powertools .core .internal .SystemWrapper ;
65
- import software .amazon .lambda .powertools .logging .handlers .PowerLogToolApiGatewayHttpApiCorrelationId ;
66
- import software .amazon .lambda .powertools .logging .handlers .PowerLogToolApiGatewayRestApiCorrelationId ;
67
- import software .amazon .lambda .powertools .logging .handlers .PowerLogToolEnabled ;
68
- import software .amazon .lambda .powertools .logging .handlers .PowerLogToolEnabledForStream ;
69
- import software .amazon .lambda .powertools .logging .handlers .PowerToolDisabled ;
70
- import software .amazon .lambda .powertools .logging .handlers .PowerToolDisabledForStream ;
71
- import software .amazon .lambda .powertools .logging .handlers .PowerToolLogEventEnabled ;
72
- import software .amazon .lambda .powertools .logging .handlers .PowerToolLogEventEnabledForStream ;
73
- import software .amazon .lambda .powertools .logging .handlers .PowerToolLogEventEnabledWithCustomMapper ;
74
- import software .amazon .lambda .powertools .logging .handlers .PowertoolsLogAlbCorrelationId ;
75
- import software .amazon .lambda .powertools .logging .handlers .PowertoolsLogEnabledWithClearState ;
76
- import software .amazon .lambda .powertools .logging .handlers .PowertoolsLogEventBridgeCorrelationId ;
38
+ import software .amazon .lambda .powertools .logging .handlers .*;
39
+
40
+ import java .io .*;
41
+ import java .lang .reflect .InvocationTargetException ;
42
+ import java .lang .reflect .Method ;
43
+ import java .nio .channels .FileChannel ;
44
+ import java .nio .charset .StandardCharsets ;
45
+ import java .nio .file .Files ;
46
+ import java .nio .file .Paths ;
47
+ import java .nio .file .StandardOpenOption ;
48
+ import java .util .List ;
49
+ import java .util .Map ;
50
+ import java .util .stream .Collectors ;
51
+
52
+ import static java .util .Collections .emptyMap ;
53
+ import static java .util .Collections .singletonList ;
54
+ import static java .util .stream .Collectors .joining ;
55
+ import static org .apache .commons .lang3 .reflect .FieldUtils .writeStaticField ;
56
+ import static org .assertj .core .api .Assertions .assertThat ;
57
+ import static org .assertj .core .api .Assertions .fail ;
58
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
59
+ import static org .mockito .Mockito .mockStatic ;
60
+ import static org .mockito .Mockito .when ;
61
+ import static org .mockito .MockitoAnnotations .openMocks ;
62
+ import static org .skyscreamer .jsonassert .JSONAssert .assertEquals ;
63
+ import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getProperty ;
64
+ import static software .amazon .lambda .powertools .core .internal .SystemWrapper .getenv ;
77
65
78
66
class LambdaLoggingAspectTest {
79
67
@@ -116,7 +104,7 @@ void shouldSetLambdaContextWhenEnabled() {
116
104
void shouldSetLambdaContextForStreamHandlerWhenEnabled () throws IOException {
117
105
requestStreamHandler = new PowerLogToolEnabledForStream ();
118
106
119
- requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte [] {}), new ByteArrayOutputStream (),
107
+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte []{}), new ByteArrayOutputStream (),
120
108
context );
121
109
122
110
assertThat (ThreadContext .getImmutableContext ())
@@ -132,14 +120,14 @@ void shouldSetLambdaContextForStreamHandlerWhenEnabled() throws IOException {
132
120
133
121
@ Test
134
122
void shouldSetColdStartFlag () throws IOException {
135
- requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte [] {}), new ByteArrayOutputStream (),
123
+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte []{}), new ByteArrayOutputStream (),
136
124
context );
137
125
138
126
assertThat (ThreadContext .getImmutableContext ())
139
127
.hasSize (EXPECTED_CONTEXT_SIZE )
140
128
.containsEntry ("coldStart" , "true" );
141
129
142
- requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte [] {}), new ByteArrayOutputStream (),
130
+ requestStreamHandler .handleRequest (new ByteArrayInputStream (new byte []{}), new ByteArrayOutputStream (),
143
131
context );
144
132
145
133
assertThat (ThreadContext .getImmutableContext ())
@@ -195,6 +183,51 @@ void shouldLogEventForHandler() throws IOException, JSONException {
195
183
assertEquals (expectEvent , event , false );
196
184
}
197
185
186
+ /**
187
+ * If POWERTOOLS_LOGGER_LOG_EVENT was set to true, the handler should log, despite @Logging(logEvent=false)
188
+ *
189
+ * @throws IOException
190
+ */
191
+ @ Test
192
+ void shouldLogEventForHandlerWhenEnvVariableSetToTrue () throws IOException , IllegalAccessException , JSONException {
193
+ try {
194
+ writeStaticField (LambdaLoggingAspect .class , "LOG_EVENT" , Boolean .TRUE , true );
195
+
196
+ requestHandler = new PowerToolLogEventDisabled ();
197
+ S3EventNotification s3EventNotification = s3EventNotification ();
198
+
199
+ requestHandler .handleRequest (s3EventNotification , context );
200
+
201
+ Map <String , Object > log = parseToMap (Files .lines (Paths .get ("target/logfile.json" )).collect (joining ()));
202
+
203
+ String event = (String ) log .get ("message" );
204
+
205
+ String expectEvent = new BufferedReader (
206
+ new InputStreamReader (this .getClass ().getResourceAsStream ("/s3EventNotification.json" )))
207
+ .lines ().collect (joining ("\n " ));
208
+
209
+ assertEquals (expectEvent , event , false );
210
+ } finally {
211
+ writeStaticField (LambdaLoggingAspect .class , "LOG_EVENT" , Boolean .FALSE , true );
212
+ }
213
+ }
214
+
215
+ /**
216
+ * If POWERTOOLS_LOGGER_LOG_EVENT was set to false and @Logging(logEvent=false), the handler shouldn't log
217
+ *
218
+ * @throws IOException
219
+ */
220
+ @ Test
221
+ void shouldNotLogEventForHandlerWhenEnvVariableSetToFalse () throws IOException {
222
+ requestHandler = new PowerToolLogEventDisabled ();
223
+ S3EventNotification s3EventNotification = s3EventNotification ();
224
+
225
+ requestHandler .handleRequest (s3EventNotification , context );
226
+
227
+ Assertions .assertEquals (0 ,
228
+ Files .lines (Paths .get ("target/logfile.json" )).collect (joining ()).length ());
229
+ }
230
+
198
231
@ Test
199
232
void shouldLogEventForHandlerWithOverriddenObjectMapper () throws IOException , JSONException {
200
233
RequestHandler <S3EventNotification , Object > handler = new PowerToolLogEventEnabledWithCustomMapper ();
0 commit comments