Skip to content

Commit

Permalink
Transition Anomaly Detector to Test Proxy (#35617)
Browse files Browse the repository at this point in the history
  • Loading branch information
alzimmermsft committed Jun 26, 2023
1 parent 106a7a6 commit 1865f1d
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 215 deletions.
5 changes: 5 additions & 0 deletions eng/scripts/TypeSpec-Compare-CurrentToCodegeneration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ $status
"
exit $LASTEXITCODE
}

# Delete out TypeSpec temporary folders if they still exist.
Get-ChildItem -Path $Directory -Filter TempTypeSpecFiles -Recurse -Directory | ForEach-Object {
Remove-Item -Path $_.FullName -Recurse -Force
}
6 changes: 6 additions & 0 deletions sdk/anomalydetector/azure-ai-anomalydetector/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/anomalydetector/azure-ai-anomalydetector",
"Tag": "java/anomalydetector/azure-ai-anomalydetector_24918ac6aa"
}
4 changes: 2 additions & 2 deletions sdk/anomalydetector/azure-ai-anomalydetector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
</developers>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jacoco.min.linecoverage>0.2</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.2</jacoco.min.branchcoverage>
<jacoco.min.linecoverage>0.1</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.1</jacoco.min.branchcoverage>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@

package com.azure.ai.anomalydetector;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import com.azure.core.http.rest.RequestOptions;
import com.azure.core.http.rest.Response;
import com.azure.core.util.BinaryData;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.logging.LogLevel;
import org.junit.jupiter.api.Test;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import java.io.StringReader;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

/**
* Unit tests for {@link AnomalyDetectorClient}.
*/
public class AnomalyDetectorClientTest extends AnomalyDetectorClientTestBase {
private static final ClientLogger LOGGER = new ClientLogger(AnomalyDetectorClientTest.class);

private AnomalyDetectorClient getClient() {
return getClientBuilder().buildClient();
Expand All @@ -28,9 +31,9 @@ private AnomalyDetectorClient getClient() {
@Test
public void testDetect() {
testDetectEntireSeriesWithResponse(request -> {

Response<BinaryData> response = getClient().detectUnivariateEntireSeriesWithResponse(request, new RequestOptions());
System.out.println(response.toString());
Response<BinaryData> response = getClient().detectUnivariateEntireSeriesWithResponse(request,
new RequestOptions());
LOGGER.log(LogLevel.INFORMATIONAL, response::toString);

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

});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,70 @@
package com.azure.ai.anomalydetector;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.AzureKeyCredentialPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.util.BinaryData;
import com.azure.core.util.Configuration;

import java.util.function.Consumer;


/**
* Base class for Anomaly Detector clients test.
*/
public class AnomalyDetectorClientTestBase extends TestBase {
private static final String FAKE_API_KEY = "fakeKeyPlaceholder";
private static final String OCP_APIM_SUBSCRIPTION_KEY = "Ocp-Apim-Subscription-Key";

public class AnomalyDetectorClientTestBase extends TestProxyTestBase {
void testDetectEntireSeriesWithResponse(Consumer<BinaryData> testRunner) {
testRunner.accept(getDetectRequest());
}

AnomalyDetectorClientBuilder getClientBuilder() {
String endpoint = getEndpoint();
AnomalyDetectorClientBuilder builder = new AnomalyDetectorClientBuilder()
.endpoint(getEndpoint())
.credential(new AzureKeyCredential(getKey()));

HttpPipelinePolicy authPolicy = new AzureKeyCredentialPolicy(OCP_APIM_SUBSCRIPTION_KEY,
new AzureKeyCredential(getKey()));
HttpClient httpClient;
if (getTestMode() == TestMode.RECORD || getTestMode() == TestMode.LIVE) {
httpClient = HttpClient.createDefault();
} else {
httpClient = interceptorManager.getPlaybackClient();
if (interceptorManager.isPlaybackMode()) {
builder.httpClient(interceptorManager.getPlaybackClient());
} else if (interceptorManager.isRecordMode()) {
builder.addPolicy(interceptorManager.getRecordPolicy());
}
HttpPipeline httpPipeline = new HttpPipelineBuilder()
.httpClient(httpClient)
.policies(authPolicy, interceptorManager.getRecordPolicy()).build();

return new AnomalyDetectorClientBuilder()
.pipeline(httpPipeline)
.endpoint(endpoint);
return builder;
}

private String getKey() {
if (getTestMode() == TestMode.PLAYBACK) {
return FAKE_API_KEY;
} else {
return Configuration.getGlobalConfiguration().get("AZURE_ANOMALY_DETECTOR_API_KEY");
}
return interceptorManager.isPlaybackMode()
? "fakeKeyPlaceholder" : Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_API_KEY");
}

String getEndpoint() {
return interceptorManager.isPlaybackMode()
? "https://localhost:8080"
: Configuration.getGlobalConfiguration().get("AZURE_ANOMALY_DETECTOR_ENDPOINT");
? "https://localhost:8080" : Configuration.getGlobalConfiguration().get("ANOMALY_DETECTOR_ENDPOINT");
}

private BinaryData getDetectRequest() {
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}");
return requestBody;
return 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}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@

import com.azure.ai.anomalydetector.models.AnomalyDetectionModel;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.util.logging.ClientLogger;
import com.azure.core.util.logging.LogLevel;
import org.junit.jupiter.api.Test;


public class MultivariateAnomalyDetectorClientTest extends AnomalyDetectorClientTestBase {
private static final ClientLogger LOGGER = new ClientLogger(MultivariateAnomalyDetectorClientTest.class);
private AnomalyDetectorClient getClient() {
return getClientBuilder().buildClient();
}

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

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

@Test
Expand All @@ -32,7 +31,6 @@ public void testDetect() {
Integer skip = 0;
Integer top = 5;
getModelList(client, skip, top);

});

}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 1865f1d

Please sign in to comment.