diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java index 7e446fc4794e6..315316f7f1927 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/TextAnalyticsAsyncClient.java @@ -30,6 +30,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -81,6 +82,7 @@ public Mono> detectLanguageWithResponse(String te @ServiceMethod(returns = ReturnType.SINGLE) Mono> detectLanguageWithResponse(String text, String countryHint, Context context) { + Objects.requireNonNull(text, "'text' cannot be null."); List languageInputs = new ArrayList<>(); languageInputs.add(new DetectLanguageInput(Integer.toString(0), text, countryHint)); // TODO: should this be a random number generator? @@ -145,6 +147,7 @@ public Mono>> detectBatc Mono>> detectBatchLanguagesWithResponse( List inputs, TextAnalyticsRequestOptions options, Context context) { + Objects.requireNonNull(inputs, "'inputs' cannot be null."); // TODO: validate inputs? final LanguageBatchInput languageBatchInput = new LanguageBatchInput().setDocuments(inputs); // TODO: confirm if options null is fine? @@ -173,8 +176,7 @@ private List getDocumentLanguages(final LanguageResult lan } private DetectLanguageResult convertToErrorDetectLanguageResult(final DocumentError errorDocument) { - return new DetectLanguageResult(errorDocument.getId(), null, - errorDocument.getError(), + return new DetectLanguageResult(errorDocument.getId(), null, errorDocument.getError(), null, null); } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/implementation/TextAnalyticsClientImpl.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/implementation/TextAnalyticsClientImpl.java index 3a9830a505ee6..7fb6de5ab1ed8 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/implementation/TextAnalyticsClientImpl.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/implementation/TextAnalyticsClientImpl.java @@ -14,6 +14,7 @@ import com.azure.core.annotation.ServiceInterface; import com.azure.core.annotation.ServiceMethod; import com.azure.core.annotation.UnexpectedResponseExceptionType; +import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpPipeline; import com.azure.core.http.HttpPipelineBuilder; import com.azure.core.http.policy.CookiePolicy; @@ -24,7 +25,6 @@ import com.azure.core.util.Context; import com.azure.cs.textanalytics.implementation.models.EntitiesResult; import com.azure.cs.textanalytics.implementation.models.EntityLinkingResult; -import com.azure.cs.textanalytics.implementation.models.ErrorException; import com.azure.cs.textanalytics.implementation.models.KeyPhraseResult; import com.azure.cs.textanalytics.implementation.models.LanguageBatchInput; import com.azure.cs.textanalytics.implementation.models.LanguageResult; @@ -105,32 +105,32 @@ public TextAnalyticsClientImpl(HttpPipeline httpPipeline) { private interface TextAnalyticsClientService { @Post("entities/recognition/general") @ExpectedResponses({200}) - @UnexpectedResponseExceptionType(ErrorException.class) + @UnexpectedResponseExceptionType(HttpResponseException.class) Mono> entitiesRecognitionGeneral(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context); @Post("entities/recognition/pii") @ExpectedResponses({200}) - @UnexpectedResponseExceptionType(ErrorException.class) + @UnexpectedResponseExceptionType(HttpResponseException.class) Mono> entitiesRecognitionPii(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context); @Post("entities/linking") @ExpectedResponses({200}) - @UnexpectedResponseExceptionType(ErrorException.class) + @UnexpectedResponseExceptionType(HttpResponseException.class) Mono> entitiesLinking(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context); @Post("keyPhrases") @ExpectedResponses({200}) - @UnexpectedResponseExceptionType(ErrorException.class) + @UnexpectedResponseExceptionType(HttpResponseException.class) Mono> keyPhrases(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context); @Post("languages") @ExpectedResponses({200}) - @UnexpectedResponseExceptionType(ErrorException.class) + @UnexpectedResponseExceptionType(HttpResponseException.class) Mono> languages(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") LanguageBatchInput input, Context context); @Post("sentiment") @ExpectedResponses({200}) - @UnexpectedResponseExceptionType(ErrorException.class) + @UnexpectedResponseExceptionType(HttpResponseException.class) Mono> sentiment(@HostParam("Endpoint") String endpoint, @QueryParam("model-version") String modelVersion, @QueryParam("showStats") Boolean showStats, @BodyParam("application/json; charset=utf-8") MultiLanguageBatchInput input, Context context); } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/DocumentError.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/DocumentError.java index f764a9a7cf63d..ad22a79238d48 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/DocumentError.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/DocumentError.java @@ -39,7 +39,7 @@ public String getId() { * @param id the id value to set. * @return the DocumentError object itself. */ - DocumentError setId(String id) { + public DocumentError setId(String id) { this.id = id; return this; } @@ -59,7 +59,7 @@ public Object getError() { * @param error the error value to set. * @return the DocumentError object itself. */ - DocumentError setError(Object error) { + public DocumentError setError(Object error) { this.error = error; return this; } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextBatchStatistics.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextBatchStatistics.java index bfca4cb07380b..2f9e061cbb898 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextBatchStatistics.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextBatchStatistics.java @@ -56,7 +56,7 @@ public int getDocumentCount() { * @param documentsCount the documentsCount value to set. * @return the DocumentBatchStatistics object itself. */ - TextBatchStatistics setDocumentCount(int documentsCount) { + public TextBatchStatistics setDocumentCount(int documentsCount) { this.documentCount = documentsCount; return this; } @@ -78,7 +78,7 @@ public int getValidDocumentCount() { * @param validDocumentCount the validDocumentCount value to set. * @return the DocumentBatchStatistics object itself. */ - TextBatchStatistics setValidDocumentCount(int validDocumentCount) { + public TextBatchStatistics setValidDocumentCount(int validDocumentCount) { this.validDocumentCount = validDocumentCount; return this; } @@ -102,7 +102,7 @@ public int getErroneousDocumentCount() { * @param erroneousDocumentCount the erroneousDocumentCount value to set. * @return the DocumentBatchStatistics object itself. */ - TextBatchStatistics setErroneousDocumentCount(int erroneousDocumentCount) { + public TextBatchStatistics setErroneousDocumentCount(int erroneousDocumentCount) { this.erroneousDocumentCount = erroneousDocumentCount; return this; } @@ -124,7 +124,7 @@ public long getTransactionCount() { * @param transactionCount the transactionsCount value to set. * @return the DocumentBatchStatistics object itself. */ - TextBatchStatistics setTransactionCount(long transactionCount) { + public TextBatchStatistics setTransactionCount(long transactionCount) { this.transactionCount = transactionCount; return this; } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextDocumentStatistics.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextDocumentStatistics.java index b4f4b3b5e7b98..ebe2142a640c7 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextDocumentStatistics.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextDocumentStatistics.java @@ -42,7 +42,7 @@ public int getCharacterCount() { * @param characterCount the charactersCount value to set. * @return the TextDocumentStatistics object itself. */ - TextDocumentStatistics setCharacterCount(int characterCount) { + public TextDocumentStatistics setCharacterCount(int characterCount) { this.characterCount = characterCount; return this; } @@ -64,7 +64,7 @@ public int getTransactionCount() { * @param transactionCount the transactionsCount value to set. * @return the TextDocumentStatistics object itself. */ - TextDocumentStatistics setTransactionCount(int transactionCount) { + public TextDocumentStatistics setTransactionCount(int transactionCount) { this.transactionCount = transactionCount; return this; } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextSentiment.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextSentiment.java index 1705235260593..4ab48480f7649 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextSentiment.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/com/azure/cs/textanalytics/models/TextSentiment.java @@ -21,7 +21,7 @@ public class TextSentiment { // sentence private String length; - private int offSet; + private int offset; // sentiment string private TextSentimentClass textSentimentClass; @@ -62,12 +62,12 @@ TextSentiment setPositiveScore(double positiveScore) { return this; } - public int getOffSet() { - return offSet; + public int getOffset() { + return offset; } - TextSentiment setOffSet(int offSet) { - this.offSet = offSet; + TextSentiment setOffset(int offset) { + this.offset = offset; return this; } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/module-info.java b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/module-info.java index c9aed4dd9445d..323b7952c7609 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/main/java/module-info.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/main/java/module-info.java @@ -7,6 +7,7 @@ opens com.azure.cs.textanalytics.implementation to com.fasterxml.jackson.databind; opens com.azure.cs.textanalytics.models to com.fasterxml.jackson.databind; + opens com.azure.cs.textanalytics.implementation.models to com.fasterxml.jackson.databind; exports com.azure.cs.textanalytics; exports com.azure.cs.textanalytics.models; diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/samples/java/com/azure/cs/textanalytics/batch/AnalyzeSentimentBatchDocuments.java b/sdk/textanalytics/azure-cs-textanalytics/src/samples/java/com/azure/cs/textanalytics/batch/AnalyzeSentimentBatchDocuments.java index 230fc9a0fd4ab..1da73ed71f148 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/samples/java/com/azure/cs/textanalytics/batch/AnalyzeSentimentBatchDocuments.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/samples/java/com/azure/cs/textanalytics/batch/AnalyzeSentimentBatchDocuments.java @@ -49,7 +49,7 @@ public static void main(String[] args) { documentSentiment.getNeutralScore(), documentSentiment.getNegativeScore(), documentSentiment.getLength(), - documentSentiment.getOffSet()); + documentSentiment.getOffset()); result.getSentenceSentiments().forEach(sentenceSentiment -> System.out.printf("Recognized sentence sentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s. Length of sentence: %s, Offset of sentence: %s", @@ -58,7 +58,7 @@ public static void main(String[] args) { sentenceSentiment.getNeutralScore(), sentenceSentiment.getNegativeScore(), sentenceSentiment.getLength(), - sentenceSentiment.getOffSet())); + sentenceSentiment.getOffset())); }); } } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsAsyncClientTest.java b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsAsyncClientTest.java index 46f6121180132..8210531c8310c 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsAsyncClientTest.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsAsyncClientTest.java @@ -3,15 +3,18 @@ package com.azure.cs.textanalytics; -import com.azure.core.util.logging.ClientLogger; +import com.azure.cs.textanalytics.models.DetectedLanguage; import org.junit.jupiter.api.Test; import reactor.test.StepVerifier; -import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; -public class TextAnalyticsAsyncClientTest extends TextAnalyticsClientTestBase{ - private final ClientLogger logger = new ClientLogger(TextAnalyticsAsyncClientTest.class); +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +public class TextAnalyticsAsyncClientTest extends TextAnalyticsClientTestBase{ private TextAnalyticsAsyncClient client; @Override @@ -23,13 +26,144 @@ protected void beforeTest() { .buildAsyncClient()); } + /** + * Test Detect batch input langugaes with show statistics. + */ @Test - public void detectLanguage() { - detectLanguageRunner((inputs, options) -> { + public void detectLanguagesBatchInputShowStatistics() { + detectLanguageShowStatisticsRunner((inputs, options) -> { StepVerifier.create(client.detectBatchLanguages(inputs, options)) - .assertNext(response -> assertTrue(true)) + .assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language")) + .verifyComplete(); + }); + } + + /** + * Test Detect batch input langugaes with show statistics. + */ + @Test + public void detectLanguagesBatchInputShowStatisticsNew() { + detectLanguageShowStatisticsRunner((inputs, options) -> { + StepVerifier.create(client.detectBatchLanguages(inputs, options)) + .assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language")) + .verifyComplete(); + }); + } + + /** + * Verifies that a batch collection is returned on batch input for detectLanguages. + */ + @Test + public void detectLanguagesBatchStringList() { + detectLanguagesCountryHintRunner((inputs, countryHint) -> { + StepVerifier.create(client.detectLanguages(inputs, countryHint)) + .assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language")) .verifyComplete(); - System.out.println("Completed!"); }); } + + /** + * Verifies that a batch collection is returned on batch input for detectLanguages. + */ + @Test + public void detectLanguagesBatchStringInput() { + detectLanguageStringInputRunner((inputs) -> { + StepVerifier.create(client.detectLanguages(inputs)) + .assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language")) + .verifyComplete(); + }); + } + + /** + * Verifies that a batch collection is returned on batch input for detectLanguages. + */ + @Test + public void detectLanguagesBatchInput() { + detectLanguageRunner((inputs) -> { + StepVerifier.create(client.detectBatchLanguages(inputs)) + .assertNext(response -> validateBatchResult(response, getExpectedBatchDetectedLanguages(), "Language")) + .verifyComplete(); + }); + } + + /** + * Verifies that a Null pointer exception is thrown when null text is passed. + */ + @Test + public void detectLanguagesNullInput() { + detectLanguageRunner((inputs) -> { + StepVerifier.create(client.detectBatchLanguagesWithResponse(null, null)) + .verifyError(NullPointerException.class); + }); + } + + /** + * Verifies that a single DetectLanguageResult is returned for a single text input to detectLanguages. + * + */ + @Test + public void detectLanguage() { + DetectedLanguage primaryLanguage = new DetectedLanguage().setName("English").setIso6391Name("en").setScore(1.0); + List expectedLanguageList = new ArrayList<>(Arrays.asList(primaryLanguage)); + StepVerifier.create(client.detectLanguage("This is a test English Text")) + .assertNext(response -> validateDetectedLanguages(expectedLanguageList, response.getDetectedLanguages())) + .verifyComplete(); + } + + /** + * Verifies that a single error DetectLanguageResult is returned for a single text input with invalid country hint. + * + * TODO: update error Model. #6559 + */ + // @Test + // public void detectLanguageInvalidCountryHint() { + // DetectedLanguage primaryLanguage = new DetectedLanguage().setName("English").setIso6391Name("en").setScore(1.0); + // Error error = new Error().setCode("Invalid Hint"); + // StepVerifier.create(client.detectLanguage("Este es un document escrito en Español.", "en")) + // .assertNext(response -> assertEquals(error.getCode(), ((Error)response.getError()).getCode())) + // .verifyComplete(); + // } + + /** + * Verifies that a single DetectLanguageResult is returned for a single text input with country hint. + * + * TODO: update error Model. #6559 + */ + // @Test + // public void detectLanguageCountryHint() { + // DetectedLanguage primaryLanguage = new DetectedLanguage().setName("Spanish").setIso6391Name("es").setScore(1.0); + // Error error = new Error().setCode("Invalid Hint"); + // StepVerifier.create(client.detectLanguage("Este es un document escrito en Español.", "es")) + // .assertNext(response -> assertEquals(error.getCode(), ((Error)response.getError()).getCode())) + // .verifyComplete(); + // } + + /** + * Verifies that the error result is returned when empty text is passed. + */ + @Test + public void detectLanguageEmptyText() { + StepVerifier.create(client.detectLanguage("")) + .assertNext(response -> assertNotNull(response.getError())) + .verifyComplete(); + } + + /** + * Verifies that it returns an exception is thrown when null text is passed. + */ + @Test + public void detectLanguageNullText() { + StepVerifier.create(client.detectLanguage(null)).verifyError(NullPointerException.class); + } + + /** + * Verifies that an document returns with an error when error text is passed. + */ + @Test + public void detectLanguageFaultyText() { + StepVerifier.create(client.detectLanguage("!@#%%")) + .assertNext(response -> + assertEquals(response.getPrimaryLanguage().getIso6391Name(), "(Unknown)")) + .verifyComplete(); + } } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTest.java b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTest.java index d646c67a358c6..1983c7de922ec 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTest.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTest.java @@ -28,4 +28,9 @@ protected void beforeTest() { public void detectLanguage() { } + + @Override + public void detectLanguagesBatchInput() { + + } } diff --git a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTestBase.java b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTestBase.java index 38923832127e4..089ebe0f54b46 100644 --- a/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTestBase.java +++ b/sdk/textanalytics/azure-cs-textanalytics/src/test/java/com.azure.cs.textanalytics/TextAnalyticsClientTestBase.java @@ -9,7 +9,6 @@ import com.azure.core.http.HttpPipelineBuilder; import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; import com.azure.core.http.policy.AddDatePolicy; -import com.azure.core.http.policy.AddHeadersPolicy; import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; import com.azure.core.http.policy.HttpLogDetailLevel; import com.azure.core.http.policy.HttpLogOptions; @@ -24,29 +23,44 @@ import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; import com.azure.cs.textanalytics.models.DetectLanguageInput; +import com.azure.cs.textanalytics.models.DetectLanguageResult; +import com.azure.cs.textanalytics.models.DetectedLanguage; +import com.azure.cs.textanalytics.models.DocumentError; +import com.azure.cs.textanalytics.models.DocumentResultCollection; import com.azure.cs.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.cs.textanalytics.models.TextBatchStatistics; +import com.azure.cs.textanalytics.models.TextDocumentStatistics; import com.azure.identity.DefaultAzureCredentialBuilder; import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; public abstract class TextAnalyticsClientTestBase extends TestBase { private static final String TEXT_ANALYTICS_PROPERTIES = "azure-textanalytics.properties"; private static final String NAME = "name"; private static final String VERSION = "version"; - Map properties = CoreUtils.getProperties(TEXT_ANALYTICS_PROPERTIES); + final Map properties = CoreUtils.getProperties(TEXT_ANALYTICS_PROPERTIES); private final ClientLogger logger = new ClientLogger(TextAnalyticsClientTestBase.class); private final String clientName = properties.getOrDefault(NAME, "UnknownName"); private final String clientVersion = properties.getOrDefault(VERSION, "UnknownVersion"); + private boolean showStatistics = false; + void beforeTestSetup() { } @@ -103,16 +117,74 @@ T clientSetup(Function clientBuilder) { @Test public abstract void detectLanguage(); - void detectLanguageRunner(BiConsumer, TextAnalyticsRequestOptions> testRunner) { + @Test + public abstract void detectLanguagesBatchInput(); + void detectLanguageShowStatisticsRunner(BiConsumer, + TextAnalyticsRequestOptions> testRunner) { final List detectLanguageInputs = Arrays.asList( - new DetectLanguageInput("1", "This is written in English","US"), - new DetectLanguageInput("2", "Este es un document escrito en Español.", "es") + new DetectLanguageInput("0", "This is written in English", "US"), + new DetectLanguageInput("1", "Este es un document escrito en Español."), + new DetectLanguageInput("2", "~@!~:)", "US") + // add error document => empty text ); - testRunner.accept(detectLanguageInputs, getTextAnalyticsRequestOptions()); + testRunner.accept(detectLanguageInputs, setTextAnalyticsRequestOptions()); } + static DocumentResultCollection getExpectedBatchDetectedLanguages() { + DetectedLanguage detectedLanguage1 = new DetectedLanguage().setName("English").setIso6391Name("en") + .setScore(1.0); + DetectedLanguage detectedLanguage2 = new DetectedLanguage().setName("Spanish").setIso6391Name("es") + .setScore(1.0); + DetectedLanguage detectedLanguage3 = new DetectedLanguage().setName("(Unknown)").setIso6391Name("(Unknown)") + .setScore(0.0); + List detectedLanguageList1 = new ArrayList<>(Collections.singletonList(detectedLanguage1)); + List detectedLanguageList2 = new ArrayList<>(Collections.singletonList(detectedLanguage2)); + List detectedLanguageList3 = new ArrayList<>(Collections.singletonList(detectedLanguage3)); + + TextDocumentStatistics textDocumentStatistics1 = new TextDocumentStatistics().setCharacterCount(26).setTransactionCount(1); + TextDocumentStatistics textDocumentStatistics2 = new TextDocumentStatistics().setCharacterCount(39).setTransactionCount(1); + TextDocumentStatistics textDocumentStatistics3 = new TextDocumentStatistics().setCharacterCount(6).setTransactionCount(1); + + DetectLanguageResult detectLanguageResult1 = new DetectLanguageResult("0", textDocumentStatistics1, null, detectedLanguage1, detectedLanguageList1); + DetectLanguageResult detectLanguageResult2 = new DetectLanguageResult("1", textDocumentStatistics2, null, detectedLanguage2, detectedLanguageList2); + DetectLanguageResult detectLanguageResult3 = new DetectLanguageResult("2", textDocumentStatistics3, null, detectedLanguage3, detectedLanguageList3); + + TextBatchStatistics textBatchStatistics = new TextBatchStatistics().setDocumentCount(3).setErroneousDocumentCount(0).setTransactionCount(3).setValidDocumentCount(3); + List detectLanguageResultList = new ArrayList<>(Arrays.asList(detectLanguageResult1, detectLanguageResult2, detectLanguageResult3)); + + return new DocumentResultCollection<>(detectLanguageResultList, "2019-10-01", textBatchStatistics); + } + + static void detectLanguagesCountryHintRunner(BiConsumer, String> testRunner) { + final List inputs = new ArrayList<>(Arrays.asList( + "This is written in English", "Este es un document escrito en Español.", "~@!~:)")); + + testRunner.accept(inputs, "US"); + } + + static void detectLanguageStringInputRunner(Consumer> testRunner) { + final List inputs = new ArrayList<>(Arrays.asList( + "This is written in English", "Este es un document escrito en Español.", "~@!~:)")); + + testRunner.accept(inputs); + } + + private TextAnalyticsRequestOptions setTextAnalyticsRequestOptions() { + this.showStatistics = true; + return new TextAnalyticsRequestOptions().setShowStatistics(true); + } + + static void detectLanguageRunner(Consumer> testRunner) { + final List detectLanguageInputs = Arrays.asList( + new DetectLanguageInput("0", "This is written in English", "US"), + new DetectLanguageInput("1", "Este es un document escrito en Español."), + new DetectLanguageInput("2", "~@!~:)", "US") + ); + + testRunner.accept(detectLanguageInputs); + } String getEndPoint() { return interceptorManager.isPlaybackMode() @@ -120,8 +192,133 @@ String getEndPoint() { : Configuration.getGlobalConfiguration().get("AZURE_TEXT_ANALYTICS_ENDPOINT"); } - TextAnalyticsRequestOptions getTextAnalyticsRequestOptions() { - return new TextAnalyticsRequestOptions().setShowStatistics(true); + /** + * Helper method to verify batch result. + * + * @param actualResult DocumentResultCollection<> returned by the API. + * @param testApi the API to test. + */ + void validateBatchResult(DocumentResultCollection actualResult, + DocumentResultCollection expectedResult, String testApi) { + // assert batch result + assertEquals(expectedResult.getModelVersion(), actualResult.getModelVersion()); + if (this.showStatistics) { + validateBatchStatistics(expectedResult.getStatistics(), actualResult.getStatistics()); + } + validateDocuments(expectedResult, actualResult, testApi); + + // TODO error model + // DocumentError error = new DocumentError().setId("4").setError("error"); + // List errors = new ArrayList<>(); + // errors.add(error); + // validateErrorDocuments(expected.getErrors(), detectLanguageResultList); + } + + /** + * Helper method to verify documents returned in a batch request. + * + * @param expectedResult the expected result collection.. + * @param actualResult the actual result collection returned by the API. + * @param testApi the API to test. + */ + private void validateDocuments(DocumentResultCollection expectedResult, + DocumentResultCollection actualResult, String testApi) { + switch (testApi) { + case "Language": + final List expectedResultList = expectedResult.stream() + .filter(element -> element instanceof DetectLanguageResult) + .map(element -> (DetectLanguageResult) element) + .collect(Collectors.toList()); + + final List actualResultList = actualResult.stream() + .filter(element -> element instanceof DetectLanguageResult) + .map(element -> (DetectLanguageResult) element) + .collect(Collectors.toList()); + assertEquals(expectedResultList.size(), actualResultList.size()); + + actualResultList.forEach(actualItem -> { + Optional optionalExpectedItem = expectedResultList.stream().filter( + expectedEachItem -> actualItem.getId().equals(expectedEachItem.getId())).findFirst(); + assertTrue(optionalExpectedItem.isPresent()); + DetectLanguageResult expectedItem = optionalExpectedItem.get(); + if (actualItem.getError() == null && this.showStatistics) { + validatePrimaryLanguage(expectedItem.getPrimaryLanguage(), actualItem.getPrimaryLanguage()); + validateDocumentStatistics(expectedItem.getStatistics(), actualItem.getStatistics()); + validateDetectedLanguages(expectedItem.getDetectedLanguages(), actualItem.getDetectedLanguages()); + } + }); + } + } + + /** + * Helper method to verify TextBatchStatistics. + * + * @param expectedStatistics + * @param actualStatistics + */ + private static void validateBatchStatistics(TextBatchStatistics expectedStatistics, + TextBatchStatistics actualStatistics) { + assertEquals(expectedStatistics.getDocumentCount(), actualStatistics.getDocumentCount()); + assertEquals(expectedStatistics.getErroneousDocumentCount(), actualStatistics.getErroneousDocumentCount()); + assertEquals(expectedStatistics.getValidDocumentCount(), actualStatistics.getValidDocumentCount()); + assertEquals(expectedStatistics.getTransactionCount(), actualStatistics.getTransactionCount()); } + /** + * Helper method to verify the error-ed documents. + * + * @param errors + * @param detectLanguageResultList + */ + static void validateErrorDocuments(List errors, + List detectLanguageResultList) { + for (DocumentError expectedErrorDocument : errors) { + Optional optionalErrorDocument = detectLanguageResultList.stream(). + filter(document -> document.getId().equals(expectedErrorDocument.getId())).findFirst(); + assertTrue(optionalErrorDocument.isPresent()); + DetectLanguageResult actualErrorDocument = optionalErrorDocument.get(); + assertEquals(expectedErrorDocument.getId(), actualErrorDocument.getId()); + // TODO: Need to fix the error model + assertEquals(expectedErrorDocument.getError().toString(), actualErrorDocument.getError().toString()); + } + + } + + /** + * Helper method to verify TextDocumentStatistics. + * + * @param expected the expected value for TextDocumentStatistics. + * @param actual the value returned by API. + */ + private static void validateDocumentStatistics(TextDocumentStatistics expected, TextDocumentStatistics actual) { + assertEquals(expected.getCharacterCount(), actual.getCharacterCount()); + assertEquals(expected.getTransactionCount(), actual.getTransactionCount()); + } + + /** + * Helper method to validate a single detected language. + * + * @param expectedLanguage detectedLanguage returned by the service. + * @param actualLanguage detectedLanguage returned by the API. + */ + static void validatePrimaryLanguage(DetectedLanguage expectedLanguage, DetectedLanguage actualLanguage) { + assertEquals(expectedLanguage.getIso6391Name(), actualLanguage.getIso6391Name()); + assertEquals(expectedLanguage.getName(), actualLanguage.getName()); + assertEquals(expectedLanguage.getScore(), actualLanguage.getScore()); + } + + /** + * Helper method to validate the list of detected languages. + * + * @param expectedLanguageList detectedLanguages returned by the service. + * @param actualLanguageList detectedLanguages returned by the API. + */ + static void validateDetectedLanguages(List expectedLanguageList, + List actualLanguageList) { + for (int i = 0; i < expectedLanguageList.size(); i++) { + DetectedLanguage expectedDetectedLanguage = expectedLanguageList.get(i); + DetectedLanguage actualDetectedLanguage = actualLanguageList.get(i); + validatePrimaryLanguage(expectedDetectedLanguage, actualDetectedLanguage); + } + } }