Skip to content

Commit

Permalink
[Metrics Advisor] Re-record with Test proxy (#34435)
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity authored Apr 18, 2023
1 parent 79ad675 commit 65e6ed3
Show file tree
Hide file tree
Showing 144 changed files with 30,494 additions and 12,208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@
opens com.azure.ai.metricsadvisor.administration.models to com.fasterxml.jackson.databind;
opens com.azure.ai.metricsadvisor.models to com.fasterxml.jackson.databind;
opens com.azure.ai.metricsadvisor.implementation.models to com.fasterxml.jackson.databind, com.azure.core;
exports com.azure.ai.metricsadvisor.implementation.models;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import org.junit.jupiter.api.Disabled;
Expand All @@ -22,20 +23,22 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Duration;
import java.util.Arrays;
import java.util.function.Consumer;

import static com.azure.ai.metricsadvisor.MetricsAdvisorClientBuilderTest.PLAYBACK_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.AZURE_METRICS_ADVISOR_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS;
import static com.azure.ai.metricsadvisor.TestUtils.INVALID_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.getEmailSanitizers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests for Metrics Advisor Administration client builder
*/
public class MetricsAdvisorAdminClientBuilderTest extends TestBase {
public class MetricsAdvisorAdminClientBuilderTest extends TestProxyTestBase {
/**
* Test client builder with invalid API key
*/
Expand Down Expand Up @@ -143,6 +146,7 @@ private void clientBuilderWithTokenCredentialRunner(HttpClient httpClient, Metri
.configuration(Configuration.getGlobalConfiguration())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));

interceptorManager.addSanitizers(getEmailSanitizers());
if (interceptorManager.isPlaybackMode()) {
clientBuilder.credential(new MetricsAdvisorKeyCredential("subscription_key", "api_key"));
} else {
Expand Down Expand Up @@ -170,11 +174,13 @@ private MetricsAdvisorAdministrationClientBuilder createClientBuilder(HttpClient
final MetricsAdvisorAdministrationClientBuilder clientBuilder = new MetricsAdvisorAdministrationClientBuilder()
.credential(credential)
.endpoint(endpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.serviceVersion(serviceVersion);

if (!interceptorManager.isPlaybackMode()) {
interceptorManager.addSanitizers(getEmailSanitizers());
if (interceptorManager.isRecordMode()) {
clientBuilder.addPolicy(interceptorManager.getRecordPolicy());
} else if (interceptorManager.isPlaybackMode()) {
interceptorManager.addMatchers(Arrays.asList(new CustomMatcher().setHeadersKeyOnlyMatch(Arrays.asList("x-api-key"))));
}

return clientBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@
import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.http.AssertingHttpClientBuilder;
import com.azure.core.test.models.BodilessMatcher;
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;

import java.util.Arrays;

import static com.azure.ai.metricsadvisor.MetricsAdvisorClientBuilderTest.PLAYBACK_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.AZURE_METRICS_ADVISOR_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.getEmailSanitizers;

public abstract class MetricsAdvisorAdministrationClientTestBase extends TestBase {
public abstract class MetricsAdvisorAdministrationClientTestBase extends TestProxyTestBase {

@Override
protected void beforeTest() {
Expand All @@ -37,11 +42,11 @@ private HttpClient buildSyncAssertingClient(HttpClient httpClient) {

MetricsAdvisorAdministrationClientBuilder getMetricsAdvisorAdministrationBuilder(HttpClient httpClient,
MetricsAdvisorServiceVersion serviceVersion, boolean isSync) {
HttpClient httpClient1;
HttpClient httpClient1 = interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient;
if (isSync) {
httpClient1 = buildSyncAssertingClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
httpClient1 = buildSyncAssertingClient(httpClient1);
} else {
httpClient1 = buildAsyncAssertingClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
httpClient1 = buildAsyncAssertingClient(httpClient1);
}
return getMetricsAdvisorAdministrationBuilderInternal(httpClient1, serviceVersion, true);
}
Expand All @@ -57,20 +62,31 @@ MetricsAdvisorAdministrationClientBuilder getMetricsAdvisorAdministrationBuilder
boolean useKeyCredential) {
MetricsAdvisorAdministrationClientBuilder builder = new MetricsAdvisorAdministrationClientBuilder()
.endpoint(getEndpoint())
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(httpClient)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.serviceVersion(serviceVersion)
.addPolicy(interceptorManager.getRecordPolicy());
.serviceVersion(serviceVersion);
interceptorManager.addSanitizers(getEmailSanitizers());

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new MetricsAdvisorKeyCredential("subscription_key", "api_key"));
if (useKeyCredential) {
if (interceptorManager.isPlaybackMode()) {
builder.credential(new MetricsAdvisorKeyCredential("subscription_key", "api_key"));
// setting bodiless matcher to "exclude" matching request bodies with UUID's
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher(), new CustomMatcher().setHeadersKeyOnlyMatch(Arrays.asList("x-api-key"))));
} else if (interceptorManager.isRecordMode()) {
builder
.credential(new MetricsAdvisorKeyCredential(
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_SUBSCRIPTION_KEY"),
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_API_KEY")))
.addPolicy(interceptorManager.getRecordPolicy());
}
} else {
if (useKeyCredential) {
builder.credential(new MetricsAdvisorKeyCredential(
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_SUBSCRIPTION_KEY"),
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_API_KEY")));
} else {
builder.credential(new DefaultAzureCredentialBuilder().build());
if (interceptorManager.isPlaybackMode()) {
builder.credential(new MockTokenCredential());
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher()));
} else if (interceptorManager.isRecordMode()) {
builder
.credential(new DefaultAzureCredentialBuilder().build())
.addPolicy(interceptorManager.getRecordPolicy());
}
}
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.util.Configuration;
import com.azure.core.util.Context;
import com.azure.identity.DefaultAzureCredentialBuilder;
Expand All @@ -23,19 +24,21 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Duration;
import java.util.Arrays;
import java.util.function.Consumer;

import static com.azure.ai.metricsadvisor.TestUtils.AZURE_METRICS_ADVISOR_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS;
import static com.azure.ai.metricsadvisor.TestUtils.INVALID_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.getEmailSanitizers;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests for Metrics Advisor client builder
*/
public class MetricsAdvisorClientBuilderTest extends TestBase {
public class MetricsAdvisorClientBuilderTest extends TestProxyTestBase {
private static final String METRIC_ID = "b6c0649c-0c51-4aa6-82b6-3c3b0aa55066";
private static final int PAGE_SIZE = 10;
private static final int LISTING_LIMIT = 100;
Expand Down Expand Up @@ -185,13 +188,15 @@ private MetricsAdvisorClientBuilder createClientBuilder(HttpClient httpClient, M
final MetricsAdvisorClientBuilder clientBuilder = new MetricsAdvisorClientBuilder()
.credential(credential)
.endpoint(endpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.serviceVersion(serviceVersion);

if (!interceptorManager.isPlaybackMode()) {
interceptorManager.addSanitizers(getEmailSanitizers());
if (interceptorManager.isRecordMode()) {
clientBuilder.addPolicy(interceptorManager.getRecordPolicy());
} else if (interceptorManager.isPlaybackMode()) {
interceptorManager.addMatchers(Arrays.asList(new CustomMatcher().setHeadersKeyOnlyMatch(Arrays.asList("x-api-key"))));
}

return clientBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.http.AssertingHttpClientBuilder;
import com.azure.core.test.models.BodilessMatcher;
import com.azure.core.test.models.CustomMatcher;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;

import java.util.Arrays;

import static com.azure.ai.metricsadvisor.TestUtils.AZURE_METRICS_ADVISOR_ENDPOINT;
import static com.azure.ai.metricsadvisor.TestUtils.getEmailSanitizers;

public abstract class MetricsAdvisorClientTestBase extends TestBase {
public abstract class MetricsAdvisorClientTestBase extends TestProxyTestBase {

@Override
protected void beforeTest() {
Expand All @@ -35,11 +40,11 @@ private HttpClient buildSyncAssertingClient(HttpClient httpClient) {

MetricsAdvisorClientBuilder getMetricsAdvisorBuilder(HttpClient httpClient,
MetricsAdvisorServiceVersion serviceVersion, boolean isSync) {
HttpClient httpClient1;
HttpClient httpClient1 = interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient;
if (isSync) {
httpClient1 = buildSyncAssertingClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
httpClient1 = buildSyncAssertingClient(httpClient1);
} else {
httpClient1 = buildAsyncAssertingClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
httpClient1 = buildAsyncAssertingClient(httpClient1);
}
return getMetricsAdvisorBuilderInternal(httpClient1, serviceVersion, true);
}
Expand All @@ -49,20 +54,32 @@ MetricsAdvisorClientBuilder getMetricsAdvisorBuilderInternal(HttpClient httpClie
boolean useKeyCredential) {
MetricsAdvisorClientBuilder builder = new MetricsAdvisorClientBuilder()
.endpoint(getEndpoint())
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(httpClient)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.serviceVersion(serviceVersion)
.addPolicy(interceptorManager.getRecordPolicy());
.serviceVersion(serviceVersion);

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new MetricsAdvisorKeyCredential("subscription_key", "api_key"));
interceptorManager.addSanitizers(getEmailSanitizers());
if (useKeyCredential) {
if (interceptorManager.isPlaybackMode()) {
builder.credential(new MetricsAdvisorKeyCredential("subscription_key", "api_key"));
// setting bodiless matcher to "exclude" matching request bodies with UUID's
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher(), new CustomMatcher().setHeadersKeyOnlyMatch(Arrays.asList("x-api-key"))));
} else if (interceptorManager.isRecordMode()) {
builder
.credential(new MetricsAdvisorKeyCredential(
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_SUBSCRIPTION_KEY"),
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_API_KEY")))
.addPolicy(interceptorManager.getRecordPolicy());
}
} else {
if (useKeyCredential) {
builder.credential(new MetricsAdvisorKeyCredential(
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_SUBSCRIPTION_KEY"),
Configuration.getGlobalConfiguration().get("AZURE_METRICS_ADVISOR_API_KEY")));
} else {
builder.credential(new DefaultAzureCredentialBuilder().build());
if (interceptorManager.isPlaybackMode()) {
builder.credential(new MockTokenCredential());
// setting bodiless matcher to "exclude" matching request bodies with UUID's
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher()));
} else if (interceptorManager.isRecordMode()) {
builder
.credential(new DefaultAzureCredentialBuilder().build())
.addPolicy(interceptorManager.getRecordPolicy());
}
}
return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.azure.ai.metricsadvisor.administration.models.DataFeedSchema;
import com.azure.ai.metricsadvisor.administration.models.SqlServerDataFeedSource;
import com.azure.core.http.HttpClient;
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -26,6 +27,7 @@

import static com.azure.core.test.TestBase.AZURE_TEST_SERVICE_VERSIONS_VALUE_ALL;
import static com.azure.core.test.TestBase.getHttpClients;
import static com.azure.core.test.models.TestProxySanitizerType.BODY_KEY;

/**
* Utility class for common methods and constants used in test classes.
Expand Down Expand Up @@ -178,6 +180,14 @@ static Stream<Arguments> getTestParameters() {
return argumentsList.stream();
}

static List<TestProxySanitizer> getEmailSanitizers() {
return Arrays.asList(
// TODO (savaity) https://github.com/Azure/azure-sdk-for-java/issues/34506
new TestProxySanitizer("admins", "^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*@", "REDACTED.",
BODY_KEY),
new TestProxySanitizer("$..userPrincipal", "^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*@", "REDACTED.", BODY_KEY));
}

/**
* Returns whether the given service version match the rules of test framework.
*
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 65e6ed3

Please sign in to comment.