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

[AppConfig] Re-record tests with Test proxy #33902

Merged
merged 10 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -4,7 +4,8 @@

import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.test.TestBase;
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,14 +16,15 @@

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;

/**
* Unit test for construct a configuration client by using AAD token credential.
*/
public class AadCredentialTest extends TestBase {
public class AadCredentialTest extends TestProxyTestBase {
private static ConfigurationClient client;
private static final String AZURE_APPCONFIG_CONNECTION_STRING = "AZURE_APPCONFIG_CONNECTION_STRING";
static String connectionString;
Expand All @@ -39,18 +41,23 @@ private void setup(HttpClient httpClient, ConfigurationServiceVersion serviceVer
.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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea - add calling setHttpClient somewhere.

builder.addPolicy(interceptorManager.getRecordPolicy()); // Record
}
client = builder.buildClient();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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.test.models.CustomMatcher;
import com.azure.core.util.logging.ClientLogger;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
import com.azure.data.appconfiguration.models.FeatureFlagConfigurationSetting;
Expand All @@ -31,6 +31,7 @@
import java.net.HttpURLConnection;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -75,18 +76,33 @@ private ConfigurationAsyncClient getConfigurationAsyncClient(HttpClient httpClie
return clientSetup(credentials -> {
ConfigurationClientBuilder builder = new ConfigurationClientBuilder()
.connectionString(connectionString)
.httpClient(buildAsyncAssertingClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient))
.serviceVersion(serviceVersion)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));
if (getTestMode() != TestMode.PLAYBACK) {

setHttpClient(httpClient, builder);

if (interceptorManager.isRecordMode()) {
builder
.addPolicy(interceptorManager.getRecordPolicy())
.addPolicy(new RetryPolicy());
} else if (interceptorManager.isPlaybackMode()) {
interceptorManager.addMatchers(Arrays.asList(new CustomMatcher().setHeadersKeyOnlyMatch(Arrays.asList("Sync-Token"))));
}
return builder.buildAsyncClient();
});
}

private ConfigurationClientBuilder setHttpClient(HttpClient httpClient, ConfigurationClientBuilder builder) {
if (interceptorManager.isRecordMode()) {
return builder
.httpClient(buildAsyncAssertingClient(httpClient));
} else if (interceptorManager.isPlaybackMode()) {
return builder
.httpClient(buildAsyncAssertingClient(interceptorManager.getPlaybackClient()));
}
return builder;
}

private HttpClient buildAsyncAssertingClient(HttpClient httpClient) {
//skip paging requests until #30031 resolved
BiFunction<HttpRequest, com.azure.core.util.Context, Boolean> skipRequestFunction = (request, context) -> {
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 @@ -188,11 +188,15 @@ public void nullServiceVersion(HttpClient httpClient) {
.connectionString(connectionString)
.retryPolicy(new RetryPolicy())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.serviceVersion(null)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);
.serviceVersion(null);

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

ConfigurationSetting addedSetting = clientBuilder.buildClient().setConfigurationSetting(key, null, value);
Expand All @@ -214,17 +218,20 @@ public void defaultPipeline() {
.configuration(Configuration.getGlobalConfiguration())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));

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

HttpClient defaultHttpClient = interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient()
: HttpClient.createDefault();
if (interceptorManager.isPlaybackMode()) {
clientBuilder.httpClient(interceptorManager.getPlaybackClient());
}

ConfigurationSetting addedSetting = clientBuilder
.httpClient(defaultHttpClient)
.buildClient()
.setConfigurationSetting(key, null, value);
.buildClient()
.setConfigurationSetting(key, null, value);

assertEquals(addedSetting.getKey(), key);
assertEquals(addedSetting.getValue(), value);
}
Expand Down Expand Up @@ -269,8 +276,8 @@ public void getEndpointAtClientInstance() {
.connectionString(FAKE_CONNECTION_STRING);
ConfigurationClient client = configurationClientBuilder.buildClient();
final ConfigurationAsyncClient asyncClient = configurationClientBuilder.buildAsyncClient();
assertEquals("http://localhost:8080", client.getEndpoint());
assertEquals("http://localhost:8080", asyncClient.getEndpoint());
assertEquals("https://localhost:8080", client.getEndpoint());
assertEquals("https://localhost:8080", asyncClient.getEndpoint());
}

