From f146230605b703e063c78c7a96b72bebc43ffb37 Mon Sep 17 00:00:00 2001 From: SrdjanStevanetic Date: Tue, 31 Oct 2023 19:03:07 +0100 Subject: [PATCH] redis caching --- pom.xml | 1 - .../vocabulary/TranslationAppConstants.java | 1 + translation-tests/pom.xml | 7 +++ .../tests/BaseTranslationTest.java | 5 +- .../tests/IntegrationTestUtils.java | 1 + .../tests/web/TranslationRestIT.java | 54 ++++++++++++++++++ .../content/translation_bad_request_1.json | 3 +- .../content/translation_bad_request_2.json | 3 +- .../content/translation_request.json | 3 +- .../content/translation_request_2.json | 3 +- .../content/translation_request_caching.json | 11 ++++ .../content/translation_with_fallback.json | 3 +- translation-web/pom.xml | 5 ++ .../config/TranslationApiAutoconfig.java | 14 +++++ .../translation/model/TranslationRequest.java | 29 ++++++++++ .../web/TranslationController.java | 57 ++++++++++++++++++- .../web/service/RedisCacheService.java | 55 ++++++++++++++++++ .../src/main/resources/application.properties | 2 +- .../src/main/resources/translation.properties | 7 ++- 19 files changed, 252 insertions(+), 12 deletions(-) create mode 100644 translation-tests/src/integration-test/resources/content/translation_request_caching.json create mode 100644 translation-web/src/main/java/eu/europeana/api/translation/web/service/RedisCacheService.java diff --git a/pom.xml b/pom.xml index 4128b8a3..bd8ffdf0 100644 --- a/pom.xml +++ b/pom.xml @@ -72,7 +72,6 @@ **/model/**/* translation-tests/target/site/jacoco-aggregate/jacoco.xml ${aggregate.report.xml} - diff --git a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/vocabulary/TranslationAppConstants.java b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/vocabulary/TranslationAppConstants.java index 0277b970..ec9c6b8c 100644 --- a/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/vocabulary/TranslationAppConstants.java +++ b/translation-definitions/src/main/java/eu/europeana/api/translation/definitions/vocabulary/TranslationAppConstants.java @@ -20,6 +20,7 @@ public class TranslationAppConstants { public static final String FALLBACK = "fallback"; public static final String SOURCE_LANG = "source"; public static final String TARGET_LANG = "target"; + public static final String CACHING = "caching"; public static final char LANG_DELIMITER = '-'; //api request/response fields diff --git a/translation-tests/pom.xml b/translation-tests/pom.xml index 67fb16bc..8927a9f7 100644 --- a/translation-tests/pom.xml +++ b/translation-tests/pom.xml @@ -44,6 +44,13 @@ mockwebserver test + + + it.ozimov + embedded-redis + 0.7.2 + test + diff --git a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java index b4b84592..10680500 100644 --- a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java +++ b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/BaseTranslationTest.java @@ -38,6 +38,7 @@ public abstract class BaseTranslationTest extends IntegrationTestUtils { protected MockMvc mockMvc; + protected final static int redisPort=6370; protected static final Logger LOG = LogManager.getLogger(BaseTranslationTest.class); @Autowired @@ -92,7 +93,7 @@ private void initServices() throws TranslationServiceConfigurationException, Lan this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } } - + @AfterAll private void stopServices() { //cannot stop the mock server here as all test classes are run by the same runner and the server is static variable @@ -125,6 +126,8 @@ static void setProperties(DynamicPropertyRegistry registry) { registry.add("translation.google.projectId", () -> "project-id-test"); registry.add("translation.google.usehttpclient", () -> "true"); + registry.add("spring.redis.host", () -> "localhost"); + registry.add("spring.redis.port", () -> redisPort); } /** diff --git a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/IntegrationTestUtils.java b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/IntegrationTestUtils.java index 882da6bd..288f6a79 100644 --- a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/IntegrationTestUtils.java +++ b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/IntegrationTestUtils.java @@ -33,6 +33,7 @@ public abstract class IntegrationTestUtils { public static final String TRANSLATION_PANGEANIC_REQUEST_2 = "/content/pangeanic/translate/translate_pangeanic_request_2.json"; public static final String TRANSLATION_PANGEANIC_RESPONSE_2 = "/content/pangeanic/translate/translate_pangeanic_response_2.json"; + public static final String TRANSLATION_REQUEST_CACHING = "/content/translation_request_caching.json"; public static final String TRANSLATION_WITH_FALLBACK = "/content/translation_with_fallback.json"; public static final String TRANSLATION_BAD_REQUEST_1 = "/content/translation_bad_request_1.json"; diff --git a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java index be402082..549d89ce 100644 --- a/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java +++ b/translation-tests/src/integration-test/java/eu/europeana/api/translation/tests/web/TranslationRestIT.java @@ -5,8 +5,10 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONObject; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -28,6 +30,8 @@ import eu.europeana.api.translation.tests.BaseTranslationTest; import eu.europeana.api.translation.tests.web.mock.MockGClient; import eu.europeana.api.translation.tests.web.mock.MockGServiceStub; +import eu.europeana.api.translation.web.service.RedisCacheService; +import redis.embedded.RedisServer; @SpringBootTest @AutoConfigureMockMvc @@ -37,6 +41,9 @@ public class TranslationRestIT extends BaseTranslationTest { @Autowired GoogleTranslationService googleTranslationService; + @Autowired + RedisCacheService redisCacheService; + @Autowired @Qualifier(BeanNames.BEAN_GOOGLE_TRANSLATION_CLIENT_WRAPPER) GoogleTranslationServiceClientWrapper clientWrapper; @@ -92,6 +99,53 @@ void translationPangeanic() throws Exception { assertNotNull(serviceFieldValue); } + @Test + void translationWithCaching() throws Exception { + RedisServer redisServer = new RedisServer(redisPort); + redisServer.start(); + + String requestJson = getJsonStringInput(TRANSLATION_REQUEST_CACHING); + JSONObject reqJsonObj = new JSONObject(requestJson); + JSONArray inputTexts = (JSONArray) reqJsonObj.get(TranslationAppConstants.TEXT); + List inputTextsList = new ArrayList(); + for(int i=0;i redisContent = redisCacheService.getRedisCache(sourceLang, targetLang, inputTextsList); + assertTrue(redisContent.size()==2 && Collections.frequency(redisContent, null)==0); + + String cachedResult = mockMvc + .perform( + post(BASE_URL_TRANSLATE) + .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .content(requestJson)) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(); + + assertNotNull(cachedResult); + JSONObject json = new JSONObject(cachedResult); + String langFieldValue = json.getString(TranslationAppConstants.LANG); + assertNotNull(langFieldValue); + JSONArray translations = json.getJSONArray(TranslationAppConstants.TRANSLATIONS); + assertTrue(translations.length()>0); + + redisCacheService.deleteAll(); + redisServer.stop(); + } + @Test void translationWithServiceParam() throws Exception { String requestJson = getJsonStringInput(TRANSLATION_REQUEST_2); diff --git a/translation-tests/src/integration-test/resources/content/translation_bad_request_1.json b/translation-tests/src/integration-test/resources/content/translation_bad_request_1.json index 50e1e584..4c672ece 100644 --- a/translation-tests/src/integration-test/resources/content/translation_bad_request_1.json +++ b/translation-tests/src/integration-test/resources/content/translation_bad_request_1.json @@ -5,5 +5,6 @@ "text": [ "eine Textzeile auf Deutsch", "eine zweite Textzeile auf Deutsch" - ] + ], + "caching": false } \ No newline at end of file diff --git a/translation-tests/src/integration-test/resources/content/translation_bad_request_2.json b/translation-tests/src/integration-test/resources/content/translation_bad_request_2.json index 315c3a23..5df90b1e 100644 --- a/translation-tests/src/integration-test/resources/content/translation_bad_request_2.json +++ b/translation-tests/src/integration-test/resources/content/translation_bad_request_2.json @@ -6,5 +6,6 @@ "eine Textzeile auf Deutsch", "eine zweite Textzeile auf Deutsch" ], - "service": "wrong-param" + "service": "wrong-param", + "caching": false } \ No newline at end of file diff --git a/translation-tests/src/integration-test/resources/content/translation_request.json b/translation-tests/src/integration-test/resources/content/translation_request.json index 4f909d80..9a8e5032 100644 --- a/translation-tests/src/integration-test/resources/content/translation_request.json +++ b/translation-tests/src/integration-test/resources/content/translation_request.json @@ -6,5 +6,6 @@ "eine Textzeile auf Deutsch", "eine zweite Textzeile auf Deutsch" ], - "service": "GOOGLE" + "service": "GOOGLE", + "caching": false } \ No newline at end of file diff --git a/translation-tests/src/integration-test/resources/content/translation_request_2.json b/translation-tests/src/integration-test/resources/content/translation_request_2.json index 603c03c5..3c51a9e8 100644 --- a/translation-tests/src/integration-test/resources/content/translation_request_2.json +++ b/translation-tests/src/integration-test/resources/content/translation_request_2.json @@ -6,5 +6,6 @@ "eine Textzeile auf Deutsch", "eine zweite Textzeile auf Deutsch" ], - "service": "PANGEANIC" + "service": "PANGEANIC", + "caching": false } \ No newline at end of file diff --git a/translation-tests/src/integration-test/resources/content/translation_request_caching.json b/translation-tests/src/integration-test/resources/content/translation_request_caching.json new file mode 100644 index 00000000..cdb5b2e9 --- /dev/null +++ b/translation-tests/src/integration-test/resources/content/translation_request_caching.json @@ -0,0 +1,11 @@ +{ + "source": "de", + "target": "en", + "detect": false, + "text": [ + "eine Textzeile auf Deutsch", + "eine zweite Textzeile auf Deutsch" + ], + "service": "PANGEANIC", + "caching": true +} \ No newline at end of file diff --git a/translation-tests/src/integration-test/resources/content/translation_with_fallback.json b/translation-tests/src/integration-test/resources/content/translation_with_fallback.json index 8e9b267a..d43fde79 100644 --- a/translation-tests/src/integration-test/resources/content/translation_with_fallback.json +++ b/translation-tests/src/integration-test/resources/content/translation_with_fallback.json @@ -7,5 +7,6 @@ "eine zweite Textzeile auf Deutsch" ], "service":"GOOGLE", - "fallback":"PANGEANIC" + "fallback":"PANGEANIC", + "caching": false } \ No newline at end of file diff --git a/translation-web/pom.xml b/translation-web/pom.xml index 5b2ada37..3c3541d6 100644 --- a/translation-web/pom.xml +++ b/translation-web/pom.xml @@ -90,6 +90,11 @@ + + + org.springframework.boot + spring-boot-starter-data-redis +