Skip to content

Commit 1865f1d

Browse files
authored
Transition Anomaly Detector to Test Proxy (Azure#35617)
1 parent 106a7a6 commit 1865f1d

File tree

10 files changed

+164
-215
lines changed

10 files changed

+164
-215
lines changed

eng/scripts/TypeSpec-Compare-CurrentToCodegeneration.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,8 @@ $status
5353
"
5454
exit $LASTEXITCODE
5555
}
56+
57+
# Delete out TypeSpec temporary folders if they still exist.
58+
Get-ChildItem -Path $Directory -Filter TempTypeSpecFiles -Recurse -Directory | ForEach-Object {
59+
Remove-Item -Path $_.FullName -Recurse -Force
60+
}
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/anomalydetector/azure-ai-anomalydetector",
5+
"Tag": "java/anomalydetector/azure-ai-anomalydetector_24918ac6aa"
6+
}

sdk/anomalydetector/azure-ai-anomalydetector/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
</developers>
3939
<properties>
4040
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
41-
<jacoco.min.linecoverage>0.2</jacoco.min.linecoverage>
42-
<jacoco.min.branchcoverage>0.2</jacoco.min.branchcoverage>
41+
<jacoco.min.linecoverage>0.1</jacoco.min.linecoverage>
42+
<jacoco.min.branchcoverage>0.1</jacoco.min.branchcoverage>
4343
</properties>
4444
<dependencies>
4545
<dependency>

sdk/anomalydetector/azure-ai-anomalydetector/src/test/java/com/azure/ai/anomalydetector/AnomalyDetectorClientTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@
33

44
package com.azure.ai.anomalydetector;
55

6-
import static org.junit.jupiter.api.Assertions.assertEquals;
7-
import static org.junit.jupiter.api.Assertions.assertFalse;
8-
96
import com.azure.core.http.rest.RequestOptions;
107
import com.azure.core.http.rest.Response;
118
import com.azure.core.util.BinaryData;
9+
import com.azure.core.util.logging.ClientLogger;
10+
import com.azure.core.util.logging.LogLevel;
1211
import org.junit.jupiter.api.Test;
1312

1413
import javax.json.Json;
1514
import javax.json.JsonArray;
1615
import javax.json.JsonObject;
1716
import java.io.StringReader;
1817

18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
1921
/**
2022
* Unit tests for {@link AnomalyDetectorClient}.
2123
*/
2224
public class AnomalyDetectorClientTest extends AnomalyDetectorClientTestBase {
25+
private static final ClientLogger LOGGER = new ClientLogger(AnomalyDetectorClientTest.class);
2326

2427
private AnomalyDetectorClient getClient() {
2528
return getClientBuilder().buildClient();
@@ -28,9 +31,9 @@ private AnomalyDetectorClient getClient() {
2831
@Test
2932
public void testDetect() {
3033
testDetectEntireSeriesWithResponse(request -> {
31-
32-
Response<BinaryData> response = getClient().detectUnivariateEntireSeriesWithResponse(request, new RequestOptions());
33-
System.out.println(response.toString());
34+
Response<BinaryData> response = getClient().detectUnivariateEntireSeriesWithResponse(request,
35+
new RequestOptions());
36+
LOGGER.log(LogLevel.INFORMATIONAL, response::toString);
3437

3538
String responseBodyStr = response.getValue().toString();
3639
JsonObject responseJsonObject = Json.createReader(new StringReader(responseBodyStr)).readObject();
@@ -53,6 +56,5 @@ public void testDetect() {
5356
}
5457

5558
});
56-
5759
}
5860
}

sdk/anomalydetector/azure-ai-anomalydetector/src/test/java/com/azure/ai/anomalydetector/AnomalyDetectorClientTestBase.java

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,70 @@
44
package com.azure.ai.anomalydetector;
55

66
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;
148
import com.azure.core.util.BinaryData;
159
import com.azure.core.util.Configuration;
10+
1611
import java.util.function.Consumer;
1712

1813

1914
/**
2015
* Base class for Anomaly Detector clients test.
2116
*/
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 {
2618
void testDetectEntireSeriesWithResponse(Consumer<BinaryData> testRunner) {
2719
testRunner.accept(getDetectRequest());
2820
}
2921

3022
AnomalyDetectorClientBuilder getClientBuilder() {
31-
String endpoint = getEndpoint();
23+
AnomalyDetectorClientBuilder builder = new AnomalyDetectorClientBuilder()
24+
.endpoint(getEndpoint())
25+
.credential(new AzureKeyCredential(getKey()));
3226

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());
4031
}
41-
HttpPipeline httpPipeline = new HttpPipelineBuilder()
42-
.httpClient(httpClient)
43-
.policies(authPolicy, interceptorManager.getRecordPolicy()).build();
4432

45-
return new AnomalyDetectorClientBuilder()
46-
.pipeline(httpPipeline)
47-
.endpoint(endpoint);
33+
return builder;
4834
}
4935

5036
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");
5639
}
5740

5841
String getEndpoint() {
5942
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");
6244
}
6345

6446
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}");
6772
}
6873
}

sdk/anomalydetector/azure-ai-anomalydetector/src/test/java/com/azure/ai/anomalydetector/MultivariateAnomalyDetectorClientTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,22 @@
55

66
import com.azure.ai.anomalydetector.models.AnomalyDetectionModel;
77
import com.azure.core.http.rest.PagedIterable;
8+
import com.azure.core.util.logging.ClientLogger;
9+
import com.azure.core.util.logging.LogLevel;
810
import org.junit.jupiter.api.Test;
911

1012

1113
public class MultivariateAnomalyDetectorClientTest extends AnomalyDetectorClientTestBase {
14+
private static final ClientLogger LOGGER = new ClientLogger(MultivariateAnomalyDetectorClientTest.class);
1215
private AnomalyDetectorClient getClient() {
1316
return getClientBuilder().buildClient();
1417
}
1518

1619
private static void getModelList(AnomalyDetectorClient client, Integer skip, Integer top) {
1720
PagedIterable<AnomalyDetectionModel> response = client.listMultivariateModels(skip, top);
1821

19-
System.out.println("ModelList: ");
20-
response.streamByPage().forEach(models -> {
21-
for (AnomalyDetectionModel item : models.getValue()) {
22-
System.out.println("\t" + item.getModelId());
23-
}
24-
});
22+
LOGGER.info("ModelList: ");
23+
response.forEach(model -> LOGGER.log(LogLevel.INFORMATIONAL, () -> "\t" + model.getModelId()));
2524
}
2625

2726
@Test
@@ -32,7 +31,6 @@ public void testDetect() {
3231
Integer skip = 0;
3332
Integer top = 5;
3433
getModelList(client, skip, top);
35-
3634
});
3735

3836
}

sdk/anomalydetector/azure-ai-anomalydetector/src/test/resources/session-records/AnomalyDetectorClientTest.testDetect.json

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

sdk/anomalydetector/azure-ai-anomalydetector/src/test/resources/session-records/MultivariateAnomalyDetectorClientTest.testDetect.json

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

sdk/anomalydetector/azure-ai-anomalydetector/src/test/resources/session-records/testDetect.json

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

0 commit comments

Comments
 (0)