3
3
4
4
package com .azure .maps .geolocation ;
5
5
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
-
13
6
import com .azure .core .credential .AzureKeyCredential ;
14
- import com .azure .core .credential .TokenCredential ;
15
7
import com .azure .core .http .HttpClient ;
16
8
import com .azure .core .http .HttpPipeline ;
17
9
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 ;
19
12
import com .azure .core .http .policy .HttpLogDetailLevel ;
20
13
import com .azure .core .http .policy .HttpLogOptions ;
14
+ import com .azure .core .http .policy .HttpLoggingPolicy ;
21
15
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 ;
22
19
import com .azure .core .http .rest .Response ;
23
20
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 ;
26
26
import com .azure .core .util .Configuration ;
27
- import com .azure .identity .EnvironmentCredentialBuilder ;
28
27
import com .azure .maps .geolocation .models .IpAddressToLocationResult ;
29
28
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
+
31
41
static final String FAKE_API_KEY = "fakeKeyPlaceholder" ;
32
42
33
- private final String endpoint = Configuration .getGlobalConfiguration ().get ("API-LEARN_ENDPOINT" );
34
- Duration durationTestMode ;
35
43
static InterceptorManager interceptorManagerTestBase ;
36
44
45
+ Duration durationTestMode ;
46
+
37
47
@ Override
38
48
protected void beforeTest () {
39
49
if (interceptorManager .isPlaybackMode ()) {
40
50
durationTestMode = Duration .ofMillis (1 );
41
51
} else {
42
52
durationTestMode = TestUtils .DEFAULT_POLL_INTERVAL ;
43
53
}
54
+
44
55
interceptorManagerTestBase = interceptorManager ;
45
56
}
46
57
47
- GeolocationClientBuilder getGeoLocationAsyncClientBuilder (HttpClient httpClient , GeolocationServiceVersion serviceVersion ) {
58
+ GeolocationClientBuilder getGeoLocationAsyncClientBuilder (HttpClient httpClient ,
59
+ GeolocationServiceVersion serviceVersion ) {
48
60
GeolocationClientBuilder builder = new GeolocationClientBuilder ()
61
+ .pipeline (getHttpPipeline (httpClient ))
49
62
.httpLogOptions (new HttpLogOptions ().setLogLevel (HttpLogDetailLevel .BODY_AND_HEADERS ))
50
63
.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" );
63
67
}
68
+
64
69
return builder ;
65
70
}
66
71
67
72
HttpPipeline getHttpPipeline (HttpClient httpClient ) {
68
- TokenCredential credential = null ;
73
+ httpClient = interceptorManager . isPlaybackMode () ? interceptorManager . getPlaybackClient () : httpClient ;
69
74
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 )));
72
79
}
73
80
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 );
77
86
}
78
87
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 ()) {
80
107
policies .add (interceptorManager .getRecordPolicy ());
81
108
}
82
109
83
- HttpPipeline pipeline = new HttpPipelineBuilder ()
110
+ return new HttpPipelineBuilder ()
84
111
.policies (policies .toArray (new HttpPipelinePolicy [0 ]))
85
- .httpClient (httpClient == null ? interceptorManager .getPlaybackClient () : httpClient )
112
+ .httpClient (interceptorManager . isPlaybackMode () ? interceptorManager .getPlaybackClient () : httpClient )
86
113
.build ();
87
-
88
- return pipeline ;
89
- }
90
-
91
- String getEndpoint () {
92
- return interceptorManager .isPlaybackMode ()
93
- ? "https://localhost:8080"
94
- : endpoint ;
95
114
}
96
115
97
116
static void validateGetLocation (IpAddressToLocationResult expected , IpAddressToLocationResult actual ) {
@@ -101,7 +120,8 @@ static void validateGetLocation(IpAddressToLocationResult expected, IpAddressToL
101
120
assertEquals (expected .getIpAddress (), actual .getIpAddress ());
102
121
}
103
122
104
- static void validateGetLocationWithResponse (IpAddressToLocationResult expected , int expectedStatusCode , Response <IpAddressToLocationResult > response ) {
123
+ static void validateGetLocationWithResponse (IpAddressToLocationResult expected , int expectedStatusCode ,
124
+ Response <IpAddressToLocationResult > response ) {
105
125
assertNotNull (response );
106
126
assertEquals (expectedStatusCode , response .getStatusCode ());
107
127
validateGetLocation (expected , response .getValue ());
0 commit comments