Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<module>smartling-reports-api</module>
<module>smartling-glossary-api</module>
<module>smartling-file-translations-api</module>
<module>smartling-mt-router-api</module>
<module>smartling-api-sdk-all</module> <!-- keep last -->
</modules>

Expand Down
24 changes: 24 additions & 0 deletions smartling-mt-router-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.smartling.api</groupId>
<artifactId>smartling-sdk-parent</artifactId>
<version>1.17.1-SNAPSHOT</version>
</parent>

<artifactId>smartling-mt-router-api</artifactId>
<version>1.17.1-SNAPSHOT</version>
<name>Smartling MT Router API</name>

<dependencies>
<dependency>
<groupId>com.smartling.api</groupId>
<artifactId>smartling-api-commons</artifactId>
<version>1.17.1-SNAPSHOT</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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<TranslationPTO> generateAccountTranslations(@PathParam("accountUid") String accountUid, GenerateAccountTranslationCommandPTO command);

}
Original file line number Diff line number Diff line change
@@ -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<MtRouterApi>
{
public MtRouterApiFactory()
{
super();
}

public MtRouterApiFactory(ClientFactory clientFactory)
{
super(clientFactory);
}

@Override
protected Class<MtRouterApi> getApiClass()
{
return MtRouterApi.class;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<SourceStringCommandPTO> items;
private String profileUid;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
artifactId=${project.artifactId}
version=${project.version}
Original file line number Diff line number Diff line change
@@ -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<TranslationPTO> 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<TranslationPTO> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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"
+ " }";
}