private static URI getURI(String endpointFormat, String namespace, String domainName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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.test.models.CustomMatcher;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.data.appconfiguration.models.ConfigurationSetting;
Expand All @@ -28,6 +28,7 @@

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -70,18 +71,33 @@ private ConfigurationClient getConfigurationClient(HttpClient httpClient,
return clientSetup(credentials -> {
ConfigurationClientBuilder builder = new ConfigurationClientBuilder()
.connectionString(connectionString)
.httpClient(buildSyncAssertingClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient))
.serviceVersion(serviceVersion)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS));
if (getTestMode() != TestMode.PLAYBACK) {

setHttpClient(httpClient, builder);

if (interceptorManager.isRecordMode()) {
builder
.addPolicy(interceptorManager.getRecordPolicy())
.addPolicy(new RetryPolicy());
} else if (interceptorManager.isPlaybackMode()) {
interceptorManager.addMatchers(Arrays.asList(new CustomMatcher().setHeadersKeyOnlyMatch(Arrays.asList("Sync-Token"))));
}
return builder.buildClient();
});
}

private ConfigurationClientBuilder setHttpClient(HttpClient httpClient, ConfigurationClientBuilder builder) {
if (interceptorManager.isRecordMode()) {
return builder
.httpClient(buildSyncAssertingClient(httpClient));
} else if (interceptorManager.isPlaybackMode()) {
return builder
.httpClient(buildSyncAssertingClient(interceptorManager.getPlaybackClient()));
}
return builder;
}

