Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
leareai committed May 6, 2024
1 parent 82eb468 commit 88ea2ed
Show file tree
Hide file tree
Showing 28 changed files with 1,215 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.vision.face.tests;

import com.azure.ai.vision.face.FaceAsyncClient;
import com.azure.ai.vision.face.FaceClient;
import com.azure.ai.vision.face.FaceServiceVersion;
import com.azure.ai.vision.face.samples.utils.Resources;
import com.azure.ai.vision.face.models.FaceDetectionModel;
import com.azure.ai.vision.face.models.FaceDetectionResult;
import com.azure.ai.vision.face.models.FaceRecognitionModel;
import com.azure.core.http.HttpClient;
import com.azure.core.util.BinaryData;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.file.FileSystems;
import java.util.List;

import static com.azure.ai.vision.face.tests.utils.TestUtils.DISPLAY_NAME_WITH_ARGUMENTS;

public class ClientTest extends FaceClientTestBase {

@ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS)
@MethodSource("com.azure.ai.vision.face.tests.utils.TestUtils#getHttpClientsAndServiceVersions")
public void testFaceClient(HttpClient httpClient, FaceServiceVersion serviceVersion) {
FaceClient faceClient = createClient(FaceClient.class,
buildSyncAssertingClient(httpClient),
serviceVersion);

BinaryData imageBinary = BinaryData.fromFile(FileSystems.getDefault().getPath(Resources.TEST_IMAGE_PATH_FAMILY1_DAD1));
List<FaceDetectionResult> detectResult = faceClient.detect(
imageBinary,
FaceDetectionModel.DETECTION_03,
FaceRecognitionModel.RECOGNITION_04,
false);

Assertions.assertEquals(detectResult.size(), 1);
}

