|
| 1 | +package com.smartling.api.mtrouter.v2; |
| 2 | + |
| 3 | +import com.smartling.api.mtrouter.v2.pto.GenerateAccountTranslationCommandPTO; |
| 4 | +import com.smartling.api.mtrouter.v2.pto.TranslationPTO; |
| 5 | +import com.smartling.api.v2.client.ClientConfiguration; |
| 6 | +import com.smartling.api.v2.client.DefaultClientConfiguration; |
| 7 | +import com.smartling.api.v2.client.auth.BearerAuthStaticTokenFilter; |
| 8 | +import com.smartling.api.v2.response.ListResponse; |
| 9 | +import okhttp3.mockwebserver.MockResponse; |
| 10 | +import okhttp3.mockwebserver.MockWebServer; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +import javax.ws.rs.core.HttpHeaders; |
| 16 | + |
| 17 | +import static com.smartling.api.mtrouter.v2.SampleApiResponses.ERRONEOUS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY; |
| 18 | +import static com.smartling.api.mtrouter.v2.SampleApiResponses.SUCCESS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY; |
| 19 | +import static com.smartling.api.mtrouter.v2.SampleApiResponses.SUCCESS_RESPONSE_ENVELOPE; |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNotNull; |
| 22 | +import static org.junit.Assert.assertNull; |
| 23 | + |
| 24 | +public class MtRouterApiTest |
| 25 | +{ |
| 26 | + private static final String ACCOUNT_UID = "a11223344"; |
| 27 | + |
| 28 | + private MockWebServer mockWebServer; |
| 29 | + private MtRouterApi mtRouterApi; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setUp() throws Exception |
| 33 | + { |
| 34 | + mockWebServer = new MockWebServer(); |
| 35 | + mockWebServer.start(); |
| 36 | + |
| 37 | + final MtRouterApiFactory factory = new MtRouterApiFactory(); |
| 38 | + final BearerAuthStaticTokenFilter tokenFilter = new BearerAuthStaticTokenFilter("foo"); |
| 39 | + final ClientConfiguration config = DefaultClientConfiguration.builder().baseUrl(mockWebServer.url("/").url()).build(); |
| 40 | + |
| 41 | + mtRouterApi = factory.buildApi(tokenFilter, config); |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + public void tearDown() throws Exception |
| 46 | + { |
| 47 | + mockWebServer.shutdown(); |
| 48 | + } |
| 49 | + |
| 50 | + private void assignResponse(final int httpStatusCode, final String body) |
| 51 | + { |
| 52 | + final MockResponse response = new MockResponse() |
| 53 | + .setResponseCode(httpStatusCode) |
| 54 | + .setHeader(HttpHeaders.CONTENT_LENGTH, body.length()) |
| 55 | + .setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8") |
| 56 | + .setBody(body); |
| 57 | + |
| 58 | + mockWebServer.enqueue(response); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testGenerateAccountTranslationErroneousResponse() |
| 63 | + { |
| 64 | + assignResponse(200, String.format(SUCCESS_RESPONSE_ENVELOPE, ERRONEOUS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY)); |
| 65 | + final GenerateAccountTranslationCommandPTO command = new GenerateAccountTranslationCommandPTO(); |
| 66 | + |
| 67 | + ListResponse<TranslationPTO> response = mtRouterApi.generateAccountTranslations(ACCOUNT_UID, command); |
| 68 | + |
| 69 | + assertNotNull(response); |
| 70 | + assertEquals(1, response.getTotalCount()); |
| 71 | + assertEquals(1, response.getItems().size()); |
| 72 | + |
| 73 | + TranslationPTO translation = response.getItems().get(0); |
| 74 | + assertNotNull(translation); |
| 75 | + assertNull(translation.getMtUid()); |
| 76 | + assertNull(translation.getTranslationText()); |
| 77 | + assertNull(translation.getProvider()); |
| 78 | + assertNotNull(translation.getError()); |
| 79 | + assertEquals("test-key", translation.getKey()); |
| 80 | + assertEquals("INVALID_SOURCE_TEXT", translation.getError().getCode()); |
| 81 | + assertEquals("validation.error", translation.getError().getKey()); |
| 82 | + assertEquals("Source text was invalid", translation.getError().getMessage()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testGenerateAccountTranslationSuccessResponse() |
| 87 | + { |
| 88 | + assignResponse(200, String.format(SUCCESS_RESPONSE_ENVELOPE, SUCCESS_GENERATE_ACCOUNT_TRANSLATION_RESPONSE_BODY)); |
| 89 | + final GenerateAccountTranslationCommandPTO command = new GenerateAccountTranslationCommandPTO(); |
| 90 | + |
| 91 | + ListResponse<TranslationPTO> response = mtRouterApi.generateAccountTranslations(ACCOUNT_UID, command); |
| 92 | + |
| 93 | + assertNotNull(response); |
| 94 | + assertEquals(1, response.getTotalCount()); |
| 95 | + assertEquals(1, response.getItems().size()); |
| 96 | + |
| 97 | + TranslationPTO translation = response.getItems().get(0); |
| 98 | + assertNotNull(translation); |
| 99 | + assertEquals("test-key", translation.getKey()); |
| 100 | + assertEquals("a36z4bunraj1", translation.getMtUid()); |
| 101 | + assertEquals("Zu übersetzenden Text testen", translation.getTranslationText()); |
| 102 | + assertNull(translation.getError()); |
| 103 | + assertEquals("AUTO_SELECT_PROVIDER", translation.getProvider()); |
| 104 | + } |
| 105 | +} |
0 commit comments