Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent NullPointerException in LIVE TestMode #19620

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ public void beforeEach(ExtensionContext extensionContext) {
@BeforeEach
public void setupTest(TestInfo testInfo) {
this.testContextManager = new TestContextManager(testInfo.getTestMethod().get(), testMode);
if (testIterationContext != null) {
testContextManager.setTestIteration(testIterationContext.testIteration);
}
testContextManager.setTestIteration(testIterationContext.testIteration);
logger.info("Test Mode: {}, Name: {}", testMode, testContextManager.getTestName());

try {
interceptorManager = new InterceptorManager(testContextManager);
} catch (UncheckedIOException e) {
logger.error("Could not create interceptor for {}", testContextManager.getTestName(), e);
Assertions.fail();
Assertions.fail(e);
}
testResourceNamer = new TestResourceNamer(testContextManager, interceptorManager.getRecordedData());

Expand All @@ -110,7 +108,7 @@ public void teardownTest(TestInfo testInfo) {
*
* @return The TestMode that has been initialized.
*/
public TestMode getTestMode() {
public static TestMode getTestMode() {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
return testMode;
}

Expand Down Expand Up @@ -193,7 +191,14 @@ public static boolean shouldClientBeTested(HttpClient client) {
.contains(configuredHttpClient.trim().toLowerCase(Locale.ROOT)));
}

private static TestMode initializeTestMode() {
/**
* Initializes the {@link TestMode} from the environment configuration {@code AZURE_TEST_MODE}.
* <p>
* If {@code AZURE_TEST_MODE} isn't configured or is invalid then {@link TestMode#PLAYBACK} is returned.
*
* @return The {@link TestMode} being used for testing.
*/
public static TestMode initializeTestMode() {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
final ClientLogger logger = new ClientLogger(TestBase.class);
final String azureTestMode = Configuration.getGlobalConfiguration().get(AZURE_TEST_MODE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.azure.core.http.HttpPipelineNextPolicy;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.test.models.NetworkCallError;
import com.azure.core.test.models.NetworkCallRecord;
import com.azure.core.test.models.RecordedData;
Expand All @@ -29,7 +31,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.zip.GZIPInputStream;

Expand Down Expand Up @@ -70,13 +71,18 @@ public RecordNetworkCallPolicy(RecordedData recordedData) {
* @param redactors The custom redactor functions to apply to redact sensitive information from recorded data.
*/
public RecordNetworkCallPolicy(RecordedData recordedData, List<Function<String, String>> redactors) {
Objects.requireNonNull(recordedData, "'recordedData' cannot be null.");
this.recordedData = recordedData;
redactor = new RecordingRedactor(redactors);

}

@Override
public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next) {
// Test is running in LIVE mode so it won't be able to record the network call, skip recording code.
if (TestBase.getTestMode() == TestMode.LIVE) {
samvaity marked this conversation as resolved.
Show resolved Hide resolved
return next.process();
}

final NetworkCallRecord networkCallRecord = new NetworkCallRecord();
Map<String, String> headers = new HashMap<>();

Expand Down Expand Up @@ -105,7 +111,7 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
}).flatMap(httpResponse -> {
final HttpResponse bufferedResponse = httpResponse.buffer();

return extractResponseData(bufferedResponse).map(responseData -> {
return extractResponseData(bufferedResponse, redactor, logger).map(responseData -> {
networkCallRecord.setResponse(responseData);
String body = responseData.get(BODY);

Expand All @@ -122,14 +128,14 @@ public Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineN
});
}

private void redactedAccountName(UrlBuilder urlBuilder) {
private static void redactedAccountName(UrlBuilder urlBuilder) {
String[] hostParts = urlBuilder.getHost().split("\\.");
hostParts[0] = "REDACTED";

urlBuilder.setHost(String.join(".", hostParts));
}

private void captureRequestHeaders(HttpHeaders requestHeaders, Map<String, String> captureHeaders,
private static void captureRequestHeaders(HttpHeaders requestHeaders, Map<String, String> captureHeaders,
String... headerNames) {
for (String headerName : headerNames) {
if (requestHeaders.getValue(headerName) != null) {
Expand All @@ -138,7 +144,8 @@ private void captureRequestHeaders(HttpHeaders requestHeaders, Map<String, Strin
}
}

private Mono<Map<String, String>> extractResponseData(final HttpResponse response) {
private static Mono<Map<String, String>> extractResponseData(final HttpResponse response,
final RecordingRedactor redactor, final ClientLogger logger) {
final Map<String, String> responseData = new HashMap<>();
responseData.put(STATUS_CODE, Integer.toString(response.getStatusCode()));

Expand Down