Skip to content

Commit 97a1769

Browse files
authored
[Test Proxy] Maps Geolocation migration (Azure#35871)
* Added exports statements to POM required for local testing. * Changed test classes to use TestProxyTestBase and refactored tests to include the user of a user agent, retry logic and better HTTP logging. * Re-recorded tests. * Migrated test recordings to assets repo. * Applied PR feedback. * Removed unused import. * Applied more PR feedback.
1 parent 3859e4e commit 97a1769

11 files changed

+87
-199
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"AssetsRepo": "Azure/azure-sdk-assets",
3+
"AssetsRepoPrefixPath": "java",
4+
"TagPrefix": "java/maps/azure-maps-geolocation",
5+
"Tag": "java/maps/azure-maps-geolocation_0f5507c629"
6+
}

sdk/maps/azure-maps-geolocation/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
<properties>
4545
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4646
<legal><![CDATA[[INFO] Any downloads listed may be third party software. Microsoft grants you no rights for third party software.]]></legal>
47+
<!-- Configures the Java 9+ run to perform the required module exports, opens, and reads that are necessary for testing but shouldn't be part of the module-info. -->
48+
<javaModulesSurefireArgLine>
49+
--add-exports com.azure.core/com.azure.core.implementation.http=ALL-UNNAMED
50+
--add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED
51+
</javaModulesSurefireArgLine>
4752
</properties>
4853

4954
<dependencies>

