From b43321c025ac2568c9bc8d9a274e75ce55399292 Mon Sep 17 00:00:00 2001 From: samvaity Date: Fri, 10 Mar 2023 11:27:34 -0800 Subject: [PATCH] add matcher and LIVE mode changes --- .../appconfiguration/AadCredentialTest.java | 21 +++++++++++++------ .../ConfigurationAsyncClientTest.java | 3 +-- .../ConfigurationClientBuilderTest.java | 18 +++++++++------- .../ConfigurationClientTest.java | 5 ++--- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/AadCredentialTest.java b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/AadCredentialTest.java index 1d9c782286edc..8355302043695 100644 --- a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/AadCredentialTest.java +++ b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/AadCredentialTest.java @@ -5,6 +5,7 @@ import com.azure.core.credential.TokenCredential; import com.azure.core.http.HttpClient; import com.azure.core.test.TestProxyTestBase; +import com.azure.core.test.models.CustomMatcher; import com.azure.core.util.Configuration; import com.azure.data.appconfiguration.implementation.ConfigurationClientCredentials; import com.azure.data.appconfiguration.models.ConfigurationSetting; @@ -15,6 +16,7 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; +import java.util.Arrays; import static com.azure.data.appconfiguration.ConfigurationClientTestBase.FAKE_CONNECTION_STRING; import static com.azure.data.appconfiguration.TestHelper.DISPLAY_NAME_WITH_ARGUMENTS; @@ -26,7 +28,8 @@ public class AadCredentialTest extends TestProxyTestBase { private static ConfigurationClient client; private static final String AZURE_APPCONFIG_CONNECTION_STRING = "AZURE_APPCONFIG_CONNECTION_STRING"; static String connectionString; - static TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();; + static TokenCredential tokenCredential; + private void setup(HttpClient httpClient, ConfigurationServiceVersion serviceVersion) throws InvalidKeyException, NoSuchAlgorithmException { if (interceptorManager.isPlaybackMode()) { @@ -34,21 +37,27 @@ private void setup(HttpClient httpClient, ConfigurationServiceVersion serviceVer String endpoint = new ConfigurationClientCredentials(connectionString).getBaseUri(); // In playback mode use connection string because CI environment doesn't set up to support AAD client = new ConfigurationClientBuilder() - .credential(tokenCredential) + .connectionString(connectionString) .endpoint(endpoint) .httpClient(interceptorManager.getPlaybackClient()) .buildClient(); + // since running in playback mode won't have the token credential, so skipping matching it. + interceptorManager.addMatchers(Arrays.asList(new CustomMatcher().setExcludedHeaders(Arrays.asList("x-ms-content-sha256")))); } else { connectionString = Configuration.getGlobalConfiguration().get(AZURE_APPCONFIG_CONNECTION_STRING); + tokenCredential = new DefaultAzureCredentialBuilder().build(); String endpoint = new ConfigurationClientCredentials(connectionString).getBaseUri(); - client = new ConfigurationClientBuilder() + ConfigurationClientBuilder builder = new ConfigurationClientBuilder() .httpClient(httpClient) .credential(tokenCredential) .endpoint(endpoint) - .addPolicy(interceptorManager.getRecordPolicy()) // Record - .serviceVersion(serviceVersion) - .buildClient(); + .serviceVersion(serviceVersion); + + if (interceptorManager.isRecordMode()) { + builder.addPolicy(interceptorManager.getRecordPolicy()); // Record + } + client = builder.buildClient(); } } diff --git a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationAsyncClientTest.java b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationAsyncClientTest.java index 13e7efc353fbd..4d990b3ca09af 100644 --- a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationAsyncClientTest.java +++ b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationAsyncClientTest.java @@ -12,7 +12,6 @@ import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.Response; -import com.azure.core.test.TestMode; import com.azure.core.test.http.AssertingHttpClientBuilder; import com.azure.core.util.logging.ClientLogger; import com.azure.data.appconfiguration.models.ConfigurationSetting; @@ -78,7 +77,7 @@ private ConfigurationAsyncClient getConfigurationAsyncClient(HttpClient httpClie .httpClient(buildAsyncAssertingClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)) .serviceVersion(serviceVersion) .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)); - if (getTestMode() != TestMode.PLAYBACK) { + if (interceptorManager.isRecordMode()) { builder .addPolicy(interceptorManager.getRecordPolicy()) .addPolicy(new RetryPolicy()); diff --git a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientBuilderTest.java b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientBuilderTest.java index 79dab91037e2b..30b13265e8502 100644 --- a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientBuilderTest.java +++ b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientBuilderTest.java @@ -14,7 +14,7 @@ import com.azure.core.http.policy.RetryOptions; import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.policy.TimeoutPolicy; -import com.azure.core.test.TestBase; +import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.annotation.DoNotRecord; import com.azure.core.test.http.MockHttpResponse; import com.azure.core.util.ClientOptions; @@ -41,7 +41,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class ConfigurationClientBuilderTest extends TestBase { +public class ConfigurationClientBuilderTest extends TestProxyTestBase { private static final String AZURE_APPCONFIG_CONNECTION_STRING = "AZURE_APPCONFIG_CONNECTION_STRING"; private static final String DEFAULT_DOMAIN_NAME = ".azconfig.io"; private static final String NAMESPACE_NAME = "dummyNamespaceName"; @@ -191,7 +191,7 @@ public void nullServiceVersion(HttpClient httpClient) { .serviceVersion(null) .httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient); - if (!interceptorManager.isPlaybackMode()) { + if (interceptorManager.isRecordMode()) { clientBuilder.addPolicy(interceptorManager.getRecordPolicy()); } @@ -214,7 +214,7 @@ public void defaultPipeline() { .configuration(Configuration.getGlobalConfiguration()) .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)); - if (!interceptorManager.isPlaybackMode()) { + if (interceptorManager.isRecordMode()) { clientBuilder.addPolicy(interceptorManager.getRecordPolicy()); } @@ -222,9 +222,13 @@ public void defaultPipeline() { : HttpClient.createDefault(); ConfigurationSetting addedSetting = clientBuilder - .httpClient(defaultHttpClient) - .buildClient() - .setConfigurationSetting(key, null, value); + .httpClient(defaultHttpClient) + .buildClient() + .setConfigurationSetting(key, null, value); + + if (interceptorManager.isPlaybackMode()) { + clientBuilder.httpClient(defaultHttpClient); + } assertEquals(addedSetting.getKey(), key); assertEquals(addedSetting.getValue(), value); } diff --git a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientTest.java b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientTest.java index 2b42fe67c9fce..8d135b1a476de 100644 --- a/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientTest.java +++ b/sdk/appconfiguration/azure-data-appconfiguration/src/test/java/com/azure/data/appconfiguration/ConfigurationClientTest.java @@ -12,7 +12,6 @@ import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.PagedIterable; import com.azure.core.http.rest.Response; -import com.azure.core.test.TestMode; import com.azure.core.test.http.AssertingHttpClientBuilder; import com.azure.core.util.Context; import com.azure.core.util.logging.ClientLogger; @@ -70,10 +69,10 @@ private ConfigurationClient getConfigurationClient(HttpClient httpClient, return clientSetup(credentials -> { ConfigurationClientBuilder builder = new ConfigurationClientBuilder() .connectionString(connectionString) - .httpClient(buildSyncAssertingClient(getTestMode() == TestMode.PLAYBACK ? interceptorManager.getPlaybackClient() : httpClient)) + .httpClient(buildSyncAssertingClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)) .serviceVersion(serviceVersion) .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)); - if (getTestMode() != TestMode.PLAYBACK) { + if (interceptorManager.isRecordMode()) { builder .addPolicy(interceptorManager.getRecordPolicy()) .addPolicy(new RetryPolicy());