|
4 | 4 | package com.azure.ai.anomalydetector; |
5 | 5 |
|
6 | 6 | import com.azure.core.credential.AzureKeyCredential; |
7 | | -import com.azure.core.http.HttpClient; |
8 | | -import com.azure.core.http.HttpPipeline; |
9 | | -import com.azure.core.http.HttpPipelineBuilder; |
10 | | -import com.azure.core.http.policy.AzureKeyCredentialPolicy; |
11 | | -import com.azure.core.http.policy.HttpPipelinePolicy; |
12 | | -import com.azure.core.test.TestBase; |
13 | | -import com.azure.core.test.TestMode; |
| 7 | +import com.azure.core.test.TestProxyTestBase; |
14 | 8 | import com.azure.core.util.BinaryData; |
15 | 9 | import com.azure.core.util.Configuration; |
| 10 | + |
16 | 11 | import java.util.function.Consumer; |
17 | 12 |
|
18 | 13 |
|
19 | 14 | /** |
20 | 15 | * Base class for Anomaly Detector clients test. |
21 | 16 | */ |
22 | | -public class AnomalyDetectorClientTestBase extends TestBase { |
23 | | - private static final String FAKE_API_KEY = "fakeKeyPlaceholder"; |
24 | | - private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key"; |
25 | | - |
| 17 | +public class AnomalyDetectorClientTestBase extends TestProxyTestBase { |
26 | 18 | void testDetectEntireSeriesWithResponse(Consumer<BinaryData> testRunner) { |
27 | 19 | testRunner.accept(getDetectRequest()); |
28 | 20 | } |
29 | 21 |
|
30 | 22 | AnomalyDetectorClientBuilder getClientBuilder() { |
31 | | - String endpoint = getEndpoint(); |
| 23 | + AnomalyDetectorClientBuilder builder = new AnomalyDetectorClientBuilder() |
| 24 | + .endpoint(getEndpoint()) |
| 25 | + .credential(new AzureKeyCredential(getKey())); |
32 | 26 |
|
33 | | - HttpPipelinePolicy authPolicy = new AzureKeyCredentialPolicy(OCP_APIM_SUBSCRIPTION_KEY, |
34 | | - new AzureKeyCredential(getKey())); |
35 | | - HttpClient httpClient; |
36 | | - if (getTestMode() == TestMode.RECORD || getTestMode() == TestMode.LIVE) { |
37 | | - httpClient = HttpClient.createDefault(); |
38 | | - } else { |
39 | | - httpClient = interceptorManager.getPlaybackClient(); |
| 27 | + if (interceptorManager.isPlaybackMode()) { |
| 28 | + builder.httpClient(interceptorManager.getPlaybackClient()); |
| 29 | + } else if (interceptorManager.isRecordMode()) { |
| 30 | + builder.addPolicy(interceptorManager.getRecordPolicy()); |
40 | 31 | } |
41 | | - HttpPipeline httpPipeline = new HttpPipelineBuilder() |
42 | | - .httpClient(httpClient) |
43 | | - .policies(authPolicy, interceptorManager.getRecordPolicy()).build(); |
44 | 32 |
|
45 | | - return new AnomalyDetectorClientBuilder() |
46 | | - .pipeline(httpPipeline) |
47 | | - .endpoint(endpoint); |
| 33 | + return builder; |
48 | 34 | } |
49 | 35 |
|
50 | 36 | private String getKey() { |
51 | | - if (getTestMode() == TestMode.PLAYBACK) { |
52 | | - return FAKE_API_KEY; |
53 | | - } else { |
54 | | - return Configuration.getGlobalConfiguration().get("AZURE_ANOMALY_DETECTOR_API_KEY"); |
55 | | - } |
| 37 | + return interceptorManager.isPlaybackMode() |
| 38 | + ? "fakeKeyPlaceholder" : Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_API_KEY"); |
56 | 39 | } |
57 | 40 |
|
58 | 41 | String getEndpoint() { |
59 | 42 | return interceptorManager.isPlaybackMode() |
60 | | - ? "https://localhost:8080" |
61 | | - : Configuration.getGlobalConfiguration().get("AZURE_ANOMALY_DETECTOR_ENDPOINT"); |
| 43 | + ? "https://localhost:8080" : Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_ENDPOINT"); |
62 | 44 | } |
63 | 45 |
|
64 | 46 | private BinaryData getDetectRequest() { |
65 | | - BinaryData requestBody = BinaryData.fromString("{\"series\":[{\"timestamp\":\"2018-01-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-02-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-03-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-04-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-05-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-06-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-07-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-08-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-09-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-10-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-11-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2018-12-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-01-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-02-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-03-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-04-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-05-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-06-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-07-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-08-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-09-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-10-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-11-19T00:00:00Z\",\"value\":855},{\"timestamp\":\"2019-12-19T00:00:00Z\",\"value\":855}],\"granularity\":\"monthly\",\"maxAnomalyRatio\":0.25,\"sensitivity\":95}"); |
66 | | - return requestBody; |
| 47 | + return BinaryData.fromString("{\"series\":[{\"timestamp\":\"2018-01-19T00:00:00Z\",\"value\":855}," |
| 48 | + + "{\"timestamp\":\"2018-02-19T00:00:00Z\",\"value\":855}," |
| 49 | + + "{\"timestamp\":\"2018-03-19T00:00:00Z\",\"value\":855}," |
| 50 | + + "{\"timestamp\":\"2018-04-19T00:00:00Z\",\"value\":855}," |
| 51 | + + "{\"timestamp\":\"2018-05-19T00:00:00Z\",\"value\":855}," |
| 52 | + + "{\"timestamp\":\"2018-06-19T00:00:00Z\",\"value\":855}," |
| 53 | + + "{\"timestamp\":\"2018-07-19T00:00:00Z\",\"value\":855}," |
| 54 | + + "{\"timestamp\":\"2018-08-19T00:00:00Z\",\"value\":855}," |
| 55 | + + "{\"timestamp\":\"2018-09-19T00:00:00Z\",\"value\":855}," |
| 56 | + + "{\"timestamp\":\"2018-10-19T00:00:00Z\",\"value\":855}," |
| 57 | + + "{\"timestamp\":\"2018-11-19T00:00:00Z\",\"value\":855}," |
| 58 | + + "{\"timestamp\":\"2018-12-19T00:00:00Z\",\"value\":855}," |
| 59 | + + "{\"timestamp\":\"2019-01-19T00:00:00Z\",\"value\":855}," |
| 60 | + + "{\"timestamp\":\"2019-02-19T00:00:00Z\",\"value\":855}," |
| 61 | + + "{\"timestamp\":\"2019-03-19T00:00:00Z\",\"value\":855}," |
| 62 | + + "{\"timestamp\":\"2019-04-19T00:00:00Z\",\"value\":855}," |
| 63 | + + "{\"timestamp\":\"2019-05-19T00:00:00Z\",\"value\":855}," |
| 64 | + + "{\"timestamp\":\"2019-06-19T00:00:00Z\",\"value\":855}," |
| 65 | + + "{\"timestamp\":\"2019-07-19T00:00:00Z\",\"value\":855}," |
| 66 | + + "{\"timestamp\":\"2019-08-19T00:00:00Z\",\"value\":855}," |
| 67 | + + "{\"timestamp\":\"2019-09-19T00:00:00Z\",\"value\":855}," |
| 68 | + + "{\"timestamp\":\"2019-10-19T00:00:00Z\",\"value\":855}," |
| 69 | + + "{\"timestamp\":\"2019-11-19T00:00:00Z\",\"value\":855}," |
| 70 | + + "{\"timestamp\":\"2019-12-19T00:00:00Z\",\"value\":855}]," |
| 71 | + + "\"granularity\":\"monthly\",\"maxAnomalyRatio\":0.25,\"sensitivity\":95}"); |
67 | 72 | } |
68 | 73 | } |
0 commit comments