From 0d6bde1331957484c426777d16e021203dbf47bf Mon Sep 17 00:00:00 2001 From: Pavlo Myrotiuk Date: Thu, 15 May 2025 13:05:39 +0300 Subject: [PATCH 1/3] JOBS-8225 add mt-router api --- pom.xml | 1 + smartling-mt-router-api/pom.xml | 24 ++++ .../api/mtrouter/v2/MtRouterApi.java | 26 +++++ .../api/mtrouter/v2/MtRouterApiFactory.java | 23 ++++ .../api/mtrouter/v2/pto/ErrorPTO.java | 18 +++ .../GenerateAccountTranslationCommandPTO.java | 20 ++++ .../v2/pto/SourceStringCommandPTO.java | 16 +++ .../api/mtrouter/v2/pto/TranslationPTO.java | 20 ++++ .../api/mtrouter/v2/sdk-project.properties | 2 + .../api/mtrouter/v2/MtRouterApiTest.java | 105 ++++++++++++++++++ .../api/mtrouter/v2/SampleApiResponses.java | 36 ++++++ 11 files changed, 291 insertions(+) create mode 100644 smartling-mt-router-api/pom.xml create mode 100644 smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApi.java create mode 100644 smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApiFactory.java create mode 100644 smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/ErrorPTO.java create mode 100644 smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/GenerateAccountTranslationCommandPTO.java create mode 100644 smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java create mode 100644 smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/TranslationPTO.java create mode 100644 smartling-mt-router-api/src/main/resources/com/smartling/api/mtrouter/v2/sdk-project.properties create mode 100644 smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/MtRouterApiTest.java create mode 100644 smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java diff --git a/pom.xml b/pom.xml index 279e0974..f716f2e8 100644 --- a/pom.xml +++ b/pom.xml @@ -186,6 +186,7 @@ smartling-reports-api smartling-glossary-api smartling-file-translations-api + smartling-mt-router-api smartling-api-sdk-all diff --git a/smartling-mt-router-api/pom.xml b/smartling-mt-router-api/pom.xml new file mode 100644 index 00000000..7d79fbc0 --- /dev/null +++ b/smartling-mt-router-api/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + com.smartling.api + smartling-sdk-parent + 1.17.1-SNAPSHOT + + + smartling-mt-router-api + 1.17.1-SNAPSHOT + Smartling MT Router API + + + + com.smartling.api + smartling-api-commons + 1.17.1-SNAPSHOT + + + + diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApi.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApi.java new file mode 100644 index 00000000..faf468c6 --- /dev/null +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApi.java @@ -0,0 +1,26 @@ +package com.smartling.api.mtrouter.v2; + + +import com.smartling.api.mtrouter.v2.pto.GenerateAccountTranslationCommandPTO; +import com.smartling.api.mtrouter.v2.pto.TranslationPTO; +import com.smartling.api.v2.response.ListResponse; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("/mt-router-api/v2") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public interface MtRouterApi extends AutoCloseable +{ + String GENERATE_ACCOUNT_TRANSLATIONS = "/accounts/{accountUid}/smartling-mt"; + + @POST + @Path(GENERATE_ACCOUNT_TRANSLATIONS) + ListResponse generateAccountTranslations(@PathParam("accountUid") String accountUid, GenerateAccountTranslationCommandPTO command); + +} diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApiFactory.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApiFactory.java new file mode 100644 index 00000000..d5d1b5a2 --- /dev/null +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/MtRouterApiFactory.java @@ -0,0 +1,23 @@ +package com.smartling.api.mtrouter.v2; + +import com.smartling.api.v2.client.AbstractApiFactory; +import com.smartling.api.v2.client.ClientFactory; + +public class MtRouterApiFactory extends AbstractApiFactory +{ + public MtRouterApiFactory() + { + super(); + } + + public MtRouterApiFactory(ClientFactory clientFactory) + { + super(clientFactory); + } + + @Override + protected Class getApiClass() + { + return MtRouterApi.class; + } +} diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/ErrorPTO.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/ErrorPTO.java new file mode 100644 index 00000000..a56ac5b3 --- /dev/null +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/ErrorPTO.java @@ -0,0 +1,18 @@ +package com.smartling.api.mtrouter.v2.pto; + +import com.smartling.api.v2.response.ResponseData; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ErrorPTO implements ResponseData +{ + private String code; + private String key; + private String message; +} diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/GenerateAccountTranslationCommandPTO.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/GenerateAccountTranslationCommandPTO.java new file mode 100644 index 00000000..5a716856 --- /dev/null +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/GenerateAccountTranslationCommandPTO.java @@ -0,0 +1,20 @@ +package com.smartling.api.mtrouter.v2.pto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class GenerateAccountTranslationCommandPTO +{ + private String sourceLocaleId; + private String targetLocaleId; + private List items; + private String profileUid; +} diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java new file mode 100644 index 00000000..db01eddd --- /dev/null +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java @@ -0,0 +1,16 @@ +package com.smartling.api.mtrouter.v2.pto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SourceStringCommandPTO +{ + private String key; + private String sourceText; +} diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/TranslationPTO.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/TranslationPTO.java new file mode 100644 index 00000000..d5897939 --- /dev/null +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/TranslationPTO.java @@ -0,0 +1,20 @@ +package com.smartling.api.mtrouter.v2.pto; + +import com.smartling.api.v2.response.ResponseData; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Builder +@Data +@AllArgsConstructor +@NoArgsConstructor +public class TranslationPTO implements ResponseData +{ + private String key; + private String mtUid; + private String translationText; + private ErrorPTO error; + private String provider; +} diff --git a/smartling-mt-router-api/src/main/resources/com/smartling/api/mtrouter/v2/sdk-project.properties b/smartling-mt-router-api/src/main/resources/com/smartling/api/mtrouter/v2/sdk-project.properties new file mode 100644 index 00000000..0b77013c --- /dev/null +++ b/smartling-mt-router-api/src/main/resources/com/smartling/api/mtrouter/v2/sdk-project.properties @@ -0,0 +1,2 @@ +artifactId=${project.artifactId} +version=${project.version} diff --git a/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/MtRouterApiTest.java b/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/MtRouterApiTest.java new file mode 100644 index 00000000..5b63fde6 --- /dev/null +++ b/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/MtRouterApiTest.java @@ -0,0 +1,105 @@ +package com.smartling.api.mtrouter.v2; + +import com.smartling.api.mtrouter.v2.pto.GenerateAccountTranslationCommandPTO; +import com.smartling.api.mtrouter.v2.pto.TranslationPTO; +import com.smartling.api.v2.client.ClientConfiguration; +import com.smartling.api.v2.client.DefaultClientConfiguration; +import com.smartling.api.v2.client.auth.BearerAuthStaticTokenFilter; +import com.smartling.api.v2.response.ListResponse; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import javax.ws.rs.core.HttpHeaders; + +import static com.smartling.api.mtrouter.v2.SampleApiResponses.ERRONEOUS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY; +import static com.smartling.api.mtrouter.v2.SampleApiResponses.SUCCESS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY; +import static com.smartling.api.mtrouter.v2.SampleApiResponses.SUCCESS_RESPONSE_ENVELOPE; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +public class MtRouterApiTest +{ + private static final String ACCOUNT_UID = "a11223344"; + + private MockWebServer mockWebServer; + private MtRouterApi mtRouterApi; + + @Before + public void setUp() throws Exception + { + mockWebServer = new MockWebServer(); + mockWebServer.start(); + + final MtRouterApiFactory factory = new MtRouterApiFactory(); + final BearerAuthStaticTokenFilter tokenFilter = new BearerAuthStaticTokenFilter("foo"); + final ClientConfiguration config = DefaultClientConfiguration.builder().baseUrl(mockWebServer.url("/").url()).build(); + + mtRouterApi = factory.buildApi(tokenFilter, config); + } + + @After + public void tearDown() throws Exception + { + mockWebServer.shutdown(); + } + + private void assignResponse(final int httpStatusCode, final String body) + { + final MockResponse response = new MockResponse() + .setResponseCode(httpStatusCode) + .setHeader(HttpHeaders.CONTENT_LENGTH, body.length()) + .setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8") + .setBody(body); + + mockWebServer.enqueue(response); + } + + @Test + public void testGenerateAccountTranslationErroneousResponse() + { + assignResponse(200, String.format(SUCCESS_RESPONSE_ENVELOPE, ERRONEOUS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY)); + final GenerateAccountTranslationCommandPTO command = new GenerateAccountTranslationCommandPTO(); + + ListResponse response = mtRouterApi.generateAccountTranslations(ACCOUNT_UID, command); + + assertNotNull(response); + assertEquals(1, response.getTotalCount()); + assertEquals(1, response.getItems().size()); + + TranslationPTO translation = response.getItems().get(0); + assertNotNull(translation); + assertNull(translation.getMtUid()); + assertNull(translation.getTranslationText()); + assertNull(translation.getProvider()); + assertNotNull(translation.getError()); + assertEquals("test-key", translation.getKey()); + assertEquals("INVALID_SOURCE_TEXT", translation.getError().getCode()); + assertEquals("validation.error", translation.getError().getKey()); + assertEquals("Source text was invalid", translation.getError().getMessage()); + } + + @Test + public void testGenerateAccountTranslationSuccessResponse() + { + assignResponse(200, String.format(SUCCESS_RESPONSE_ENVELOPE, SUCCESS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY)); + final GenerateAccountTranslationCommandPTO command = new GenerateAccountTranslationCommandPTO(); + + ListResponse response = mtRouterApi.generateAccountTranslations(ACCOUNT_UID, command); + + assertNotNull(response); + assertEquals(1, response.getTotalCount()); + assertEquals(1, response.getItems().size()); + + TranslationPTO translation = response.getItems().get(0); + assertNotNull(translation); + assertEquals("test-key", translation.getKey()); + assertEquals("a36z4bunraj1", translation.getMtUid()); + assertEquals("Zu übersetzenden Text testen", translation.getTranslationText()); + assertNull(translation.getError()); + assertEquals("AUTO_SELECT_PROVIDER", translation.getProvider()); + } +} diff --git a/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java b/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java new file mode 100644 index 00000000..685e6cb6 --- /dev/null +++ b/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java @@ -0,0 +1,36 @@ +package com.smartling.api.mtrouter.v2; + +interface SampleApiResponses +{ + String SUCCESS_RESPONSE_ENVELOPE = "{\"response\":{\"code\":\"SUCCESS\",\"data\":%s}})"; + + String ERRONEOUS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY = "{\n" + + " \"totalCount\": 1,\n" + + " \"items\": [\n" + + " {\n" + + " \"key\": \"test-key\",\n" + + " \"mtUid\": null,\n" + + " \"translationText\": null,\n" + + " \"error\": {\n" + + " \"code\": \"INVALID_SOURCE_TEXT\",\n" + + " \"key\": \"validation.error\",\n" + + " \"message\": \"Source text was invalid\"\n" + + " },\n" + + " \"provider\": null\n" + + " }\n" + + " ]\n" + + " }"; + + String SUCCESS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY = " {\n" + + " \"totalCount\": 1,\n" + + " \"items\": [\n" + + " {\n" + + " \"key\": \"test-key\",\n" + + " \"mtUid\": \"a36z4bunraj1\",\n" + + " \"translationText\": \"Zu übersetzenden Text testen\",\n" + + " \"error\": null,\n" + + " \"provider\": \"AUTO_SELECT_PROVIDER\"\n" + + " }\n" + + " ]\n" + + " }"; +} From 428046d6af13f5f988227ebbc7f7e769e55f2c66 Mon Sep 17 00:00:00 2001 From: PavloMyrotiuk Date: Thu, 15 May 2025 14:04:16 +0300 Subject: [PATCH 2/3] Update smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../java/com/smartling/api/mtrouter/v2/SampleApiResponses.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java b/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java index 685e6cb6..12fbf86e 100644 --- a/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java +++ b/smartling-mt-router-api/src/test/java/com/smartling/api/mtrouter/v2/SampleApiResponses.java @@ -2,7 +2,7 @@ interface SampleApiResponses { - String SUCCESS_RESPONSE_ENVELOPE = "{\"response\":{\"code\":\"SUCCESS\",\"data\":%s}})"; + String SUCCESS_RESPONSE_ENVELOPE = "{\"response\":{\"code\":\"SUCCESS\",\"data\":%s}}"; String ERRONEOUS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY = "{\n" + " \"totalCount\": 1,\n" From ba56460695311673cc9cec9f5895c1a64e9a20da Mon Sep 17 00:00:00 2001 From: PavloMyrotiuk Date: Thu, 15 May 2025 14:04:39 +0300 Subject: [PATCH 3/3] Update smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java index db01eddd..31e42fd7 100644 --- a/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java +++ b/smartling-mt-router-api/src/main/java/com/smartling/api/mtrouter/v2/pto/SourceStringCommandPTO.java @@ -11,6 +11,6 @@ @NoArgsConstructor public class SourceStringCommandPTO { - private String key; - private String sourceText; + private String key; + private String sourceText; }