46
46
import java .lang .reflect .Method ;
47
47
import java .nio .channels .FileChannel ;
48
48
import java .nio .charset .StandardCharsets ;
49
+ import java .nio .file .NoSuchFileException ;
49
50
import java .nio .file .Paths ;
50
51
import java .nio .file .StandardOpenOption ;
51
52
import java .util .Collections ;
52
53
import org .junit .jupiter .api .AfterEach ;
53
54
import org .junit .jupiter .api .BeforeEach ;
54
55
import org .junit .jupiter .api .Test ;
55
56
import org .junit .jupiter .params .ParameterizedTest ;
56
- import org .junitpioneer .jupiter .ClearEnvironmentVariable ;
57
- import org .junitpioneer .jupiter .SetEnvironmentVariable ;
58
57
import org .mockito .Mock ;
59
58
import org .mockito .MockedStatic ;
60
59
import org .slf4j .MDC ;
@@ -86,15 +85,19 @@ class LambdaLoggingAspectTest {
86
85
private Context context ;
87
86
88
87
@ BeforeEach
89
- @ ClearEnvironmentVariable (key = "POWERTOOLS_LOGGER_SAMPLE_RATE" )
90
- void setUp () throws IllegalAccessException , IOException , NoSuchMethodException , InvocationTargetException {
88
+ void setUp () throws IllegalAccessException , NoSuchMethodException , InvocationTargetException , IOException {
91
89
openMocks (this );
92
90
MDC .clear ();
93
91
writeStaticField (LambdaHandlerProcessor .class , "IS_COLD_START" , null , true );
94
92
setupContext ();
95
93
requestHandler = new PowertoolsLogEnabled ();
96
94
requestStreamHandler = new PowertoolsLogEnabledForStream ();
97
95
resetLogLevel (Level .INFO );
96
+ try {
97
+ FileChannel .open (Paths .get ("target/logfile.json" ), StandardOpenOption .WRITE ).truncate (0 ).close ();
98
+ } catch (NoSuchFileException e ) {
99
+ // may not be there in the first run
100
+ }
98
101
}
99
102
100
103
@ AfterEach
@@ -188,29 +191,42 @@ void shouldLogDebugWhenSamplingEqualsOne() {
188
191
}
189
192
190
193
@ Test
191
- @ SetEnvironmentVariable (key = "POWERTOOLS_LOGGER_SAMPLE_RATE" , value = "1" )
192
194
void shouldLogDebugWhenSamplingEnvVarEqualsOne () {
193
- PowertoolsLogEnabled handler = new PowertoolsLogEnabled ();
194
- handler .handleRequest (new Object (), context );
195
- File logFile = new File ("target/logfile.json" );
196
- assertThat (contentOf (logFile )).contains ("Test debug event" );
195
+ try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
196
+ mocked .when (() -> getenv ("POWERTOOLS_LOGGER_SAMPLE_RATE" ))
197
+ .thenReturn ("1" );
198
+
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
+ }
197
204
}
198
205
199
206
@ Test
200
- @ SetEnvironmentVariable (key = "POWERTOOLS_LOGGER_SAMPLE_RATE" , value = "42" )
201
207
void shouldNotLogDebugWhenSamplingEnvVarIsTooBig () {
202
- requestHandler .handleRequest (new Object (), context );
203
- File logFile = new File ("target/logfile.json" );
204
- assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
208
+ try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
209
+ mocked .when (() -> getenv ("POWERTOOLS_LOGGER_SAMPLE_RATE" ))
210
+ .thenReturn ("42" );
211
+
212
+ requestHandler .handleRequest (new Object (), context );
213
+ File logFile = new File ("target/logfile.json" );
214
+ assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
215
+ }
205
216
}
206
217
207
218
@ Test
208
- @ SetEnvironmentVariable (key = "POWERTOOLS_LOGGER_SAMPLE_RATE" , value = "NotANumber" )
209
219
void shouldNotLogDebugWhenSamplingEnvVarIsInvalid () {
210
- requestHandler .handleRequest (new Object (), context );
211
- File logFile = new File ("target/logfile.json" );
212
- assertThat (contentOf (logFile )).doesNotContain ("Test debug event" );
213
- assertThat (contentOf (logFile )).contains ("Skipping sampling rate on environment variable configuration because of invalid value" );
220
+ try (MockedStatic <SystemWrapper > mocked = mockStatic (SystemWrapper .class )) {
221
+ mocked .when (() -> getenv ("POWERTOOLS_LOGGER_SAMPLE_RATE" ))
222
+ .thenReturn ("NotANumber" );
223
+
224
+ 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
+ }
214
230
}
215
231
216
232
@ Test
@@ -256,7 +272,7 @@ void shouldLogxRayTraceIdEnvVarSet() {
256
272
}
257
273
258
274
@ Test
259
- void shouldLogEventForHandler () throws IOException {
275
+ void shouldLogEventForHandlerWithLogEventAnnotation () {
260
276
requestHandler = new PowertoolsLogEvent ();
261
277
262
278
requestHandler .handleRequest (Collections .singletonList ("ListOfOneElement" ), context );
@@ -265,6 +281,21 @@ void shouldLogEventForHandler() throws IOException {
265
281
assertThat (contentOf (logFile )).contains ("[\" ListOfOneElement\" ]" );
266
282
}
267
283
284
+ @ Test
285
+ void shouldLogEventForHandlerWithLogEventEnvVar () {
286
+ requestHandler = new PowertoolsLogEnabled ();
287
+
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 );
293
+
294
+ File logFile = new File ("target/logfile.json" );
295
+ assertThat (contentOf (logFile )).contains ("[\" ListOfOneElement\" ]" );
296
+ }
297
+ }
298
+
268
299
@ Test
269
300
void shouldLogEventForStreamHandler () throws IOException {
270
301
requestStreamHandler = new PowertoolsLogEventForStream ();
0 commit comments