Skip to content

Commit

Permalink
Added RandomUserAgent class, updated Readme file, completed tests, mi…
Browse files Browse the repository at this point in the history
…nor changes.
  • Loading branch information
Anton3413 committed Jul 5, 2024
1 parent 64d961b commit 8240cc6
Show file tree
Hide file tree
Showing 11 changed files with 1,723 additions and 72 deletions.
17 changes: 0 additions & 17 deletions src/main/java/reverso/Main.java

This file was deleted.

13 changes: 9 additions & 4 deletions src/main/java/reverso/Reverso.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package reverso;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import reverso.data.parser.HtmlParser;
import reverso.data.request.RandomUserAgent;
import reverso.data.request.SpellCheckRequest;
import reverso.data.response.impl.*;
import reverso.supportedLanguages.Language;
import reverso.supportedLanguages.Voice;
import reverso.language.Language;
import reverso.language.Voice;
import java.io.IOException;
import java.util.*;

Expand Down Expand Up @@ -38,6 +38,7 @@ public SynonymResponse getSynonyms(Language language, String word) {
Connection.Response response;
try {
response = Jsoup.connect(URL)
.header("User-Agent", RandomUserAgent.getRandomUserAgent())
.ignoreHttpErrors(true)
.execute();
} catch (IOException e) {
Expand Down Expand Up @@ -65,14 +66,15 @@ public ContextResponse getContext(Language sourceLanguage, Language targetLangua
contextResponse.setErrorMessage(properties.getProperty("message.error.context.sameLanguage"));
return contextResponse;
}
String URL = CONTEXT_URL + sourceLanguage.toString() + "-" + targetLanguage.toString() + "/" + word;
String URL = CONTEXT_URL + sourceLanguage + "-" + targetLanguage + "/" + word;

Document document;
Map<String, String> contextMap;
String[] translations;
Connection.Response response;
try {
response = Jsoup.connect(URL)
.header("User-Agent", RandomUserAgent.getRandomUserAgent())
.ignoreHttpErrors(true)
.execute();
document = response.parse();
Expand Down Expand Up @@ -107,6 +109,7 @@ public VoiceResponse getVoiceStream(Voice voice, String text) {
Connection.Response response;
try {
response = Jsoup.connect(requestURL)
.header("User-Agent", RandomUserAgent.getRandomUserAgent())
.ignoreContentType(true)
.ignoreHttpErrors(true)
.execute();
Expand Down Expand Up @@ -143,6 +146,7 @@ public ConjugationResponse getWordConjugation(Language language, String word) {
Connection.Response response;
try {
response = Jsoup.connect(URL)
.header("User-Agent", RandomUserAgent.getRandomUserAgent())
.ignoreContentType(true)
.ignoreHttpErrors(true)
.execute();
Expand Down Expand Up @@ -177,6 +181,7 @@ public SpellCheckResponse getSpellCheck(Language language, String text) {
try {
response = Jsoup.connect(SPELLCHECK_URL)
.header("Content-Type", "application/json")
.header("User-Agent", RandomUserAgent.getRandomUserAgent())
.ignoreContentType(true)
.ignoreHttpErrors(true)
.requestBody(requestJson)
Expand Down
1,670 changes: 1,670 additions & 0 deletions src/main/java/reverso/data/request/RandomUserAgent.java

Large diffs are not rendered by default.

43 changes: 0 additions & 43 deletions src/main/java/reverso/data/request/TranslationRequest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reverso.supportedLanguages;
package reverso.language;

public enum Language {
ARABIC("ar", true, null),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package reverso.supportedLanguages;
package reverso.language;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/reverso/getContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.jupiter.api.*;
import reverso.data.response.impl.ContextResponse;
import reverso.supportedLanguages.Language;
import reverso.language.Language;

import java.io.IOException;
import java.util.Properties;
Expand Down
40 changes: 38 additions & 2 deletions src/test/java/reverso/getSpellCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import reverso.data.response.impl.SpellCheckResponse;
import reverso.supportedLanguages.Language;
import reverso.language.Language;
import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException;
import java.util.Properties;
Expand Down Expand Up @@ -34,7 +34,7 @@ void initializeReversoAndProperties() {
}

@Test
void SuccessEnglishSpellCheckRequest() {
void successEnglishSpellCheckRequest() {

String englishText = "here ve have errores, we shuld to fix et";
SpellCheckResponse response = reverso.getSpellCheck(Language.ENGLISH,englishText);
Expand All @@ -46,6 +46,42 @@ void SuccessEnglishSpellCheckRequest() {
assertNotNull(response.getStats());
}

@Test
void successFrenchSpellCheckRequest() {

String frenchText = "Je sui arivé a l'otel en Paris, é j'aim bocoup moin la vil.";

SpellCheckResponse response = reverso.getSpellCheck(Language.FRENCH,frenchText);

assertTrue(response.isOK());
assertNull(response.getErrorMessage());
assertNotNull(response.getCorrectedText());
assertNotEquals(frenchText, response.getCorrectedText());
assertNotNull(response.getStats());
}


@Test
void failedUnsupportedRussianSpellCheckRequest() {

SpellCheckResponse response = reverso.getSpellCheck(Language.RUSSIAN,"good luck");

assertFalse(response.isOK());
assertNotNull(response.getErrorMessage());
assertEquals(properties.getProperty("message.error.spellCheck.unsupportedLanguage"), response.getErrorMessage());
assertNull(response.getCorrectedText());
}

@Test
void failedNoMistakesOnTextSpellCheckRequest() {

SpellCheckResponse response = reverso.getSpellCheck(Language.SPANISH,"hola");

assertFalse(response.isOK());
assertNotNull(response.getErrorMessage());
assertEquals(properties.getProperty("message.error.spellCheck.noErrorsOrMismatchedLanguage"), response.getErrorMessage());
}

@AfterEach
void initializeInstance(){
spellCheckResponse = null;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/reverso/getSynonymsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import reverso.data.response.impl.SynonymResponse;
import reverso.supportedLanguages.Language;
import reverso.language.Language;
import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/reverso/getVoiceStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.jupiter.api.*;
import reverso.data.response.impl.VoiceResponse;
import reverso.supportedLanguages.Voice;
import reverso.language.Voice;

import java.io.IOException;
import java.util.Properties;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/reverso/getWordConjugationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import reverso.data.response.impl.ConjugationResponse;
import reverso.supportedLanguages.Language;
import reverso.language.Language;

import java.io.IOException;
import java.util.Properties;
Expand Down

0 comments on commit 8240cc6

Please sign in to comment.