@ParameterizedTest(name = DISPLAY_NAME_WITH_ARGUMENTS)
@MethodSource("com.azure.ai.vision.face.tests.utils.TestUtils#getHttpClientsAndServiceVersions")
public void testFaceAsyncClient(HttpClient httpClient, FaceServiceVersion serviceVersion) {
FaceAsyncClient faceClient = createClient(
FaceAsyncClient.class, buildAsyncAssertingClient(httpClient), serviceVersion);

BinaryData imageBinary = BinaryData.fromFile(FileSystems.getDefault().getPath(Resources.TEST_IMAGE_PATH_FAMILY1_DAD1));
List<FaceDetectionResult> detectResult = faceClient.detect(
imageBinary,
FaceDetectionModel.DETECTION_03,
FaceRecognitionModel.RECOGNITION_04,
false).block();

Assertions.assertNotNull(detectResult);
Assertions.assertEquals(detectResult.size(), 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.vision.face.tests;

import com.azure.ai.vision.face.FaceAsyncClient;
import com.azure.ai.vision.face.FaceClient;
import com.azure.ai.vision.face.FaceServiceVersion;
import com.azure.ai.vision.face.models.FaceDetectionModel;
import com.azure.ai.vision.face.models.FaceDetectionResult;
import com.azure.ai.vision.face.models.FaceRecognitionModel;
import com.azure.ai.vision.face.samples.utils.Resources;
import com.azure.ai.vision.face.tests.commands.detection.DetectFunctionProvider;
import com.azure.ai.vision.face.tests.commands.detection.DetectSyncFunction;
import com.azure.ai.vision.face.tests.utils.FaceDisplayNameGenerator;
import com.azure.ai.vision.face.tests.utils.TestUtils;
import com.azure.core.http.HttpClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.List;
import java.util.stream.Stream;

@DisplayNameGeneration(FaceDisplayNameGenerator.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class DetectionTest extends FaceClientTestBase {
private static DetectSyncFunction[] sAllSyncFunction;

@BeforeAll
public void createClients() {
FaceClient faceClient = createClient(FaceClient.class);
FaceAsyncClient faceAsyncClient = createClient((FaceAsyncClient.class));
sAllSyncFunction = DetectFunctionProvider.getAllFunctions(
faceClient, faceAsyncClient, Resources.TEST_IMAGE_PATH_FAMILY1_DAD1, Resources.TEST_IMAGE_URL_DETECT_SAMPLE);
}

@ParameterizedTest
@MethodSource("getDataFortTestDetectFaceReturnFaceIdAndReturnRecognitionModel")
public void testDetectFaceReturnFaceIdAndReturnRecognitionModel(
boolean returnFaceId, boolean returnRecognitionModel, DetectSyncFunction detectionFunction) {
FaceRecognitionModel recognitionModel = FaceRecognitionModel.RECOGNITION_04;
List<FaceDetectionResult> detectResults = detectionFunction.execute(
FaceDetectionModel.DETECTION_03, recognitionModel, returnFaceId, null,
null, returnRecognitionModel, null);

Assertions.assertNotNull(detectResults);

Assertions.assertEquals(detectResults.size(), 1);
FaceDetectionResult result = detectResults.get(0);

Assertions.assertEquals(returnFaceId, result.getFaceId() != null);

FaceRecognitionModel expectReturnedModel = (returnFaceId && returnRecognitionModel) ? recognitionModel : null;
Assertions.assertEquals(expectReturnedModel, result.getRecognitionModel());
}

private Stream<Arguments> getDataFortTestDetectFaceReturnFaceIdAndReturnRecognitionModel() {
Boolean[] booleanArray = {true, false};
return TestUtils.createCombination(
booleanArray, booleanArray, sAllSyncFunction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.vision.face.tests;

import com.azure.ai.vision.face.*;
import com.azure.ai.vision.face.samples.utils.ConfigurationHelper;
import com.azure.core.client.traits.EndpointTrait;
import com.azure.core.client.traits.HttpTrait;
import com.azure.core.client.traits.KeyCredentialTrait;
import com.azure.core.client.traits.TokenCredentialTrait;
import com.azure.core.credential.KeyCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.ExponentialBackoffOptions;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryOptions;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.http.AssertingHttpClientBuilder;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;

import java.time.Duration;
import java.util.HashMap;

public class FaceClientTestBase extends TestProxyTestBase {
private static final HashMap<Class<?>, Function<FaceClientTestBase, HttpClient, FaceServiceVersion, Object>> TYPE_MAP = new HashMap<Class<?>, Function<FaceClientTestBase, HttpClient, FaceServiceVersion, Object>>() {{
put(FaceClient.class, (testBase, httpClient, serviceVersion) ->
testBase.getFaceClientBuilder(serviceVersion, httpClient).buildClient());
put(FaceAsyncClient.class, (testBase, httpClient, serviceVersion) ->
testBase.getFaceClientBuilder(serviceVersion, httpClient).buildAsyncClient());
put(FaceAdministrationClient.class, (testBase, httpClient, serviceVersion) ->
testBase.getFaceAdministrationClientBuilder(serviceVersion, httpClient).buildClient());
put(FaceAdministrationAsyncClient.class, (testBase, httpClient, serviceVersion) ->
testBase.getFaceAdministrationClientBuilder(serviceVersion, httpClient).buildAsyncClient());
put(FaceSessionClient.class, (testBase, httpClient, serviceVersion) ->
testBase.getFaceSessionClientBuilder(serviceVersion, httpClient).buildClient());
put(FaceSessionAsyncClient.class, (testBase, httpClient, serviceVersion) ->
testBase.getFaceSessionClientBuilder(serviceVersion, httpClient).buildAsyncClient());
}};

protected <T> T createClient(Class<T> clazz) {
return createClient(clazz, null, null);
}

@SuppressWarnings("unchecked")
protected <T> T createClient(Class<T> clazz, HttpClient httpClient, FaceServiceVersion serviceVersion) {
Function<FaceClientTestBase, HttpClient, FaceServiceVersion, Object> creator = TYPE_MAP.get(clazz);
if (null == creator) {
throw new IllegalArgumentException("No such client type: " + clazz);
}

return (T) creator.apply(this, httpClient, serviceVersion);
}

protected HttpClient buildSyncAssertingClient(HttpClient httpClient) {
return new AssertingHttpClientBuilder(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.skipRequest((ignored1, ignored2) -> false)
.assertSync()
.build();
}

protected HttpClient buildAsyncAssertingClient(HttpClient httpClient) {
return new AssertingHttpClientBuilder(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.skipRequest((ignored1, ignored2) -> false)
.assertAsync()
.build();
}

private FaceClientBuilder getFaceClientBuilder(FaceServiceVersion serviceVersion, HttpClient httpClient) {
return this.configureBuilder(new FaceClientBuilder().serviceVersion(serviceVersion), httpClient);
}

private FaceAdministrationClientBuilder getFaceAdministrationClientBuilder(FaceServiceVersion serviceVersion, HttpClient httpClient) {
return this.configureBuilder(new FaceAdministrationClientBuilder().serviceVersion(serviceVersion), httpClient);
}

private FaceSessionClientBuilder getFaceSessionClientBuilder(FaceServiceVersion serviceVersion, HttpClient httpClient) {
return this.configureBuilder(new FaceSessionClientBuilder().serviceVersion(serviceVersion), httpClient);
}

private <T extends HttpTrait<T> & TokenCredentialTrait<T> & KeyCredentialTrait<T> & EndpointTrait<T>> T configureBuilder(
T clientBuilder, HttpClient httpClient) {

clientBuilder.endpoint(ConfigurationHelper.getEndpoint())
.httpClient(httpClient != null ? httpClient : HttpClient.createDefault())
.retryOptions(new RetryOptions(new ExponentialBackoffOptions()
.setBaseDelay(Duration.ofMillis(1000))
.setMaxDelay(Duration.ofSeconds(1))
.setMaxRetries(10)))
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BASIC));

switch (getTestMode()) {
case PLAYBACK:
return configureForPlayBackMode(clientBuilder);
case RECORD:
return configureForRecordMode(clientBuilder);
case LIVE:
return configureForLiveMode(clientBuilder);
default:
throw new IllegalStateException("Incorrect test mode:" + getTestMode());
}
}

private <T extends HttpTrait<T> & TokenCredentialTrait<T> & KeyCredentialTrait<T>> T configureCredential(T clientBuilder) {
String accountKey = ConfigurationHelper.getAccountKey();
if (accountKey != null && !accountKey.isEmpty()) {
return clientBuilder.credential(new KeyCredential(accountKey));
}

return clientBuilder.credential(new DefaultAzureCredentialBuilder().build());
}

private <T extends HttpTrait<T> & TokenCredentialTrait<T> & KeyCredentialTrait<T>> T configureForPlayBackMode(T clientBuilder) {
return clientBuilder
.httpClient(interceptorManager.getPlaybackClient())
.credential(new MockTokenCredential());
}

private <T extends HttpTrait<T> & TokenCredentialTrait<T> & KeyCredentialTrait<T>> T configureForRecordMode(T clientBuilder) {
return configureCredential(clientBuilder)
.addPolicy(interceptorManager.getRecordPolicy());
}

private <T extends HttpTrait<T> & TokenCredentialTrait<T> & KeyCredentialTrait<T>> T configureForLiveMode(T clientBuilder) {
return configureCredential(clientBuilder);
}

@FunctionalInterface
interface Function<T, U, V, R> {
R apply(T t, U u, V v);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.ai.vision.face.tests;

import com.azure.ai.vision.face.FaceSessionAsyncClient;
import com.azure.ai.vision.face.FaceSessionClient;
import com.azure.ai.vision.face.models.CreateLivenessSessionContent;
import com.azure.ai.vision.face.models.CreateLivenessSessionResult;
import com.azure.ai.vision.face.models.LivenessOperationMode;
import com.azure.ai.vision.face.models.LivenessSession;
import com.azure.ai.vision.face.tests.commands.liveness.ILivenessSessionSyncCommands;
import com.azure.ai.vision.face.tests.commands.liveness.LivenessSessionCommandsProvider;
import com.azure.ai.vision.face.tests.utils.FaceDisplayNameGenerator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.UUID;
import java.util.stream.Stream;

import static com.azure.ai.vision.face.samples.utils.Utils.log;

@DisplayNameGeneration(FaceDisplayNameGenerator.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class LivenessSessionTest extends FaceClientTestBase {
private ILivenessSessionSyncCommands[] mLivenessCommands;
private ILivenessSessionSyncCommands mCurrentCommand;
private String mSessionId;

@BeforeAll
public void createClients() {
FaceSessionClient syncClient = createClient((FaceSessionClient.class));
FaceSessionAsyncClient asyncClient = createClient((FaceSessionAsyncClient.class));
mLivenessCommands = LivenessSessionCommandsProvider.getCommands(syncClient, asyncClient);
}

@ParameterizedTest
@MethodSource("getTestCommands")
public void testCreateSession(ILivenessSessionSyncCommands livenessCommands) {
String uuid = UUID.randomUUID().toString();
CreateLivenessSessionContent content = new CreateLivenessSessionContent(LivenessOperationMode.PASSIVE)
.setDeviceCorrelationId(uuid);
createSessionAndVerify(livenessCommands, content);
}

@ParameterizedTest
@MethodSource("getTestCommands")
public void testCreateSessionDeviceIdOptional(ILivenessSessionSyncCommands livenessCommands) {
CreateLivenessSessionContent content = new CreateLivenessSessionContent(LivenessOperationMode.PASSIVE)
.setDeviceCorrelationIdSetInClient(true);
createSessionAndVerify(livenessCommands, content);
}

@ParameterizedTest
@MethodSource("getTestCommands")
public void testCreateSessionWithTokenTTL(ILivenessSessionSyncCommands livenessCommands) {
int authTokenTimeToLiveInSeconds = 60;
String uuid = UUID.randomUUID().toString();
CreateLivenessSessionContent content = new CreateLivenessSessionContent(LivenessOperationMode.PASSIVE)
.setDeviceCorrelationId(uuid)
.setAuthTokenTimeToLiveInSeconds(authTokenTimeToLiveInSeconds);

CreateLivenessSessionResult result = createSessionAndVerify(livenessCommands, content);
LivenessSession livenessSession = livenessCommands.getLivenessSessionResultSync(result.getSessionId());
Assertions.assertNotNull(livenessSession);
Assertions.assertEquals(livenessSession.getAuthTokenTimeToLiveInSeconds(), authTokenTimeToLiveInSeconds);
}

@AfterEach
public void tearDown() {
String sessionId = mSessionId;
if (sessionId != null && !sessionId.isEmpty()) {
log("sessionId:" + sessionId + ", mCurrentCommand:" + mCurrentCommand);
mCurrentCommand.deleteLivenessSessionSync(sessionId);
}
}

private Stream<Arguments> getTestCommands() {
return Arrays.stream(mLivenessCommands).map(command -> {
mCurrentCommand = command;
return Arguments.of(command);
});
}

private CreateLivenessSessionResult createSessionAndVerify(
ILivenessSessionSyncCommands livenessCommands, CreateLivenessSessionContent content) {
mSessionId = null;
CreateLivenessSessionResult result = livenessCommands.createLivenessSessionSync(content);
mSessionId = result.getSessionId();

Assertions.assertNotNull(result);
Assertions.assertNotNull(result.getSessionId());
Assertions.assertNotNull(result.getAuthToken());

return result;
}
}
Loading

0 comments on commit 88ea2ed

Please sign in to comment.