sdk/maps/azure-maps-geolocation/src/main/java/com/azure/maps/geolocation/GeolocationClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ public final class GeolocationClient {
3535
/**
3636
* Initializes an instance of GeolocationClient client.
3737
*
38-
* @param serviceClient the service client implementation.
38+
* @param asyncClient the service client implementation.
3939
*/
4040
GeolocationClient(GeolocationAsyncClient asyncClient) {
4141
this.asyncClient = asyncClient;
4242
}
4343

4444
/**
4545
* Get Location
46-
*
46+
*
4747
* <!-- src_embed com.azure.maps.geolocation.sync.get_ip_to_location -->
4848
* <pre>
4949
* client.getLocation&#40;&quot;131.107.0.89&quot;&#41;;
5050
* </pre>
5151
* <!-- end com.azure.maps.geolocation.sync.get_ip_to_location -->
52-
*
52+
*
5353
* **Applies to**: S0 and S1 pricing tiers.
5454
*
5555
* <p>This service will return the ISO country code for the provided IP address. Developers can use this information
@@ -68,13 +68,13 @@ public IpAddressToLocationResult getLocation(String ipAddress) {
6868

6969
/**
7070
* Get Location
71-
*
71+
*
7272
* <!-- src_embed com.azure.maps.geolocation.sync.get_ip_to_location -->
7373
* <pre>
7474
* client.getLocation&#40;&quot;131.107.0.89&quot;&#41;;
7575
* </pre>
7676
* <!-- end com.azure.maps.geolocation.sync.get_ip_to_location -->
77-
*
77+
*
7878
* **Applies to**: S0 and S1 pricing tiers.
7979
*
8080
* <p>This service will return the ISO country code for the provided IP address. Developers can use this information

sdk/maps/azure-maps-geolocation/src/main/java/com/azure/maps/geolocation/GeolocationClientBuilder.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ public final class GeolocationClientBuilder implements AzureKeyCredentialTrait<G
6767
// constants
6868
private static final String SDK_NAME = "name";
6969
private static final String SDK_VERSION = "version";
70-
private static final String GEOLOCATION_SUBSCRIPTION_KEY = "subscription-key";
7170
private static final String X_MS_CLIENT_ID = "x-ms-client-id";
72-
//subscription-key
73-
74-
// instance fields
7571
private static final ClientLogger LOGGER = new ClientLogger(GeolocationClientBuilder.class);
7672
private final Map<String, String> properties = new HashMap<>();
73+
74+
// subscription-key
75+
static final String GEOLOCATION_SUBSCRIPTION_KEY = "subscription-key";
76+
77+
// instance fields
7778
private String endpoint;
7879
private GeolocationServiceVersion serviceVersion;
7980
/*

sdk/maps/azure-maps-geolocation/src/test/java/com/azure/maps/geolocation/GeolocationClientTestBase.java

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,95 +3,114 @@
33

44
package com.azure.maps.geolocation;
55

6-
import static org.junit.jupiter.api.Assertions.assertEquals;
7-
import static org.junit.jupiter.api.Assertions.assertNotNull;
8-
9-
import java.time.Duration;
10-
import java.util.ArrayList;
11-
import java.util.List;
12-
136
import com.azure.core.credential.AzureKeyCredential;
14-
import com.azure.core.credential.TokenCredential;
157
import com.azure.core.http.HttpClient;
168
import com.azure.core.http.HttpPipeline;
179
import com.azure.core.http.HttpPipelineBuilder;
18-
import com.azure.core.http.policy.BearerTokenAuthenticationPolicy;
10+
import com.azure.core.http.policy.AzureKeyCredentialPolicy;
11+
import com.azure.core.http.policy.ExponentialBackoff;
1912
import com.azure.core.http.policy.HttpLogDetailLevel;
2013
import com.azure.core.http.policy.HttpLogOptions;
14+
import com.azure.core.http.policy.HttpLoggingPolicy;
2115
import com.azure.core.http.policy.HttpPipelinePolicy;
16+
import com.azure.core.http.policy.HttpPolicyProviders;
17+
import com.azure.core.http.policy.RetryPolicy;
18+
import com.azure.core.http.policy.UserAgentPolicy;
2219
import com.azure.core.http.rest.Response;
2320
import com.azure.core.test.InterceptorManager;
24-
import com.azure.core.test.TestBase;
25-
import com.azure.core.test.TestMode;
21+
import com.azure.core.test.TestProxyTestBase;
22+
import com.azure.core.test.models.CustomMatcher;
23+
import com.azure.core.test.models.TestProxyRequestMatcher;
24+
import com.azure.core.test.models.TestProxySanitizer;
25+
import com.azure.core.test.models.TestProxySanitizerType;
2626
import com.azure.core.util.Configuration;
27-
import com.azure.identity.EnvironmentCredentialBuilder;
2827
import com.azure.maps.geolocation.models.IpAddressToLocationResult;
2928

30-
public class GeolocationClientTestBase extends TestBase {
29+
import java.time.Duration;
30+
import java.util.ArrayList;
31+
import java.util.Collections;
32+
import java.util.List;
33+
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
37+
public class GeolocationClientTestBase extends TestProxyTestBase {
38+
private static final String SDK_NAME = "client_name";
39+
private static final String SDK_VERSION = "client_version";
40+
3141
static final String FAKE_API_KEY = "fakeKeyPlaceholder";
3242

33-
private final String endpoint = Configuration.getGlobalConfiguration().get("API-LEARN_ENDPOINT");
34-
Duration durationTestMode;
3543
static InterceptorManager interceptorManagerTestBase;
3644

45+
Duration durationTestMode;
46+
3747
@Override
3848
protected void beforeTest() {
3949
if (interceptorManager.isPlaybackMode()) {
4050
durationTestMode = Duration.ofMillis(1);
4151
} else {
4252
durationTestMode = TestUtils.DEFAULT_POLL_INTERVAL;
4353
}
54+
4455
interceptorManagerTestBase = interceptorManager;
4556
}
4657

47-
GeolocationClientBuilder getGeoLocationAsyncClientBuilder(HttpClient httpClient, GeolocationServiceVersion serviceVersion) {
58+
GeolocationClientBuilder getGeoLocationAsyncClientBuilder(HttpClient httpClient,
59+
GeolocationServiceVersion serviceVersion) {
4860
GeolocationClientBuilder builder = new GeolocationClientBuilder()
61+
.pipeline(getHttpPipeline(httpClient))
4962
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
5063
.serviceVersion(serviceVersion);
51-
String endpoint = getEndpoint();
52-
if (getEndpoint() != null) {
53-
builder.endpoint(endpoint);
54-
}
55-
if (getTestMode() == TestMode.RECORD) {
56-
builder.addPolicy(interceptorManager.getRecordPolicy());
57-
}
58-
if (getTestMode() == TestMode.PLAYBACK) {
59-
builder.credential(new AzureKeyCredential(FAKE_API_KEY)).httpClient(interceptorManager.getPlaybackClient());
60-
} else {
61-
builder.credential((new AzureKeyCredential(
62-
Configuration.getGlobalConfiguration().get("SUBSCRIPTION_KEY"))));
64+
65+
if (interceptorManager.isPlaybackMode()) {
66+
builder.endpoint("https://localhost:8080");
6367
}
68+
6469
return builder;
6570
}
6671

6772
HttpPipeline getHttpPipeline(HttpClient httpClient) {
68-
TokenCredential credential = null;
73+
httpClient = interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient;
6974

70-
if (!interceptorManager.isPlaybackMode()) {
71-
credential = new EnvironmentCredentialBuilder().httpClient(httpClient).build();
75+
if (interceptorManager.isRecordMode()) {
76+
interceptorManager.addSanitizers(
77+
Collections.singletonList(
78+
new TestProxySanitizer("subscription-key", ".+", "REDACTED", TestProxySanitizerType.HEADER)));
7279
}
7380

74-
final List<HttpPipelinePolicy> policies = new ArrayList<>();
75-
if (credential != null) {
76-
policies.add(new BearerTokenAuthenticationPolicy(credential, endpoint.replaceFirst("/$", "") + "/.default"));
81+
if (interceptorManager.isPlaybackMode()) {
82+
List<TestProxyRequestMatcher> customMatchers = new ArrayList<>();
83+
84+
customMatchers.add(new CustomMatcher().setHeadersKeyOnlyMatch(Collections.singletonList("subscription-key")));
85+
interceptorManager.addMatchers(customMatchers);
7786
}
7887

79-
if (getTestMode() == TestMode.RECORD) {
88+
final List<HttpPipelinePolicy> policies = new ArrayList<>();
89+
90+
policies.add(new UserAgentPolicy(null, SDK_NAME, SDK_VERSION, Configuration.getGlobalConfiguration().clone()));
91+
92+
HttpPolicyProviders.addBeforeRetryPolicies(policies);
93+
94+
policies.add(new RetryPolicy(new ExponentialBackoff(5, Duration.ofSeconds(2), Duration.ofSeconds(16))));
95+
policies.add(
96+
new AzureKeyCredentialPolicy(
97+
GeolocationClientBuilder.GEOLOCATION_SUBSCRIPTION_KEY,
98+
new AzureKeyCredential(interceptorManager.isPlaybackMode()
99+
? FAKE_API_KEY
100+
: Configuration.getGlobalConfiguration().get("SUBSCRIPTION_KEY"))));
101+
102+
HttpPolicyProviders.addAfterRetryPolicies(policies);
103+
104+
policies.add(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)));
105+
106+
if (interceptorManager.isRecordMode()) {
80107
policies.add(interceptorManager.getRecordPolicy());
81108
}
82109

83-
HttpPipeline pipeline = new HttpPipelineBuilder()
110+
return new HttpPipelineBuilder()
84111
.policies(policies.toArray(new HttpPipelinePolicy[0]))
85-
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
112+
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
86113
.build();
87-
88-
return pipeline;
89-
}
90-
91-
String getEndpoint() {
92-
return interceptorManager.isPlaybackMode()
93-
? "https://localhost:8080"
94-
: endpoint;
95114
}
96115

97116
static void validateGetLocation(IpAddressToLocationResult expected, IpAddressToLocationResult actual) {
@@ -101,7 +120,8 @@ static void validateGetLocation(IpAddressToLocationResult expected, IpAddressToL
101120
assertEquals(expected.getIpAddress(), actual.getIpAddress());
102121
}
103122

104-
static void validateGetLocationWithResponse(IpAddressToLocationResult expected, int expectedStatusCode, Response<IpAddressToLocationResult> response) {
123+
static void validateGetLocationWithResponse(IpAddressToLocationResult expected, int expectedStatusCode,
124+
Response<IpAddressToLocationResult> response) {
105125
assertNotNull(response);
106126
assertEquals(expectedStatusCode, response.getStatusCode());
107127
validateGetLocation(expected, response.getValue());

sdk/maps/azure-maps-geolocation/src/test/resources/session-records/GeolocationAsyncClientTest.testAsyncGetLocationWithResponse[1].json

Lines changed: 0 additions & 24 deletions
This file was deleted.

sdk/maps/azure-maps-geolocation/src/test/resources/session-records/GeolocationAsyncClientTest.testAsyncGetLocation[1].json

Lines changed: 0 additions & 24 deletions
This file was deleted.

sdk/maps/azure-maps-geolocation/src/test/resources/session-records/GeolocationAsyncClientTest.testAsyncInvalidGetDataForPointsWithResponse[1].json

Lines changed: 0 additions & 24 deletions
This file was deleted.

sdk/maps/azure-maps-geolocation/src/test/resources/session-records/GeolocationClientTest.testGetLocationWithResponse[1].json

Lines changed: 0 additions & 24 deletions
This file was deleted.

sdk/maps/azure-maps-geolocation/src/test/resources/session-records/GeolocationClientTest.testGetLocation[1].json

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)