private HttpClient buildSyncAssertingClient(HttpClient httpClient) {
//skip paging requests until #30031 resolved
BiFunction<HttpRequest, Context, Boolean> skipRequestFunction = (request, context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.rest.Response;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.logging.ClientLogger;
Expand Down Expand Up @@ -41,19 +41,18 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public abstract class ConfigurationClientTestBase extends TestBase {
public abstract class ConfigurationClientTestBase extends TestProxyTestBase {
private static final String AZURE_APPCONFIG_CONNECTION_STRING = "AZURE_APPCONFIG_CONNECTION_STRING";
private static final String KEY_PREFIX = "key";
private static final String LABEL_PREFIX = "label";
private static final int PREFIX_LENGTH = 8;
private static final int RESOURCE_LENGTH = 16;

public static final String FAKE_CONNECTION_STRING =
"Endpoint=http://localhost:8080;Id=0000000000000;Secret=fakeSecrePlaceholder";
"Endpoint=https://localhost:8080;Id=0000000000000;Secret=fakeSecrePlaceholder";
static String connectionString;

private final ClientLogger logger = new ClientLogger(ConfigurationClientTestBase.class);

String keyPrefix;
String labelPrefix;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,54 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://REDACTED.azconfig.io/kv/newKey?api-version=1.0",
"Headers" : {
"User-Agent" : "azsdk-java-azure-data-appconfiguration/1.5.0-beta.1 (11.0.10; Windows 10; 10.0)",
"x-ms-client-request-id" : "68039daf-5cbf-42c0-8f7d-1a261e67254d",
"Content-Type" : "application/json"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"Server" : "openresty/1.21.4.1",
"Access-Control-Allow-Origin" : "*",
"Connection" : "keep-alive",
"Last-Modified" : "Wed, 08 Mar 2023 22:02:54 GMT",
"retry-after" : "0",
"StatusCode" : "200",
"Date" : "Wed, 08 Mar 2023 22:02:54 GMT",
"x-ms-correlation-request-id" : "d53eb0ee-7df1-4439-ad9a-cbec9501ce12",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Access-Control-Expose-Headers" : "DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, x-ms-client-session-id, x-ms-effective-locale, WWW-Authenticate, traceparent, tracestate",
"ETag" : "\"GYH5WDQh5JsfxcRY5mVr0-wUgbyfGEUJlmdNpOXaxJE\"",
"Access-Control-Allow-Credentials" : "true",
"Sync-Token" : "zAJw6V16=MjoyMCM0MjIzMDU5;sn=4223059",
"x-ms-request-id" : "d53eb0ee-7df1-4439-ad9a-cbec9501ce12",
"Body" : "{\"etag\":\"GYH5WDQh5JsfxcRY5mVr0-wUgbyfGEUJlmdNpOXaxJE\",\"key\":\"newKey\",\"label\":null,\"content_type\":null,\"value\":\"newValue\",\"tags\":{},\"locked\":false,\"last_modified\":\"2023-03-08T22:02:54+00:00\"}",
"x-ms-client-request-id" : "68039daf-5cbf-42c0-8f7d-1a261e67254d",
"Content-Type" : "application/vnd.microsoft.appconfig.kv+json; charset=utf-8"
},
"Exception" : null
} ],
"variables" : [ ]
}
"Entries": [
{
"RequestUri": "https://REDACTED/kv/newKey?api-version=1.0",
"RequestMethod": "PUT",
"RequestHeaders": {
"Accept": "application/vnd.microsoft.azconfig.kv\u002Bjson",
"Authorization": "Sanitized",
"Content-Length": "60",
"Content-Type": "application/json",
"Date": "Wed, 05 Apr 2023 02:25:30 GMT",
"Sync-Token": "",
"User-Agent": "azsdk-java-azure-data-appconfiguration/1.5.0-beta.1 (17.0.1; Windows 11; 10.0)",
"x-ms-client-request-id": "ab86eff1-c976-4de9-8d39-47931c47b8e3",
"x-ms-return-client-request-id": "true"
},
"RequestBody": {
"key": "newKey",
"value": "newValue",
"tags": {},
"locked": false
},
"StatusCode": 200,
"ResponseHeaders": {
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "*",
"Access-Control-Expose-Headers": "DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization, x-ms-client-request-id, x-ms-useragent, x-ms-content-sha256, x-ms-date, host, Accept, Accept-Datetime, Date, If-Match, If-None-Match, Sync-Token, x-ms-return-client-request-id, ETag, Last-Modified, Link, Memento-Datetime, retry-after-ms, x-ms-request-id, x-ms-client-session-id, x-ms-effective-locale, WWW-Authenticate, traceparent, tracestate",
"Connection": "keep-alive",
"Content-Type": "application/vnd.microsoft.appconfig.kv\u002Bjson; charset=utf-8",
"Date": "Wed, 05 Apr 2023 02:25:33 GMT",
"ETag": "\u0022myvDafxHUtyNUDgFV2-lvAamc3jj4LwCAM-Ku-TJRTI\u0022",
"Last-Modified": "Wed, 05 Apr 2023 02:25:33 GMT",
"Server": "openresty/1.21.4.1",
"Strict-Transport-Security": "max-age=15724800; includeSubDomains",
"Sync-Token": "zAJw6V16=MjoyMCM0MjgwNzk5;sn=4280799",
"Transfer-Encoding": "chunked",
"x-ms-client-request-id": "ab86eff1-c976-4de9-8d39-47931c47b8e3",
"x-ms-correlation-request-id": "0da178ec-df71-41f2-bea6-d3f7554e13d5",
"x-ms-request-id": "0da178ec-df71-41f2-bea6-d3f7554e13d5"
},
"ResponseBody": {
"etag": "myvDafxHUtyNUDgFV2-lvAamc3jj4LwCAM-Ku-TJRTI",
"key": "newKey",
"label": null,
"content_type": null,
"value": "newValue",
"tags": {},
"locked": false,
"last_modified": "2023-04-05T02:25:33\u002B00:00"
}
}
],
"Variables": {}
}
Loading