Skip to content

Commit

Permalink
add matcher and LIVE mode changes
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity committed Apr 5, 2023
1 parent 8e12ece commit b43321c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -26,29 +28,36 @@ 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()) {
connectionString = FAKE_CONNECTION_STRING;
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();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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());
}

Expand All @@ -214,17 +214,21 @@ public void defaultPipeline() {
.configuration(Configuration.getGlobalConfiguration())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));

if (!interceptorManager.isPlaybackMode()) {
if (interceptorManager.isRecordMode()) {
clientBuilder.addPolicy(interceptorManager.getRecordPolicy());
}

HttpClient defaultHttpClient = interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient()
: 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit b43321c

Please sign in to comment.