Skip to content

Commit

Permalink
Updates to azure-core-test (#41775)
Browse files Browse the repository at this point in the history
Updates to azure-core-test
  • Loading branch information
alzimmermsft authored Sep 11, 2024
1 parent dc6d535 commit 5faff56
Show file tree
Hide file tree
Showing 23 changed files with 536 additions and 237 deletions.
1 change: 1 addition & 0 deletions eng/versioning/external_dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ javax.json:javax.json-api;1.1.4
javax.servlet:javax.servlet-api;4.0.1
javax.websocket:javax.websocket-api;1.1
org.apache.commons:commons-compress;1.26.0
org.apache.ant:ant;1.10.14
org.apache.avro:avro;1.11.3
org.apache.avro:avro-maven-plugin;1.11.3
org.apache.commons:commons-lang3;3.12.0
Expand Down
29 changes: 18 additions & 11 deletions sdk/core/azure-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
<artifactId>azure-core</artifactId>
<version>1.52.0-beta.1</version> <!-- {x-version-update;com.azure:azure-core;current} -->
</dependency>

<!-- Azure HTTP client implementation depdencnies -->
<!-- Downstream libraries no longer need to add test dependencies on these libraries to run tests against different implementations. -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-jdk-httpclient</artifactId>
Expand All @@ -95,34 +98,37 @@
<version>1.0.0-beta.21</version> <!-- {x-version-update;com.azure:azure-core-http-vertx;current} -->
</dependency>

<!-- Common external testing dependencies -->
<!-- Downstream libraries no longer need to add test dependencies on these libraries. -->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.4.38</version> <!-- {x-version-update;io.projectreactor:reactor-test;external_dependency} -->
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.0</version> <!-- {x-version-update;org.apache.commons:commons-compress;external_dependency} -->
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} -->
</dependency>

<!-- Used to unpack the tar.gz files of Test Proxy server -->
<!-- This used to be org.apache.commons:commons-compress but it was replaced with org.apache.ant:ant -->
<!-- This was done as ant has fewer transitive dependencies, meaning there is less chance of conflicts with other libraries. -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.3</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
<scope>test</scope>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.14</version> <!-- {x-version-update;org.apache.ant:ant;external_dependency} -->
</dependency>
</dependencies>

