18
18
import com .azure .core .util .logging .LogLevel ;
19
19
import org .junit .jupiter .api .AfterEach ;
20
20
import org .junit .jupiter .api .BeforeEach ;
21
+ import org .junit .jupiter .api .parallel .Execution ;
22
+ import org .junit .jupiter .api .parallel .ExecutionMode ;
23
+ import org .junit .jupiter .api .parallel .Isolated ;
21
24
import org .junit .jupiter .api .parallel .ResourceLock ;
25
+ import org .junit .jupiter .api .parallel .Resources ;
22
26
import org .junit .jupiter .params .ParameterizedTest ;
23
27
import org .junit .jupiter .params .provider .Arguments ;
24
28
import org .junit .jupiter .params .provider .EnumSource ;
28
32
import reactor .test .StepVerifier ;
29
33
30
34
import java .io .ByteArrayOutputStream ;
31
- import java .io .IOException ;
32
35
import java .io .PrintStream ;
36
+ import java .io .UncheckedIOException ;
33
37
import java .io .UnsupportedEncodingException ;
34
38
import java .net .MalformedURLException ;
35
39
import java .net .URL ;
50
54
/**
51
55
* This class contains tests for {@link HttpLoggingPolicy}.
52
56
*/
57
+ @ Execution (ExecutionMode .SAME_THREAD )
58
+ @ Isolated
59
+ @ ResourceLock (Resources .SYSTEM_OUT )
53
60
public class HttpLoggingPolicyTests {
54
61
private static final String REDACTED = "REDACTED" ;
55
62
private static final Context CONTEXT = new Context ("caller-method" , HttpLoggingPolicyTests .class .getName ());
@@ -61,9 +68,7 @@ public class HttpLoggingPolicyTests {
61
68
@ BeforeEach
62
69
public void prepareForTest () {
63
70
// Set the log level to information for the test.
64
- originalLogLevel = Configuration .getGlobalConfiguration ().get (PROPERTY_AZURE_LOG_LEVEL );
65
- Configuration .getGlobalConfiguration ().put (PROPERTY_AZURE_LOG_LEVEL ,
66
- String .valueOf (LogLevel .INFORMATIONAL .getLogLevel ()));
71
+ setupLogLevel (LogLevel .INFORMATIONAL .getLogLevel ());
67
72
68
73
/*
69
74
* DefaultLogger uses System.out to log. Inject a custom PrintStream to log into for the duration of the test to
@@ -75,25 +80,19 @@ public void prepareForTest() {
75
80
}
76
81
77
82
@ AfterEach
78
- public void cleanupAfterTest () throws IOException {
83
+ public void cleanupAfterTest () {
79
84
// Reset or clear the log level after the test completes.
80
- if (CoreUtils .isNullOrEmpty (originalLogLevel )) {
81
- Configuration .getGlobalConfiguration ().remove (PROPERTY_AZURE_LOG_LEVEL );
82
- } else {
83
- Configuration .getGlobalConfiguration ().put (PROPERTY_AZURE_LOG_LEVEL , originalLogLevel );
84
- }
85
+ setPropertyToOriginalOrClear (originalLogLevel );
85
86
86
87
// Reset System.err to the original PrintStream.
87
88
System .setOut (originalSystemOut );
88
- logCaptureStream .close ();
89
89
}
90
90
91
91
/**
92
92
* Tests that a query string will be properly redacted before it is logged.
93
93
*/
94
94
@ ParameterizedTest
95
95
@ MethodSource ("redactQueryParametersSupplier" )
96
- @ ResourceLock ("SYSTEM_OUT" )
97
96
public void redactQueryParameters (String requestUrl , String expectedQueryString ,
98
97
Set <String > allowedQueryParameters ) {
99
98
HttpPipeline pipeline = new HttpPipelineBuilder ()
@@ -138,7 +137,6 @@ private static Stream<Arguments> redactQueryParametersSupplier() {
138
137
*/
139
138
@ ParameterizedTest (name = "[{index}] {displayName}" )
140
139
@ MethodSource ("validateLoggingDoesNotConsumeSupplier" )
141
- @ ResourceLock ("SYSTEM_OUT" )
142
140
public void validateLoggingDoesNotConsumeRequest (Flux <ByteBuffer > stream , byte [] data , int contentLength )
143
141
throws MalformedURLException {
144
142
URL requestUrl = new URL ("https://test.com" );
@@ -154,7 +152,7 @@ public void validateLoggingDoesNotConsumeRequest(Flux<ByteBuffer> stream, byte[]
154
152
.build ();
155
153
156
154
StepVerifier .create (pipeline .send (new HttpRequest (HttpMethod .POST , requestUrl , requestHeaders , stream ),
157
- CONTEXT ))
155
+ CONTEXT ))
158
156
.verifyComplete ();
159
157
160
158
String logString = convertOutputStreamToString (logCaptureStream );
@@ -166,7 +164,6 @@ public void validateLoggingDoesNotConsumeRequest(Flux<ByteBuffer> stream, byte[]
166
164
*/
167
165
@ ParameterizedTest (name = "[{index}] {displayName}" )
168
166
@ MethodSource ("validateLoggingDoesNotConsumeSupplier" )
169
- @ ResourceLock ("SYSTEM_OUT" )
170
167
public void validateLoggingDoesNotConsumeResponse (Flux <ByteBuffer > stream , byte [] data , int contentLength ) {
171
168
HttpRequest request = new HttpRequest (HttpMethod .GET , "https://test.com" );
172
169
HttpHeaders responseHeaders = new HttpHeaders ()
@@ -276,8 +273,7 @@ public Mono<String> getBodyAsString(Charset charset) {
276
273
277
274
@ ParameterizedTest (name = "[{index}] {displayName}" )
278
275
@ EnumSource (value = HttpLogDetailLevel .class , mode = EnumSource .Mode .INCLUDE ,
279
- names = { "BASIC" , "HEADERS" , "BODY" , "BODY_AND_HEADERS" })
280
- @ ResourceLock ("SYSTEM_OUT" )
276
+ names = {"BASIC" , "HEADERS" , "BODY" , "BODY_AND_HEADERS" })
281
277
public void loggingIncludesRetryCount (HttpLogDetailLevel logLevel ) {
282
278
AtomicInteger requestCount = new AtomicInteger ();
283
279
HttpRequest request = new HttpRequest (HttpMethod .GET , "https://test.com" );
@@ -298,11 +294,24 @@ public void loggingIncludesRetryCount(HttpLogDetailLevel logLevel) {
298
294
assertTrue (logString .contains ("Try count: 2" ));
299
295
}
300
296
297
+ private void setupLogLevel (int logLevelToSet ) {
298
+ originalLogLevel = Configuration .getGlobalConfiguration ().get (PROPERTY_AZURE_LOG_LEVEL );
299
+ Configuration .getGlobalConfiguration ().put (PROPERTY_AZURE_LOG_LEVEL , String .valueOf (logLevelToSet ));
300
+ }
301
+
302
+ private void setPropertyToOriginalOrClear (String originalValue ) {
303
+ if (CoreUtils .isNullOrEmpty (originalValue )) {
304
+ Configuration .getGlobalConfiguration ().remove (PROPERTY_AZURE_LOG_LEVEL );
305
+ } else {
306
+ Configuration .getGlobalConfiguration ().put (PROPERTY_AZURE_LOG_LEVEL , originalValue );
307
+ }
308
+ }
309
+
301
310
private static String convertOutputStreamToString (ByteArrayOutputStream stream ) {
302
311
try {
303
- return stream .toString ("UTF-8" );
312
+ return stream .toString (StandardCharsets . UTF_8 . name () );
304
313
} catch (UnsupportedEncodingException e ) {
305
- throw new RuntimeException (e );
314
+ throw new UncheckedIOException (e );
306
315
}
307
316
}
308
317
}
0 commit comments