Skip to content

Commit

Permalink
[Form Recognizer] Re-record with Test proxy (#34436)
Browse files Browse the repository at this point in the history
  • Loading branch information
samvaity authored Apr 17, 2023
1 parent f47a7f8 commit 0b34d9f
Show file tree
Hide file tree
Showing 481 changed files with 1,097,958 additions and 37,876 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.core.test.http.MockHttpResponse;
import com.azure.core.util.ClientOptions;
Expand Down Expand Up @@ -44,7 +44,7 @@
/**
* Tests for Form Recognizer client builder
*/
public class FormRecognizerClientBuilderTest extends TestBase {
public class FormRecognizerClientBuilderTest extends TestProxyTestBase {
/**
* Test client builder with invalid API key
*/
Expand Down Expand Up @@ -247,10 +247,10 @@ FormRecognizerClientBuilder createClientBuilder(HttpClient httpClient, FormRecog
final FormRecognizerClientBuilder clientBuilder = new FormRecognizerClientBuilder()
.credential(credential)
.endpoint(endpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.serviceVersion(serviceVersion);

if (!interceptorManager.isPlaybackMode()) {
if (interceptorManager.isRecordMode()) {
clientBuilder.addPolicy(interceptorManager.getRecordPolicy());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,30 @@
import com.azure.ai.formrecognizer.models.SelectionMarkState;
import com.azure.ai.formrecognizer.models.TextStyleName;
import com.azure.ai.formrecognizer.training.FormTrainingClientBuilder;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.models.BodilessMatcher;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -51,18 +55,17 @@
import static com.azure.ai.formrecognizer.FormTrainingClientTestBase.FORM_RECOGNIZER_SELECTION_MARK_BLOB_CONTAINER_SAS_URL;
import static com.azure.ai.formrecognizer.FormTrainingClientTestBase.FORM_RECOGNIZER_TRAINING_BLOB_CONTAINER_SAS_URL;
import static com.azure.ai.formrecognizer.TestUtils.FAKE_ENCODED_EMPTY_SPACE_URL;
import static com.azure.ai.formrecognizer.TestUtils.INVALID_KEY;
import static com.azure.ai.formrecognizer.TestUtils.ONE_NANO_DURATION;
import static com.azure.ai.formrecognizer.TestUtils.TEST_DATA_PNG;
import static com.azure.ai.formrecognizer.TestUtils.URL_TEST_FILE_FORMAT;
import static com.azure.ai.formrecognizer.TestUtils.getAudience;
import static com.azure.ai.formrecognizer.TestUtils.getTestProxySanitizers;
import static com.azure.ai.formrecognizer.implementation.Utility.DEFAULT_POLL_INTERVAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public abstract class FormRecognizerClientTestBase extends TestBase {
public abstract class FormRecognizerClientTestBase extends TestProxyTestBase {
private static final String EXPECTED_RECEIPT_ADDRESS_VALUE = "123 Main Street Redmond, WA 98052";
private static final String EXPECTED_JPEG_RECEIPT_PHONE_NUMBER_VALUE = "+19876543210";
private static final String ITEMIZED_RECEIPT_VALUE = "Itemized";
Expand Down Expand Up @@ -137,17 +140,20 @@ FormRecognizerClientBuilder getFormRecognizerClientBuilder(HttpClient httpClient

FormRecognizerClientBuilder builder = new FormRecognizerClientBuilder()
.endpoint(endpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.serviceVersion(serviceVersion)
.addPolicy(interceptorManager.getRecordPolicy())
.audience(audience);

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new AzureKeyCredential(INVALID_KEY));
} else {
if (interceptorManager.isPlaybackMode()) {
builder.credential(new MockTokenCredential());
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher()));
} else if (interceptorManager.isRecordMode()) {
builder.credential(new DefaultAzureCredentialBuilder().build());
builder.addPolicy(interceptorManager.getRecordPolicy());
}

interceptorManager.addSanitizers(getTestProxySanitizers());
return builder;
}

Expand All @@ -158,17 +164,24 @@ FormTrainingClientBuilder getFormTrainingClientBuilder(HttpClient httpClient,

FormTrainingClientBuilder builder = new FormTrainingClientBuilder()
.endpoint(endpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.serviceVersion(serviceVersion)
.addPolicy(interceptorManager.getRecordPolicy())
.audience(audience);

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new AzureKeyCredential(INVALID_KEY));
} else {
if (interceptorManager.isPlaybackMode()) {
builder.credential(new TokenCredential() {
@Override
public Mono<AccessToken> getToken(TokenRequestContext tokenRequestContext) {
return Mono.just(new AccessToken("mockToken", OffsetDateTime.now().plusHours(2)));
}
});
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher()));
} else if (interceptorManager.isRecordMode()) {
builder.credential(new DefaultAzureCredentialBuilder().build());
builder.addPolicy(interceptorManager.getRecordPolicy());
}
interceptorManager.addSanitizers(getTestProxySanitizers());
return builder;
}

Expand Down Expand Up @@ -451,15 +464,12 @@ void testingContainerUrlRunner(Consumer<String> testRunner, String fileName) {
void dataRunner(BiConsumer<InputStream, Long> testRunner, String fileName) {
final long fileLength = new File(LOCAL_FILE_PATH + fileName).length();

if (interceptorManager.isPlaybackMode()) {
testRunner.accept(new ByteArrayInputStream(TEST_DATA_PNG.getBytes(StandardCharsets.UTF_8)), fileLength);
} else {
try {
testRunner.accept(new FileInputStream(LOCAL_FILE_PATH + fileName), fileLength);
} catch (FileNotFoundException e) {
throw new RuntimeException("Local file not found.", e);
}
try {
testRunner.accept(new FileInputStream(LOCAL_FILE_PATH + fileName), fileLength);
} catch (FileNotFoundException e) {
throw new RuntimeException("Local file not found.", e);
}

}

void localFilePathRunner(BiConsumer<String, Long> testRunner, String fileName) {
Expand Down Expand Up @@ -1195,19 +1205,11 @@ private void validateBoundingBoxData(FieldBoundingBox fieldBoundingBox) {
* @return the training data set Url
*/
private String getTrainingSasUri() {
if (interceptorManager.isPlaybackMode()) {
return "https://isPlaybackmode";
} else {
return Configuration.getGlobalConfiguration().get(FORM_RECOGNIZER_TRAINING_BLOB_CONTAINER_SAS_URL);
}
return Configuration.getGlobalConfiguration().get(FORM_RECOGNIZER_TRAINING_BLOB_CONTAINER_SAS_URL, "https://isPlaybackmode");
}

private String getSelectionMarkTrainingSasUri() {
if (interceptorManager.isPlaybackMode()) {
return "https://isPlaybackmode";
} else {
return Configuration.getGlobalConfiguration().get(FORM_RECOGNIZER_SELECTION_MARK_BLOB_CONTAINER_SAS_URL);
}
return Configuration.getGlobalConfiguration().get(FORM_RECOGNIZER_SELECTION_MARK_BLOB_CONTAINER_SAS_URL, "https://isPlaybackmode");
}

/**
Expand All @@ -1216,12 +1218,8 @@ private String getSelectionMarkTrainingSasUri() {
* @return the training data set Url
*/
private String getMultipageTrainingSasUri() {
if (interceptorManager.isPlaybackMode()) {
return "https://isPlaybackmode";
} else {
return Configuration.getGlobalConfiguration()
.get(FORM_RECOGNIZER_MULTIPAGE_TRAINING_BLOB_CONTAINER_SAS_URL);
}
return Configuration.getGlobalConfiguration()
.get(FORM_RECOGNIZER_MULTIPAGE_TRAINING_BLOB_CONTAINER_SAS_URL, "https://isPlaybackmode");
}

/**
Expand All @@ -1230,11 +1228,7 @@ private String getMultipageTrainingSasUri() {
* @return the testing data set Url
*/
private String getTestingSasUri() {
if (interceptorManager.isPlaybackMode()) {
return "https://isPlaybackmode?SASToken";
} else {
return Configuration.getGlobalConfiguration().get("FORM_RECOGNIZER_TESTING_BLOB_CONTAINER_SAS_URL");
}
return Configuration.getGlobalConfiguration().get("FORM_RECOGNIZER_TESTING_BLOB_CONTAINER_SAS_URL", "https://isPlaybackmode?SASToken");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.annotation.DoNotRecord;
import com.azure.core.test.http.MockHttpResponse;
import com.azure.core.util.ClientOptions;
Expand Down Expand Up @@ -45,7 +45,7 @@
/**
* Tests for Form Training client builder
*/
public class FormTrainingClientBuilderTest extends TestBase {
public class FormTrainingClientBuilderTest extends TestProxyTestBase {
private static final String FORM_JPG = "Form_1.jpg";

/**
Expand Down Expand Up @@ -236,10 +236,10 @@ FormTrainingClientBuilder createClientBuilder(HttpClient httpClient, FormRecogni
final FormTrainingClientBuilder clientBuilder = new FormTrainingClientBuilder()
.credential(credential)
.endpoint(endpoint)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.serviceVersion(serviceVersion);

if (!interceptorManager.isPlaybackMode()) {
if (interceptorManager.isRecordMode()) {
clientBuilder.addPolicy(interceptorManager.getRecordPolicy());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,37 @@
import com.azure.ai.formrecognizer.training.models.CustomFormModelStatus;
import com.azure.ai.formrecognizer.training.models.TrainingDocumentInfo;
import com.azure.ai.formrecognizer.training.models.TrainingStatus;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.test.TestProxyTestBase;
import com.azure.core.test.models.BodilessMatcher;
import com.azure.core.test.utils.MockTokenCredential;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static com.azure.ai.formrecognizer.TestUtils.BLANK_PDF;
import static com.azure.ai.formrecognizer.TestUtils.INVALID_KEY;
import static com.azure.ai.formrecognizer.TestUtils.INVALID_RECEIPT_URL;
import static com.azure.ai.formrecognizer.TestUtils.ONE_NANO_DURATION;
import static com.azure.ai.formrecognizer.TestUtils.TEST_DATA_PNG;
import static com.azure.ai.formrecognizer.TestUtils.getTestProxySanitizers;
import static com.azure.ai.formrecognizer.implementation.Utility.DEFAULT_POLL_INTERVAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public abstract class FormTrainingClientTestBase extends TestBase {
public abstract class FormTrainingClientTestBase extends TestProxyTestBase {
private static final String RESOURCE_ID = "FORM_RECOGNIZER_TARGET_RESOURCE_ID";
private static final String RESOURCE_REGION = "FORM_RECOGNIZER_TARGET_RESOURCE_REGION";
private static final String LOCAL_FILE_PATH = "src/test/resources/sample_files/Test/";
Expand Down Expand Up @@ -90,16 +88,19 @@ FormTrainingClientBuilder getFormTrainingClientBuilder(HttpClient httpClient,
FormRecognizerServiceVersion serviceVersion) {
FormTrainingClientBuilder builder = new FormTrainingClientBuilder()
.endpoint(getEndpoint())
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient)
.httpClient(interceptorManager.isPlaybackMode() ? interceptorManager.getPlaybackClient() : httpClient)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.serviceVersion(serviceVersion)
.addPolicy(interceptorManager.getRecordPolicy());
.serviceVersion(serviceVersion);

if (getTestMode() == TestMode.PLAYBACK) {
builder.credential(new AzureKeyCredential(INVALID_KEY));
} else {
if (interceptorManager.isPlaybackMode()) {
builder.credential(new MockTokenCredential());
interceptorManager.addMatchers(Arrays.asList(new BodilessMatcher()));
} else if (interceptorManager.isRecordMode()) {
builder.credential(new DefaultAzureCredentialBuilder().build());
builder.addPolicy(interceptorManager.getRecordPolicy());
}
interceptorManager.addSanitizers(getTestProxySanitizers());

return builder;
}

Expand Down Expand Up @@ -287,15 +288,10 @@ String getEndpoint() {

void blankPdfDataRunner(BiConsumer<InputStream, Long> testRunner) {
final long fileLength = new File(LOCAL_FILE_PATH + BLANK_PDF).length();

if (interceptorManager.isPlaybackMode()) {
testRunner.accept(new ByteArrayInputStream(TEST_DATA_PNG.getBytes(StandardCharsets.UTF_8)), fileLength);
} else {
try {
testRunner.accept(new FileInputStream(LOCAL_FILE_PATH + BLANK_PDF), fileLength);
} catch (FileNotFoundException e) {
throw new RuntimeException("Local file not found.", e);
}
try {
testRunner.accept(new FileInputStream(LOCAL_FILE_PATH + BLANK_PDF), fileLength);
} catch (FileNotFoundException e) {
throw new RuntimeException("Local file not found.", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpClient;
import com.azure.core.test.InterceptorManager;
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.test.models.TestProxySanitizerType;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.FluxUtil;
Expand Down Expand Up @@ -36,18 +38,18 @@
* Contains helper methods for generating inputs for test methods
*/
final class TestUtils {
private static final String REDACTED_VALUE = "REDACTED";
private static final String URL_REGEX = "(?<=http://|https://)([^/?]+)";
// Duration
static final Duration ONE_NANO_DURATION = Duration.ofMillis(1);
// Local test files
static final String BLANK_PDF = "blank.pdf";
static final String CONTENT_FORM_JPG = "Form_1.jpg";
static final String TEST_DATA_PNG = "testData.png";
static final String SELECTION_MARK_PDF = "selectionMarkForm.pdf";
static final String CONTENT_GERMAN_PDF = "content_german.pdf";
// Other resources
static final String DISPLAY_NAME_WITH_ARGUMENTS = "{displayName} with [{arguments}]";
static final String FAKE_ENCODED_EMPTY_SPACE_URL = "https://fakeuri.com/blank%20space";
static final String INVALID_IMAGE_URL_ERROR_CODE = "InvalidImageURL";
static final String INVALID_KEY = "invalid key";
static final String INVALID_MODEL_ID = "a0a3998a-4c4affe66b7";
static final String INVALID_MODEL_ID_ERROR = "Invalid UUID string: " + INVALID_MODEL_ID;
Expand Down Expand Up @@ -176,5 +178,16 @@ static <T, U> SyncPoller<T, U> setSyncPollerPollInterval(SyncPoller<T, U> syncPo
? syncPoller.setPollInterval(Duration.ofMillis(1))
: syncPoller;
}

public static List<TestProxySanitizer> getTestProxySanitizers() {
return Arrays.asList(
new TestProxySanitizer("$..targetModelLocation", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
new TestProxySanitizer("$..targetResourceId", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
new TestProxySanitizer("$..urlSource", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
new TestProxySanitizer("$..azureBlobSource.containerUrl", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
new TestProxySanitizer("$..source", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
new TestProxySanitizer("$..resourceLocation", null, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY),
new TestProxySanitizer("Location", URL_REGEX, REDACTED_VALUE, TestProxySanitizerType.BODY_KEY));
}
}

Loading

0 comments on commit 0b34d9f

Please sign in to comment.