Expand All @@ -138,9 +144,10 @@
<includes>
<!-- special allowance for azure-core-test as it is not a shipping library: -->
<include>org.junit.jupiter:junit-jupiter-api:[5.9.3]</include> <!-- {x-include-update;org.junit.jupiter:junit-jupiter-api;external_dependency} -->
<include>org.junit.jupiter:junit-jupiter-engine:[5.9.3]</include> <!-- {x-include-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
<include>org.junit.jupiter:junit-jupiter-params:[5.9.3]</include> <!-- {x-include-update;org.junit.jupiter:junit-jupiter-params;external_dependency} -->
<include>io.projectreactor:reactor-test:[3.4.38]</include> <!-- {x-include-update;io.projectreactor:reactor-test;external_dependency} -->
<include>org.apache.commons:commons-compress:[1.26.0]</include> <!-- {x-include-update;org.apache.commons:commons-compress;external_dependency} -->
<include>org.apache.ant:ant:[1.10.14]</include> <!-- {x-include-update;org.apache.ant:ant;external_dependency} -->
</includes>
</bannedDependencies>
</rules>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import com.azure.core.test.utils.TestUtils;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.azure.json.JsonProviders;
import com.azure.json.JsonReader;
import com.azure.json.JsonWriter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand Down Expand Up @@ -58,8 +59,6 @@
* calls that were recorded are persisted to: "<i>session-records/{@code testName}.json</i>"
*/
public class InterceptorManager implements AutoCloseable {
private static final ObjectMapper RECORD_MAPPER = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);

private static final ClientLogger LOGGER = new ClientLogger(InterceptorManager.class);
private final Map<String, String> textReplacementRules;
private final String testName;
Expand Down Expand Up @@ -267,6 +266,7 @@ public RecordedData getRecordedData() {

/**
* A {@link Supplier} for retrieving a variable from a test proxy recording.
*
* @return The supplier for retrieving a variable.
*/
public Supplier<String> getProxyVariableSupplier() {
Expand All @@ -282,6 +282,7 @@ public Supplier<String> getProxyVariableSupplier() {

/**
* Get a {@link Consumer} for adding variables used in test proxy tests.
*
* @return The consumer for adding a variable.
*/
public Consumer<String> getProxyVariableConsumer() {
Expand All @@ -293,7 +294,6 @@ public Consumer<String> getProxyVariableConsumer() {
* {@link InterceptorManager}.
*
* @return HttpPipelinePolicy to record network calls.
*
* @throws IllegalStateException A recording policy was requested when the test proxy is enabled and test mode is not RECORD.
*/
public HttpPipelinePolicy getRecordPolicy() {
Expand All @@ -310,7 +310,6 @@ public HttpPipelinePolicy getRecordPolicy() {
* @param recordingRedactors The custom redactor functions that are applied in addition to the default redactor
* functions defined in {@link RecordingRedactor}.
* @return {@link HttpPipelinePolicy} to record network calls.
*
* @throws IllegalStateException A recording policy was requested when the test proxy is enabled and test mode is not RECORD.
*/
public HttpPipelinePolicy getRecordPolicy(List<Function<String, String>> recordingRedactors) {
Expand All @@ -324,7 +323,6 @@ public HttpPipelinePolicy getRecordPolicy(List<Function<String, String>> recordi
* Gets a new HTTP client that plays back test session records managed by {@link InterceptorManager}.
*
* @return An HTTP client that plays back network calls from its recorded data.
*
* @throws IllegalStateException A playback client was requested when the test proxy is enabled and test mode is LIVE.
*/
public HttpClient getPlaybackClient() {
Expand Down Expand Up @@ -356,8 +354,9 @@ public void close() {
if (testProxyEnabled) {
testProxyRecordPolicy.stopRecording(proxyVariableQueue);
} else {
try (BufferedWriter writer = Files.newBufferedWriter(createRecordFile(playbackRecordName).toPath())) {
RECORD_MAPPER.writeValue(writer, recordedData);
try (BufferedWriter writer = Files.newBufferedWriter(createRecordFile(playbackRecordName).toPath());
JsonWriter jsonWriter = JsonProviders.createWriter(writer)) {
jsonWriter.writeJson(recordedData).flush();
} catch (IOException ex) {
throw LOGGER
.logExceptionAsError(new UncheckedIOException("Unable to write data to playback file.", ex));
Expand All @@ -371,8 +370,9 @@ public void close() {
private RecordedData readDataFromFile() {
File recordFile = getRecordFile();

try (BufferedReader reader = Files.newBufferedReader(recordFile.toPath())) {
return RECORD_MAPPER.readValue(reader, RecordedData.class);
try (BufferedReader reader = Files.newBufferedReader(recordFile.toPath());
JsonReader jsonReader = JsonProviders.createReader(reader)) {
return RecordedData.fromJson(jsonReader);
} catch (IOException ex) {
throw LOGGER.logExceptionAsWarning(new UncheckedIOException(ex));
}
Expand All @@ -391,6 +391,7 @@ private HttpPipelinePolicy getProxyRecordingPolicy() {

/**
* Computes the relative path of the record file to the repo root.
*
* @return A {@link File} with the partial path to where the record file lives.
*/
private File getTestProxyRecordFile() {
Expand Down Expand Up @@ -457,6 +458,7 @@ public void addTextReplacementRule(String regex, String replacement) {

/**
* Add sanitizer rule for sanitization during record or playback.
*
* @param testProxySanitizers the list of replacement regex and rules.
* @throws RuntimeException Neither playback or record has started.
*/
Expand All @@ -475,6 +477,7 @@ public void addSanitizers(List<TestProxySanitizer> testProxySanitizers) {

/**
* Disable common sanitizer rule for sanitization during record or playback.
*
* @param testProxySanitizersId the list of sanitizer rule Id to disable.
* @throws RuntimeException Neither playback or record has started.
*/
Expand All @@ -493,6 +496,7 @@ public void removeSanitizers(String... testProxySanitizersId) {

/**
* Add sanitizer rule for sanitization during record or playback.
*
* @param testProxySanitizers the list of replacement regex and rules.
*/
public void addSanitizers(TestProxySanitizer... testProxySanitizers) {
Expand All @@ -504,6 +508,7 @@ public void addSanitizers(TestProxySanitizer... testProxySanitizers) {
/**
* Add matcher rules to match recorded data in playback.
* Matchers are only applied for playback session and so this will be a noop when invoked in RECORD/LIVE mode.
*
* @param testProxyMatchers the list of matcher rules when playing back recorded data.
* @throws RuntimeException Playback has not started.
*/
Expand All @@ -525,6 +530,7 @@ public void addMatchers(List<TestProxyRequestMatcher> testProxyMatchers) {
/**
* Add matcher rules to match recorded data in playback.
* Matchers are only applied for playback session and so this will be a noop when invoked in RECORD/LIVE mode.
*
* @param testProxyRequestMatchers the list of matcher rules when playing back recorded data.
*/
public void addMatchers(TestProxyRequestMatcher... testProxyRequestMatchers) {
Expand All @@ -535,6 +541,7 @@ public void addMatchers(TestProxyRequestMatcher... testProxyRequestMatchers) {

/**
* Get the recording file location in assets repo.
*
* @return the assets repo location of the recording file.
*/
public String getRecordingFileLocation() {
Expand All @@ -543,6 +550,7 @@ public String getRecordingFileLocation() {

/**
* Sets the httpClient to be used for this test.
*
* @param httpClient The {@link HttpClient} implementation to use.
*/
void setHttpClient(HttpClient httpClient) {
Expand All @@ -551,6 +559,7 @@ void setHttpClient(HttpClient httpClient) {

/**
* Sets the recording options for the proxy.
*
* @param testProxyRecordingOptions The {@link TestProxyRecordingOptions} to use.
* @throws RuntimeException if test mode is not record.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Mono<HttpResponse> send(HttpRequest request) {
final HttpBinJson json = new HttpBinJson();
json.url(cleanseUrl(requestUrl));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, json);
response = new MockHttpResponse(request, 200, json.toJsonBytes());
}
} else if (requestPathLower.startsWith("/bytes/")) {
final String byteCountString = requestPath.substring("/bytes/".length());
Expand Down Expand Up @@ -144,17 +144,17 @@ public Mono<HttpResponse> send(HttpRequest request) {
final HttpBinJson json = new HttpBinJson();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
response = new MockHttpResponse(request, 200, json);
response = new MockHttpResponse(request, 200, json.toJsonBytes());
} else if ("/get".equals(requestPathLower)) {
final HttpBinJson json = new HttpBinJson();
json.url(cleanseUrl(requestUrl));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, json);
response = new MockHttpResponse(request, 200, json.toJsonBytes());
} else if ("/patch".equals(requestPathLower)) {
final HttpBinJson json = new HttpBinJson();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
response = new MockHttpResponse(request, 200, json);
response = new MockHttpResponse(request, 200, json.toJsonBytes());
} else if ("/post".equals(requestPathLower)) {
if (contentType != null && contentType.contains("x-www-form-urlencoded")) {
Map<String, String> parsed = bodyToMap(request);
Expand All @@ -166,20 +166,20 @@ public Mono<HttpResponse> send(HttpRequest request) {
form.pizzaSize(PizzaSize.valueOf(parsed.get("size")));
form.toppings(Arrays.asList(parsed.get("toppings").split(",")));
json.form(form);
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json.toJsonBytes());
} else {
final HttpBinJson json = new HttpBinJson();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, json);
response = new MockHttpResponse(request, 200, json.toJsonBytes());
}
} else if ("/put".equals(requestPathLower)) {
final HttpBinJson json = new HttpBinJson();
json.url(cleanseUrl(requestUrl));
json.data(createHttpBinResponseDataForRequest(request));
json.headers(toMap(request.getHeaders()));
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json.toJsonBytes());
} else if (requestPathLower.startsWith("/status/")) {
final String statusCodeString = requestPathLower.substring("/status/".length());
final int statusCode = Integer.parseInt(statusCodeString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public class MockHttpResponse extends HttpResponse {
private static final SerializerAdapter SERIALIZER = new JacksonAdapter();

private final int statusCode;

private final HttpHeaders headers;

private final byte[] bodyBytes;

/**
Expand Down
Loading

0 comments on commit 5faff56

Please sign in to comment.