|
32 | 32 | import io.grpc.Status;
|
33 | 33 | import io.grpc.StatusRuntimeException;
|
34 | 34 | import java.io.IOException;
|
| 35 | +import java.util.ArrayList; |
35 | 36 | import java.util.Arrays;
|
36 | 37 | import java.util.List;
|
37 | 38 | import java.util.UUID;
|
@@ -81,6 +82,62 @@ public void tearDown() throws Exception {
|
81 | 82 | client.close();
|
82 | 83 | }
|
83 | 84 |
|
| 85 | + @Test |
| 86 | + @SuppressWarnings("all") |
| 87 | + public void translateTextTest() { |
| 88 | + TranslateTextResponse expectedResponse = TranslateTextResponse.newBuilder().build(); |
| 89 | + mockTranslationService.addResponse(expectedResponse); |
| 90 | + |
| 91 | + List<String> contents = new ArrayList<>(); |
| 92 | + String targetLanguageCode = "targetLanguageCode1323228230"; |
| 93 | + LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); |
| 94 | + TranslateTextRequest request = |
| 95 | + TranslateTextRequest.newBuilder() |
| 96 | + .addAllContents(contents) |
| 97 | + .setTargetLanguageCode(targetLanguageCode) |
| 98 | + .setParent(parent.toString()) |
| 99 | + .build(); |
| 100 | + |
| 101 | + TranslateTextResponse actualResponse = client.translateText(request); |
| 102 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 103 | + |
| 104 | + List<AbstractMessage> actualRequests = mockTranslationService.getRequests(); |
| 105 | + Assert.assertEquals(1, actualRequests.size()); |
| 106 | + TranslateTextRequest actualRequest = (TranslateTextRequest) actualRequests.get(0); |
| 107 | + |
| 108 | + Assert.assertEquals(contents, actualRequest.getContentsList()); |
| 109 | + Assert.assertEquals(targetLanguageCode, actualRequest.getTargetLanguageCode()); |
| 110 | + Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); |
| 111 | + Assert.assertTrue( |
| 112 | + channelProvider.isHeaderSent( |
| 113 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 114 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @SuppressWarnings("all") |
| 119 | + public void translateTextExceptionTest() throws Exception { |
| 120 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 121 | + mockTranslationService.addException(exception); |
| 122 | + |
| 123 | + try { |
| 124 | + List<String> contents = new ArrayList<>(); |
| 125 | + String targetLanguageCode = "targetLanguageCode1323228230"; |
| 126 | + LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); |
| 127 | + TranslateTextRequest request = |
| 128 | + TranslateTextRequest.newBuilder() |
| 129 | + .addAllContents(contents) |
| 130 | + .setTargetLanguageCode(targetLanguageCode) |
| 131 | + .setParent(parent.toString()) |
| 132 | + .build(); |
| 133 | + |
| 134 | + client.translateText(request); |
| 135 | + Assert.fail("No exception raised"); |
| 136 | + } catch (InvalidArgumentException e) { |
| 137 | + // Expected exception |
| 138 | + } |
| 139 | + } |
| 140 | + |
84 | 141 | @Test
|
85 | 142 | @SuppressWarnings("all")
|
86 | 143 | public void detectLanguageTest() {
|
@@ -171,6 +228,88 @@ public void getSupportedLanguagesExceptionTest() throws Exception {
|
171 | 228 | }
|
172 | 229 | }
|
173 | 230 |
|
| 231 | + @Test |
| 232 | + @SuppressWarnings("all") |
| 233 | + public void batchTranslateTextTest() throws Exception { |
| 234 | + long totalCharacters = 1368640955L; |
| 235 | + long translatedCharacters = 1337326221L; |
| 236 | + long failedCharacters = 1723028396L; |
| 237 | + BatchTranslateResponse expectedResponse = |
| 238 | + BatchTranslateResponse.newBuilder() |
| 239 | + .setTotalCharacters(totalCharacters) |
| 240 | + .setTranslatedCharacters(translatedCharacters) |
| 241 | + .setFailedCharacters(failedCharacters) |
| 242 | + .build(); |
| 243 | + Operation resultOperation = |
| 244 | + Operation.newBuilder() |
| 245 | + .setName("batchTranslateTextTest") |
| 246 | + .setDone(true) |
| 247 | + .setResponse(Any.pack(expectedResponse)) |
| 248 | + .build(); |
| 249 | + mockTranslationService.addResponse(resultOperation); |
| 250 | + |
| 251 | + LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); |
| 252 | + String sourceLanguageCode = "sourceLanguageCode1687263568"; |
| 253 | + List<String> targetLanguageCodes = new ArrayList<>(); |
| 254 | + List<InputConfig> inputConfigs = new ArrayList<>(); |
| 255 | + OutputConfig outputConfig = OutputConfig.newBuilder().build(); |
| 256 | + BatchTranslateTextRequest request = |
| 257 | + BatchTranslateTextRequest.newBuilder() |
| 258 | + .setParent(parent.toString()) |
| 259 | + .setSourceLanguageCode(sourceLanguageCode) |
| 260 | + .addAllTargetLanguageCodes(targetLanguageCodes) |
| 261 | + .addAllInputConfigs(inputConfigs) |
| 262 | + .setOutputConfig(outputConfig) |
| 263 | + .build(); |
| 264 | + |
| 265 | + BatchTranslateResponse actualResponse = client.batchTranslateTextAsync(request).get(); |
| 266 | + Assert.assertEquals(expectedResponse, actualResponse); |
| 267 | + |
| 268 | + List<AbstractMessage> actualRequests = mockTranslationService.getRequests(); |
| 269 | + Assert.assertEquals(1, actualRequests.size()); |
| 270 | + BatchTranslateTextRequest actualRequest = (BatchTranslateTextRequest) actualRequests.get(0); |
| 271 | + |
| 272 | + Assert.assertEquals(parent, LocationName.parse(actualRequest.getParent())); |
| 273 | + Assert.assertEquals(sourceLanguageCode, actualRequest.getSourceLanguageCode()); |
| 274 | + Assert.assertEquals(targetLanguageCodes, actualRequest.getTargetLanguageCodesList()); |
| 275 | + Assert.assertEquals(inputConfigs, actualRequest.getInputConfigsList()); |
| 276 | + Assert.assertEquals(outputConfig, actualRequest.getOutputConfig()); |
| 277 | + Assert.assertTrue( |
| 278 | + channelProvider.isHeaderSent( |
| 279 | + ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), |
| 280 | + GaxGrpcProperties.getDefaultApiClientHeaderPattern())); |
| 281 | + } |
| 282 | + |
| 283 | + @Test |
| 284 | + @SuppressWarnings("all") |
| 285 | + public void batchTranslateTextExceptionTest() throws Exception { |
| 286 | + StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); |
| 287 | + mockTranslationService.addException(exception); |
| 288 | + |
| 289 | + try { |
| 290 | + LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); |
| 291 | + String sourceLanguageCode = "sourceLanguageCode1687263568"; |
| 292 | + List<String> targetLanguageCodes = new ArrayList<>(); |
| 293 | + List<InputConfig> inputConfigs = new ArrayList<>(); |
| 294 | + OutputConfig outputConfig = OutputConfig.newBuilder().build(); |
| 295 | + BatchTranslateTextRequest request = |
| 296 | + BatchTranslateTextRequest.newBuilder() |
| 297 | + .setParent(parent.toString()) |
| 298 | + .setSourceLanguageCode(sourceLanguageCode) |
| 299 | + .addAllTargetLanguageCodes(targetLanguageCodes) |
| 300 | + .addAllInputConfigs(inputConfigs) |
| 301 | + .setOutputConfig(outputConfig) |
| 302 | + .build(); |
| 303 | + |
| 304 | + client.batchTranslateTextAsync(request).get(); |
| 305 | + Assert.fail("No exception raised"); |
| 306 | + } catch (ExecutionException e) { |
| 307 | + Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); |
| 308 | + InvalidArgumentException apiException = (InvalidArgumentException) e.getCause(); |
| 309 | + Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); |
| 310 | + } |
| 311 | + } |
| 312 | + |
174 | 313 | @Test
|
175 | 314 | @SuppressWarnings("all")
|
176 | 315 | public void createGlossaryTest() throws Exception {
|
|
0 commit comments