Skip to content

Commit

Permalink
revert translation issue
Browse files Browse the repository at this point in the history
  • Loading branch information
athan29 committed Jun 26, 2023
1 parent 75c67e5 commit 02d6c0b
Show file tree
Hide file tree
Showing 10 changed files with 455 additions and 88 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,65 @@ public Text translate(Text text, Language sourceLanguage,

//translates everything paragraph by paragraph. if it cannot translate
//everything, it translates nothing.
try {
return getTranslatedText(targetLanguage, resultList, translatedTitle,
text);
} catch (ExecutionException e) {
System.out.println("ExecutionException");
} catch (InterruptedException e) {
System.out.println("InterruptedException");
}

return text;
// try {
// return getTranslatedText(targetLanguage, resultList, translatedTitle,
// text);
// } catch (ExecutionException e) {
// System.out.println("ExecutionException");
// } catch (InterruptedException e) {
// System.out.println("InterruptedException");
// }

// return text;
return getTranslatedText(targetLanguage, resultList, translatedTitle,
text);
}

protected Text getTranslatedText(Language targetLanguage,
List<Future<TranslateTextResult>> resultList,
Future<TranslateTextResult> translatedTitle,
Text text)
throws ExecutionException, InterruptedException {
// protected Text getTranslatedText(Language targetLanguage,
// List<Future<TranslateTextResult>> resultList,
// Future<TranslateTextResult> translatedTitle,
// Text text)
// throws ExecutionException, InterruptedException {

List<String> translatedParagraphs = new ArrayList<>();
// List<String> translatedParagraphs = new ArrayList<>();

// //translate the body
// for (Future<TranslateTextResult> result : resultList) {
// translatedParagraphs.add(result.get().getTranslatedText());
// }

// //translate the title
// text.setTitle(translatedTitle.get().getTranslatedText());

// text.setContent(translatedParagraphs);
// text.setLanguage(targetLanguage);
// return text;
// }

private Text getTranslatedText(Language targetLanguage,
List<Future<TranslateTextResult>> resultList,
Future<TranslateTextResult> translatedTitle,
Text text) {
List<String> translatedParagraphs = new ArrayList<>();
//translate the body
for (Future<TranslateTextResult> result : resultList) {
translatedParagraphs.add(result.get().getTranslatedText());
}
resultList.forEach(x -> {
try {
translatedParagraphs.add(x.get().getTranslatedText());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});

//translate the title
text.setTitle(translatedTitle.get().getTranslatedText());
try {
text.setTitle(translatedTitle.get().getTranslatedText());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}

text.setContent(translatedParagraphs);
text.setLanguage(targetLanguage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
// import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
// import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -16,56 +16,59 @@
import com.awsomenlp.lambda.config.objects.Author;
import com.awsomenlp.lambda.config.objects.Language;
import com.awsomenlp.lambda.config.objects.Text;
import java.util.ArrayList;
// import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
// import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
public class AmazonTranslateTest {


public class AmazonTranslateTest {
AmazonTranslate amazonTranslate;
AmazonTranslateAsync translateAsync;


/**
* Set up before each.
*/
@BeforeEach
public void setup() {
translateAsync = mock(AmazonTranslateAsync.class);
amazonTranslate = new AmazonTranslate();
amazonTranslate.setTranslateAsync(translateAsync);
}

/**
* Tests the translate method by mocking the AmazonTranslateAsync object.
*/
@Test
public void testTranslate() {
// Prepare input
AmazonTranslateAsync translateAsync = mock(AmazonTranslateAsync.class);
AmazonTranslate amazonTranslate = new AmazonTranslate();
amazonTranslate.setTranslateAsync(translateAsync);

List<Author> authors = List.of(new Author("Mr.", "Jaden", "Smith"));
List<String> content = List.of("Come here, mama, I know you want me\n",
"Food from the soul, I know you're hungry\n");
Text text = new Text(Language.ENGLISH, "Ninety", authors, content);

TranslateTextResult paragraph1 = new TranslateTextResult();
paragraph1.setTranslatedText("sumn translated");
paragraph1.setSourceLanguageCode("en");
paragraph1.setTargetLanguageCode("tr");

TranslateTextResult paragraph2 = new TranslateTextResult();
paragraph2.setTranslatedText("sumn else translated");
paragraph2.setSourceLanguageCode("en");
paragraph2.setTargetLanguageCode("tr");

TranslateTextResult title = new TranslateTextResult();
title.setTranslatedText("noventa");
title.setSourceLanguageCode("en");
title.setTargetLanguageCode("tr");

when(translateAsync.translateTextAsync(any())).thenReturn(
CompletableFuture.completedFuture(paragraph1),
CompletableFuture.completedFuture(paragraph2),
CompletableFuture.completedFuture(title)
);

// Test
Text translatedText =
amazonTranslate.translate(text, Language.ENGLISH, Language.TURKISH);

// Assert
Text expectedText =
new Text(Language.TURKISH, title.getTranslatedText(), authors,
Expand All @@ -75,57 +78,110 @@ public void testTranslate() {

verify(translateAsync, times(3)).translateTextAsync(any());
}

@Test
void testTranslateFailExecutionException()
throws ExecutionException, InterruptedException {
// Prepare input
Text originalText = new Text(Language.ENGLISH, "Title",
new ArrayList<>(), new ArrayList<>());
Text textCopy =
originalText; // Copy the original text to compare later

AmazonTranslate amazonTranslate = spy(new AmazonTranslate(
"0")); // Create a spy of the AmazonTranslate object

// Stub the getTranslatedText method to throw an exception
doThrow(new ExecutionException(new Exception()))
.when(amazonTranslate)
.getTranslatedText(any(), any(), any(),
any());

//Test
Text result = amazonTranslate
.translate(originalText, Language.ENGLISH, Language.TURKISH);

// Assert that the text hasn't been translated
assertEquals(textCopy, result);
}

@Test
void testTranslateFailInterruptedException()
throws ExecutionException, InterruptedException {
// Prepare input
Text originalText = new Text(Language.ENGLISH, "Title",
new ArrayList<>(), new ArrayList<>());
Text textCopy =
originalText; // Copy the original text to compare later

AmazonTranslate amazonTranslate = spy(new AmazonTranslate(
"0")); // Create a spy of the AmazonTranslate object

// Stub the getTranslatedText method to throw an exception
doThrow(new InterruptedException())
.when(amazonTranslate)
.getTranslatedText(any(), any(), any(),
any());

//Test
Text result = amazonTranslate
.translate(originalText, Language.ENGLISH, Language.TURKISH);

// Assert that the text hasn't been translated
assertEquals(textCopy, result);
}
}
// public class AmazonTranslateTest {


// /**
// * Tests the translate method by mocking the AmazonTranslateAsync object.
// */
// @Test
// public void testTranslate() {
// // Prepare input
// AmazonTranslateAsync translateAsync = mock(AmazonTranslateAsync.class);
// AmazonTranslate amazonTranslate = new AmazonTranslate();
// amazonTranslate.setTranslateAsync(translateAsync);

// List<Author> authors = List.of(new Author("Mr.", "Jaden", "Smith"));
// List<String> content = List.of("Come here, mama, I know you want me\n",
// "Food from the soul, I know you're hungry\n");
// Text text = new Text(Language.ENGLISH, "Ninety", authors, content);

// TranslateTextResult paragraph1 = new TranslateTextResult();
// paragraph1.setTranslatedText("sumn translated");
// paragraph1.setSourceLanguageCode("en");
// paragraph1.setTargetLanguageCode("tr");

// TranslateTextResult paragraph2 = new TranslateTextResult();
// paragraph2.setTranslatedText("sumn else translated");
// paragraph2.setSourceLanguageCode("en");
// paragraph2.setTargetLanguageCode("tr");

// TranslateTextResult title = new TranslateTextResult();
// title.setTranslatedText("noventa");
// title.setSourceLanguageCode("en");
// title.setTargetLanguageCode("tr");

// when(translateAsync.translateTextAsync(any())).thenReturn(
// CompletableFuture.completedFuture(paragraph1),
// CompletableFuture.completedFuture(paragraph2),
// CompletableFuture.completedFuture(title)
// );

// // Test
// Text translatedText =
// amazonTranslate.translate(text, Language.ENGLISH, Language.TURKISH);

// // Assert
// Text expectedText =
// new Text(Language.TURKISH, title.getTranslatedText(), authors,
// List.of(paragraph1.getTranslatedText(),
// paragraph2.getTranslatedText()));
// assertEquals(expectedText, translatedText);

// verify(translateAsync, times(3)).translateTextAsync(any());
// }

// @Test
// void testTranslateFailExecutionException()
// throws ExecutionException, InterruptedException {
// // Prepare input
// Text originalText = new Text(Language.ENGLISH, "Title",
// new ArrayList<>(), new ArrayList<>());
// Text textCopy =
// originalText; // Copy the original text to compare later

// AmazonTranslate amazonTranslate = spy(new AmazonTranslate(
// "0")); // Create a spy of the AmazonTranslate object

// // Stub the getTranslatedText method to throw an exception
// doThrow(new ExecutionException(new Exception()))
// .when(amazonTranslate)
// .getTranslatedText(any(), any(), any(),
// any());

// //Test
// Text result = amazonTranslate
// .translate(originalText, Language.ENGLISH, Language.TURKISH);

// // Assert that the text hasn't been translated
// assertEquals(textCopy, result);
// }

// @Test
// void testTranslateFailInterruptedException()
// throws ExecutionException, InterruptedException {
// // Prepare input
// Text originalText = new Text(Language.ENGLISH, "Title",
// new ArrayList<>(), new ArrayList<>());
// Text textCopy =
// originalText; // Copy the original text to compare later

// AmazonTranslate amazonTranslate = spy(new AmazonTranslate(
// "0")); // Create a spy of the AmazonTranslate object

// // Stub the getTranslatedText method to throw an exception
// doThrow(new InterruptedException())
// .when(amazonTranslate)
// .getTranslatedText(any(), any(), any(),
// any());

// //Test
// Text result = amazonTranslate
// .translate(originalText, Language.ENGLISH, Language.TURKISH);

// // Assert that the text hasn't been translated
// assertEquals(textCopy, result);
// }
// }

Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 02d6c0b

Please sign in to comment.