Skip to content

Commit

Permalink
Feature: JTest
Browse files Browse the repository at this point in the history
  • Loading branch information
brenoepics committed Jan 11, 2024
1 parent 1a443ce commit 865e8db
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,10 @@
<version>33.0.0-jre</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.8.0</version>
<scope>compile</scope>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
59 changes: 23 additions & 36 deletions src/test/java/AzureApiTests.java
Original file line number Diff line number Diff line change
@@ -1,115 +1,102 @@
import okhttp3.OkHttpClient;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.Test;
import tech.brenoepic.at4j.AzureApi;
import tech.brenoepic.at4j.AzureApiBuilder;
import tech.brenoepic.at4j.azure.BaseURL;
import tech.brenoepic.at4j.azure.lang.Language;
import tech.brenoepic.at4j.data.request.AvailableLanguagesParams;
import tech.brenoepic.at4j.data.request.TranslateParams;
import tech.brenoepic.at4j.data.Translation;
import tech.brenoepic.at4j.data.response.TranslationResponse;

import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import static org.junit.Assert.*;


public class AzureApiTests {
@Mock

AzureApi api;

@Test
@DisplayName("Build with null key")
void BuildNullKey() {
public void BuildNullKey() {
AzureApiBuilder builder = new AzureApiBuilder();
Assertions.assertThrows(IllegalArgumentException.class, builder::build, "Subscription key cannot be null");
assertThrows("Subscription key cannot be null", IllegalArgumentException.class, builder::build);
}

@Test
@DisplayName("Build with HTTP client")
void BuildNullHttpClient() {
public void BuildNullHttpClient() {
AzureApiBuilder builder = new AzureApiBuilder().setSubscriptionKey("");
Assertions.assertThrowsExactly(IllegalArgumentException.class, builder::build, "HTTP client cannot be null");
assertThrows("HTTP client cannot be null", IllegalArgumentException.class, builder::build);
}

@Test
@DisplayName("Build the api")
void buildApi() {
public void buildApi() {
api = new AzureApiBuilder().setSubscriptionKey("test").setSubscriptionRegion("test").setOkHttpClient(new OkHttpClient()).build();
Assertions.assertNotNull(api);
assertNotNull(api);
api.getThreadPool().getExecutorService().shutdown();
}


@Test
@DisplayName("Get languages")
void getLanguages() {
public void getLanguages() {
AzureApi api = new AzureApiBuilder().setBaseURL(BaseURL.GLOBAL).setSubscriptionKey("test").setSubscriptionRegion("test").setOkHttpClient(new OkHttpClient()).build();

CompletableFuture<Optional<Collection<Language>>> languages = api.getAvailableLanguages( new AvailableLanguagesParams().setSourceLanguage("en"));
languages.whenComplete((s, throwable) -> Assertions.assertNull(throwable));
Assertions.assertNotNull(languages.join());
languages.whenComplete((s, throwable) -> assertNull(throwable));
assertNotNull(languages.join());
api.getThreadPool().getExecutorService().shutdown();
}

@Test
@DisplayName("Get languages with empty key")
void getLanguagesEmptyKey() {
public void getLanguagesEmptyKey() {
api = new AzureApiBuilder().setBaseURL(BaseURL.GLOBAL).setSubscriptionKey("").setSubscriptionRegion("test").setOkHttpClient(new OkHttpClient()).build();

CompletableFuture<Optional<Collection<Language>>> languages = api.getAvailableLanguages(new AvailableLanguagesParams());
Assertions.assertDoesNotThrow(languages::join);
languages.whenComplete((s, throwable) -> assertNull(throwable));
api.getThreadPool().getExecutorService().shutdown();
}

@Test
@DisplayName("Get languages with empty source language")
void getLanguagesEmptySourceLanguage() {
public void getLanguagesEmptySourceLanguage() {
api = new AzureApiBuilder().setBaseURL(BaseURL.GLOBAL).setSubscriptionKey("test").setSubscriptionRegion("test").setOkHttpClient(new OkHttpClient()).build();

CompletableFuture<Optional<Collection<Language>>> languages = api.getAvailableLanguages(new AvailableLanguagesParams());
Assertions.assertDoesNotThrow(languages::join);
languages.whenComplete((s, throwable) -> assertNull(throwable));
api.getThreadPool().getExecutorService().shutdown();
}

@Test
@DisplayName("Translate with empty key")
void translateEmptyKey() {
public void translateEmptyKey() {
api = new AzureApiBuilder().setBaseURL(BaseURL.GLOBAL).setSubscriptionKey("").setSubscriptionRegion("test").setOkHttpClient(new OkHttpClient()).build();

TranslateParams params = new TranslateParams("test").setSourceLanguage("en").setTargetLanguages("pt");
CompletableFuture<Optional<TranslationResponse>> translation = api.translate(params);
Assertions.assertThrows(CompletionException.class, translation::join);
assertThrows(CompletionException.class, translation::join);
api.getThreadPool().getExecutorService().shutdown();
}

@Test
@DisplayName("Translate with empty text")
void translateEmptyText() {
public void translateEmptyText() {
api = new AzureApiBuilder().setBaseURL(BaseURL.GLOBAL).setSubscriptionKey("test").setOkHttpClient(new OkHttpClient()).build();

TranslateParams params = new TranslateParams("").setSourceLanguage("en").setTargetLanguages("pt");
CompletableFuture<Optional<TranslationResponse>> translation = api.translate(params);
Optional<TranslationResponse> tr = translation.join();
tr.ifPresent(translations -> Assertions.assertEquals(0, translations.getTranslations().size()));
tr.ifPresent(translations -> assertEquals(0, translations.getTranslations().size()));
api.getThreadPool().getExecutorService().shutdown();
}

@Test
@DisplayName("Translate with empty source language")
void translateEmptySourceLanguage() {
public void translateEmptySourceLanguage() {
api = new AzureApiBuilder().setBaseURL(BaseURL.GLOBAL).setSubscriptionKey("test").setOkHttpClient(new OkHttpClient()).build();

TranslateParams params = new TranslateParams("").setTargetLanguages("pt");
CompletableFuture<Optional<TranslationResponse>> translation = api.translate(params);
Optional<TranslationResponse> tr = translation.join();
tr.ifPresent(translations -> Assertions.assertEquals(0, translations.getTranslations().size()));
tr.ifPresent(translations -> assertEquals(0, translations.getTranslations().size()));
api.getThreadPool().getExecutorService().shutdown();
}

Expand Down

0 comments on commit 865e8db

Please sign in to comment.