• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.translate.v3beta1;
18 
19 import static com.google.cloud.translate.v3beta1.TranslationServiceClient.ListGlossariesPagedResponse;
20 
21 import com.google.api.gax.core.NoCredentialsProvider;
22 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
23 import com.google.api.gax.httpjson.testing.MockHttpService;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.ApiException;
26 import com.google.api.gax.rpc.ApiExceptionFactory;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.api.gax.rpc.testing.FakeStatusCode;
30 import com.google.cloud.translate.v3beta1.stub.HttpJsonTranslationServiceStub;
31 import com.google.common.collect.Lists;
32 import com.google.longrunning.Operation;
33 import com.google.protobuf.Any;
34 import com.google.protobuf.Timestamp;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.concurrent.ExecutionException;
41 import javax.annotation.Generated;
42 import org.junit.After;
43 import org.junit.AfterClass;
44 import org.junit.Assert;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 
49 @Generated("by gapic-generator-java")
50 public class TranslationServiceClientHttpJsonTest {
51   private static MockHttpService mockService;
52   private static TranslationServiceClient client;
53 
54   @BeforeClass
startStaticServer()55   public static void startStaticServer() throws IOException {
56     mockService =
57         new MockHttpService(
58             HttpJsonTranslationServiceStub.getMethodDescriptors(),
59             TranslationServiceSettings.getDefaultEndpoint());
60     TranslationServiceSettings settings =
61         TranslationServiceSettings.newHttpJsonBuilder()
62             .setTransportChannelProvider(
63                 TranslationServiceSettings.defaultHttpJsonTransportProviderBuilder()
64                     .setHttpTransport(mockService)
65                     .build())
66             .setCredentialsProvider(NoCredentialsProvider.create())
67             .build();
68     client = TranslationServiceClient.create(settings);
69   }
70 
71   @AfterClass
stopServer()72   public static void stopServer() {
73     client.close();
74   }
75 
76   @Before
setUp()77   public void setUp() {}
78 
79   @After
tearDown()80   public void tearDown() throws Exception {
81     mockService.reset();
82   }
83 
84   @Test
translateTextTest()85   public void translateTextTest() throws Exception {
86     TranslateTextResponse expectedResponse =
87         TranslateTextResponse.newBuilder()
88             .addAllTranslations(new ArrayList<Translation>())
89             .addAllGlossaryTranslations(new ArrayList<Translation>())
90             .build();
91     mockService.addResponse(expectedResponse);
92 
93     TranslateTextRequest request =
94         TranslateTextRequest.newBuilder()
95             .addAllContents(new ArrayList<String>())
96             .setMimeType("mimeType-1392120434")
97             .setSourceLanguageCode("sourceLanguageCode1645917472")
98             .setTargetLanguageCode("targetLanguageCode-106414698")
99             .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
100             .setModel("model104069929")
101             .setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder().build())
102             .putAllLabels(new HashMap<String, String>())
103             .build();
104 
105     TranslateTextResponse actualResponse = client.translateText(request);
106     Assert.assertEquals(expectedResponse, actualResponse);
107 
108     List<String> actualRequests = mockService.getRequestPaths();
109     Assert.assertEquals(1, actualRequests.size());
110 
111     String apiClientHeaderKey =
112         mockService
113             .getRequestHeaders()
114             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
115             .iterator()
116             .next();
117     Assert.assertTrue(
118         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
119             .matcher(apiClientHeaderKey)
120             .matches());
121   }
122 
123   @Test
translateTextExceptionTest()124   public void translateTextExceptionTest() throws Exception {
125     ApiException exception =
126         ApiExceptionFactory.createException(
127             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
128     mockService.addException(exception);
129 
130     try {
131       TranslateTextRequest request =
132           TranslateTextRequest.newBuilder()
133               .addAllContents(new ArrayList<String>())
134               .setMimeType("mimeType-1392120434")
135               .setSourceLanguageCode("sourceLanguageCode1645917472")
136               .setTargetLanguageCode("targetLanguageCode-106414698")
137               .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
138               .setModel("model104069929")
139               .setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder().build())
140               .putAllLabels(new HashMap<String, String>())
141               .build();
142       client.translateText(request);
143       Assert.fail("No exception raised");
144     } catch (InvalidArgumentException e) {
145       // Expected exception.
146     }
147   }
148 
149   @Test
detectLanguageTest()150   public void detectLanguageTest() throws Exception {
151     DetectLanguageResponse expectedResponse =
152         DetectLanguageResponse.newBuilder()
153             .addAllLanguages(new ArrayList<DetectedLanguage>())
154             .build();
155     mockService.addResponse(expectedResponse);
156 
157     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
158     String model = "model104069929";
159     String mimeType = "mimeType-1392120434";
160 
161     DetectLanguageResponse actualResponse = client.detectLanguage(parent, model, mimeType);
162     Assert.assertEquals(expectedResponse, actualResponse);
163 
164     List<String> actualRequests = mockService.getRequestPaths();
165     Assert.assertEquals(1, actualRequests.size());
166 
167     String apiClientHeaderKey =
168         mockService
169             .getRequestHeaders()
170             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
171             .iterator()
172             .next();
173     Assert.assertTrue(
174         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
175             .matcher(apiClientHeaderKey)
176             .matches());
177   }
178 
179   @Test
detectLanguageExceptionTest()180   public void detectLanguageExceptionTest() throws Exception {
181     ApiException exception =
182         ApiExceptionFactory.createException(
183             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
184     mockService.addException(exception);
185 
186     try {
187       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
188       String model = "model104069929";
189       String mimeType = "mimeType-1392120434";
190       client.detectLanguage(parent, model, mimeType);
191       Assert.fail("No exception raised");
192     } catch (InvalidArgumentException e) {
193       // Expected exception.
194     }
195   }
196 
197   @Test
detectLanguageTest2()198   public void detectLanguageTest2() throws Exception {
199     DetectLanguageResponse expectedResponse =
200         DetectLanguageResponse.newBuilder()
201             .addAllLanguages(new ArrayList<DetectedLanguage>())
202             .build();
203     mockService.addResponse(expectedResponse);
204 
205     String parent = "projects/project-5833/locations/location-5833";
206     String model = "model104069929";
207     String mimeType = "mimeType-1392120434";
208 
209     DetectLanguageResponse actualResponse = client.detectLanguage(parent, model, mimeType);
210     Assert.assertEquals(expectedResponse, actualResponse);
211 
212     List<String> actualRequests = mockService.getRequestPaths();
213     Assert.assertEquals(1, actualRequests.size());
214 
215     String apiClientHeaderKey =
216         mockService
217             .getRequestHeaders()
218             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
219             .iterator()
220             .next();
221     Assert.assertTrue(
222         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
223             .matcher(apiClientHeaderKey)
224             .matches());
225   }
226 
227   @Test
detectLanguageExceptionTest2()228   public void detectLanguageExceptionTest2() throws Exception {
229     ApiException exception =
230         ApiExceptionFactory.createException(
231             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
232     mockService.addException(exception);
233 
234     try {
235       String parent = "projects/project-5833/locations/location-5833";
236       String model = "model104069929";
237       String mimeType = "mimeType-1392120434";
238       client.detectLanguage(parent, model, mimeType);
239       Assert.fail("No exception raised");
240     } catch (InvalidArgumentException e) {
241       // Expected exception.
242     }
243   }
244 
245   @Test
getSupportedLanguagesTest()246   public void getSupportedLanguagesTest() throws Exception {
247     SupportedLanguages expectedResponse =
248         SupportedLanguages.newBuilder().addAllLanguages(new ArrayList<SupportedLanguage>()).build();
249     mockService.addResponse(expectedResponse);
250 
251     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
252     String displayLanguageCode = "displayLanguageCode-1457478841";
253     String model = "model104069929";
254 
255     SupportedLanguages actualResponse =
256         client.getSupportedLanguages(parent, displayLanguageCode, model);
257     Assert.assertEquals(expectedResponse, actualResponse);
258 
259     List<String> actualRequests = mockService.getRequestPaths();
260     Assert.assertEquals(1, actualRequests.size());
261 
262     String apiClientHeaderKey =
263         mockService
264             .getRequestHeaders()
265             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
266             .iterator()
267             .next();
268     Assert.assertTrue(
269         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
270             .matcher(apiClientHeaderKey)
271             .matches());
272   }
273 
274   @Test
getSupportedLanguagesExceptionTest()275   public void getSupportedLanguagesExceptionTest() throws Exception {
276     ApiException exception =
277         ApiExceptionFactory.createException(
278             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
279     mockService.addException(exception);
280 
281     try {
282       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
283       String displayLanguageCode = "displayLanguageCode-1457478841";
284       String model = "model104069929";
285       client.getSupportedLanguages(parent, displayLanguageCode, model);
286       Assert.fail("No exception raised");
287     } catch (InvalidArgumentException e) {
288       // Expected exception.
289     }
290   }
291 
292   @Test
getSupportedLanguagesTest2()293   public void getSupportedLanguagesTest2() throws Exception {
294     SupportedLanguages expectedResponse =
295         SupportedLanguages.newBuilder().addAllLanguages(new ArrayList<SupportedLanguage>()).build();
296     mockService.addResponse(expectedResponse);
297 
298     String parent = "projects/project-5833/locations/location-5833";
299     String displayLanguageCode = "displayLanguageCode-1457478841";
300     String model = "model104069929";
301 
302     SupportedLanguages actualResponse =
303         client.getSupportedLanguages(parent, displayLanguageCode, model);
304     Assert.assertEquals(expectedResponse, actualResponse);
305 
306     List<String> actualRequests = mockService.getRequestPaths();
307     Assert.assertEquals(1, actualRequests.size());
308 
309     String apiClientHeaderKey =
310         mockService
311             .getRequestHeaders()
312             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
313             .iterator()
314             .next();
315     Assert.assertTrue(
316         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
317             .matcher(apiClientHeaderKey)
318             .matches());
319   }
320 
321   @Test
getSupportedLanguagesExceptionTest2()322   public void getSupportedLanguagesExceptionTest2() throws Exception {
323     ApiException exception =
324         ApiExceptionFactory.createException(
325             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
326     mockService.addException(exception);
327 
328     try {
329       String parent = "projects/project-5833/locations/location-5833";
330       String displayLanguageCode = "displayLanguageCode-1457478841";
331       String model = "model104069929";
332       client.getSupportedLanguages(parent, displayLanguageCode, model);
333       Assert.fail("No exception raised");
334     } catch (InvalidArgumentException e) {
335       // Expected exception.
336     }
337   }
338 
339   @Test
translateDocumentTest()340   public void translateDocumentTest() throws Exception {
341     TranslateDocumentResponse expectedResponse =
342         TranslateDocumentResponse.newBuilder()
343             .setDocumentTranslation(DocumentTranslation.newBuilder().build())
344             .setGlossaryDocumentTranslation(DocumentTranslation.newBuilder().build())
345             .setModel("model104069929")
346             .setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder().build())
347             .build();
348     mockService.addResponse(expectedResponse);
349 
350     TranslateDocumentRequest request =
351         TranslateDocumentRequest.newBuilder()
352             .setParent("projects/project-5833/locations/location-5833")
353             .setSourceLanguageCode("sourceLanguageCode1645917472")
354             .setTargetLanguageCode("targetLanguageCode-106414698")
355             .setDocumentInputConfig(DocumentInputConfig.newBuilder().build())
356             .setDocumentOutputConfig(DocumentOutputConfig.newBuilder().build())
357             .setModel("model104069929")
358             .setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder().build())
359             .putAllLabels(new HashMap<String, String>())
360             .build();
361 
362     TranslateDocumentResponse actualResponse = client.translateDocument(request);
363     Assert.assertEquals(expectedResponse, actualResponse);
364 
365     List<String> actualRequests = mockService.getRequestPaths();
366     Assert.assertEquals(1, actualRequests.size());
367 
368     String apiClientHeaderKey =
369         mockService
370             .getRequestHeaders()
371             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
372             .iterator()
373             .next();
374     Assert.assertTrue(
375         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
376             .matcher(apiClientHeaderKey)
377             .matches());
378   }
379 
380   @Test
translateDocumentExceptionTest()381   public void translateDocumentExceptionTest() throws Exception {
382     ApiException exception =
383         ApiExceptionFactory.createException(
384             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
385     mockService.addException(exception);
386 
387     try {
388       TranslateDocumentRequest request =
389           TranslateDocumentRequest.newBuilder()
390               .setParent("projects/project-5833/locations/location-5833")
391               .setSourceLanguageCode("sourceLanguageCode1645917472")
392               .setTargetLanguageCode("targetLanguageCode-106414698")
393               .setDocumentInputConfig(DocumentInputConfig.newBuilder().build())
394               .setDocumentOutputConfig(DocumentOutputConfig.newBuilder().build())
395               .setModel("model104069929")
396               .setGlossaryConfig(TranslateTextGlossaryConfig.newBuilder().build())
397               .putAllLabels(new HashMap<String, String>())
398               .build();
399       client.translateDocument(request);
400       Assert.fail("No exception raised");
401     } catch (InvalidArgumentException e) {
402       // Expected exception.
403     }
404   }
405 
406   @Test
batchTranslateTextTest()407   public void batchTranslateTextTest() throws Exception {
408     BatchTranslateResponse expectedResponse =
409         BatchTranslateResponse.newBuilder()
410             .setTotalCharacters(-1368640955)
411             .setTranslatedCharacters(-1337326221)
412             .setFailedCharacters(1723028396)
413             .setSubmitTime(Timestamp.newBuilder().build())
414             .setEndTime(Timestamp.newBuilder().build())
415             .build();
416     Operation resultOperation =
417         Operation.newBuilder()
418             .setName("batchTranslateTextTest")
419             .setDone(true)
420             .setResponse(Any.pack(expectedResponse))
421             .build();
422     mockService.addResponse(resultOperation);
423 
424     BatchTranslateTextRequest request =
425         BatchTranslateTextRequest.newBuilder()
426             .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
427             .setSourceLanguageCode("sourceLanguageCode1645917472")
428             .addAllTargetLanguageCodes(new ArrayList<String>())
429             .putAllModels(new HashMap<String, String>())
430             .addAllInputConfigs(new ArrayList<InputConfig>())
431             .setOutputConfig(OutputConfig.newBuilder().build())
432             .putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
433             .putAllLabels(new HashMap<String, String>())
434             .build();
435 
436     BatchTranslateResponse actualResponse = client.batchTranslateTextAsync(request).get();
437     Assert.assertEquals(expectedResponse, actualResponse);
438 
439     List<String> actualRequests = mockService.getRequestPaths();
440     Assert.assertEquals(1, actualRequests.size());
441 
442     String apiClientHeaderKey =
443         mockService
444             .getRequestHeaders()
445             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
446             .iterator()
447             .next();
448     Assert.assertTrue(
449         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
450             .matcher(apiClientHeaderKey)
451             .matches());
452   }
453 
454   @Test
batchTranslateTextExceptionTest()455   public void batchTranslateTextExceptionTest() throws Exception {
456     ApiException exception =
457         ApiExceptionFactory.createException(
458             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
459     mockService.addException(exception);
460 
461     try {
462       BatchTranslateTextRequest request =
463           BatchTranslateTextRequest.newBuilder()
464               .setParent(LocationName.of("[PROJECT]", "[LOCATION]").toString())
465               .setSourceLanguageCode("sourceLanguageCode1645917472")
466               .addAllTargetLanguageCodes(new ArrayList<String>())
467               .putAllModels(new HashMap<String, String>())
468               .addAllInputConfigs(new ArrayList<InputConfig>())
469               .setOutputConfig(OutputConfig.newBuilder().build())
470               .putAllGlossaries(new HashMap<String, TranslateTextGlossaryConfig>())
471               .putAllLabels(new HashMap<String, String>())
472               .build();
473       client.batchTranslateTextAsync(request).get();
474       Assert.fail("No exception raised");
475     } catch (ExecutionException e) {
476     }
477   }
478 
479   @Test
batchTranslateDocumentTest()480   public void batchTranslateDocumentTest() throws Exception {
481     BatchTranslateDocumentResponse expectedResponse =
482         BatchTranslateDocumentResponse.newBuilder()
483             .setTotalPages(-396186871)
484             .setTranslatedPages(-1652747493)
485             .setFailedPages(-2002254526)
486             .setTotalBillablePages(1292117569)
487             .setTotalCharacters(-1368640955)
488             .setTranslatedCharacters(-1337326221)
489             .setFailedCharacters(1723028396)
490             .setTotalBillableCharacters(1242495501)
491             .setSubmitTime(Timestamp.newBuilder().build())
492             .setEndTime(Timestamp.newBuilder().build())
493             .build();
494     Operation resultOperation =
495         Operation.newBuilder()
496             .setName("batchTranslateDocumentTest")
497             .setDone(true)
498             .setResponse(Any.pack(expectedResponse))
499             .build();
500     mockService.addResponse(resultOperation);
501 
502     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
503     String sourceLanguageCode = "sourceLanguageCode1645917472";
504     List<String> targetLanguageCodes = new ArrayList<>();
505     List<BatchDocumentInputConfig> inputConfigs = new ArrayList<>();
506     BatchDocumentOutputConfig outputConfig = BatchDocumentOutputConfig.newBuilder().build();
507 
508     BatchTranslateDocumentResponse actualResponse =
509         client
510             .batchTranslateDocumentAsync(
511                 parent, sourceLanguageCode, targetLanguageCodes, inputConfigs, outputConfig)
512             .get();
513     Assert.assertEquals(expectedResponse, actualResponse);
514 
515     List<String> actualRequests = mockService.getRequestPaths();
516     Assert.assertEquals(1, actualRequests.size());
517 
518     String apiClientHeaderKey =
519         mockService
520             .getRequestHeaders()
521             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
522             .iterator()
523             .next();
524     Assert.assertTrue(
525         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
526             .matcher(apiClientHeaderKey)
527             .matches());
528   }
529 
530   @Test
batchTranslateDocumentExceptionTest()531   public void batchTranslateDocumentExceptionTest() throws Exception {
532     ApiException exception =
533         ApiExceptionFactory.createException(
534             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
535     mockService.addException(exception);
536 
537     try {
538       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
539       String sourceLanguageCode = "sourceLanguageCode1645917472";
540       List<String> targetLanguageCodes = new ArrayList<>();
541       List<BatchDocumentInputConfig> inputConfigs = new ArrayList<>();
542       BatchDocumentOutputConfig outputConfig = BatchDocumentOutputConfig.newBuilder().build();
543       client
544           .batchTranslateDocumentAsync(
545               parent, sourceLanguageCode, targetLanguageCodes, inputConfigs, outputConfig)
546           .get();
547       Assert.fail("No exception raised");
548     } catch (ExecutionException e) {
549     }
550   }
551 
552   @Test
batchTranslateDocumentTest2()553   public void batchTranslateDocumentTest2() throws Exception {
554     BatchTranslateDocumentResponse expectedResponse =
555         BatchTranslateDocumentResponse.newBuilder()
556             .setTotalPages(-396186871)
557             .setTranslatedPages(-1652747493)
558             .setFailedPages(-2002254526)
559             .setTotalBillablePages(1292117569)
560             .setTotalCharacters(-1368640955)
561             .setTranslatedCharacters(-1337326221)
562             .setFailedCharacters(1723028396)
563             .setTotalBillableCharacters(1242495501)
564             .setSubmitTime(Timestamp.newBuilder().build())
565             .setEndTime(Timestamp.newBuilder().build())
566             .build();
567     Operation resultOperation =
568         Operation.newBuilder()
569             .setName("batchTranslateDocumentTest")
570             .setDone(true)
571             .setResponse(Any.pack(expectedResponse))
572             .build();
573     mockService.addResponse(resultOperation);
574 
575     String parent = "projects/project-5833/locations/location-5833";
576     String sourceLanguageCode = "sourceLanguageCode1645917472";
577     List<String> targetLanguageCodes = new ArrayList<>();
578     List<BatchDocumentInputConfig> inputConfigs = new ArrayList<>();
579     BatchDocumentOutputConfig outputConfig = BatchDocumentOutputConfig.newBuilder().build();
580 
581     BatchTranslateDocumentResponse actualResponse =
582         client
583             .batchTranslateDocumentAsync(
584                 parent, sourceLanguageCode, targetLanguageCodes, inputConfigs, outputConfig)
585             .get();
586     Assert.assertEquals(expectedResponse, actualResponse);
587 
588     List<String> actualRequests = mockService.getRequestPaths();
589     Assert.assertEquals(1, actualRequests.size());
590 
591     String apiClientHeaderKey =
592         mockService
593             .getRequestHeaders()
594             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
595             .iterator()
596             .next();
597     Assert.assertTrue(
598         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
599             .matcher(apiClientHeaderKey)
600             .matches());
601   }
602 
603   @Test
batchTranslateDocumentExceptionTest2()604   public void batchTranslateDocumentExceptionTest2() throws Exception {
605     ApiException exception =
606         ApiExceptionFactory.createException(
607             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
608     mockService.addException(exception);
609 
610     try {
611       String parent = "projects/project-5833/locations/location-5833";
612       String sourceLanguageCode = "sourceLanguageCode1645917472";
613       List<String> targetLanguageCodes = new ArrayList<>();
614       List<BatchDocumentInputConfig> inputConfigs = new ArrayList<>();
615       BatchDocumentOutputConfig outputConfig = BatchDocumentOutputConfig.newBuilder().build();
616       client
617           .batchTranslateDocumentAsync(
618               parent, sourceLanguageCode, targetLanguageCodes, inputConfigs, outputConfig)
619           .get();
620       Assert.fail("No exception raised");
621     } catch (ExecutionException e) {
622     }
623   }
624 
625   @Test
createGlossaryTest()626   public void createGlossaryTest() throws Exception {
627     Glossary expectedResponse =
628         Glossary.newBuilder()
629             .setName(GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]").toString())
630             .setInputConfig(GlossaryInputConfig.newBuilder().build())
631             .setEntryCount(-811131134)
632             .setSubmitTime(Timestamp.newBuilder().build())
633             .setEndTime(Timestamp.newBuilder().build())
634             .build();
635     Operation resultOperation =
636         Operation.newBuilder()
637             .setName("createGlossaryTest")
638             .setDone(true)
639             .setResponse(Any.pack(expectedResponse))
640             .build();
641     mockService.addResponse(resultOperation);
642 
643     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
644     Glossary glossary = Glossary.newBuilder().build();
645 
646     Glossary actualResponse = client.createGlossaryAsync(parent, glossary).get();
647     Assert.assertEquals(expectedResponse, actualResponse);
648 
649     List<String> actualRequests = mockService.getRequestPaths();
650     Assert.assertEquals(1, actualRequests.size());
651 
652     String apiClientHeaderKey =
653         mockService
654             .getRequestHeaders()
655             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
656             .iterator()
657             .next();
658     Assert.assertTrue(
659         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
660             .matcher(apiClientHeaderKey)
661             .matches());
662   }
663 
664   @Test
createGlossaryExceptionTest()665   public void createGlossaryExceptionTest() throws Exception {
666     ApiException exception =
667         ApiExceptionFactory.createException(
668             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
669     mockService.addException(exception);
670 
671     try {
672       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
673       Glossary glossary = Glossary.newBuilder().build();
674       client.createGlossaryAsync(parent, glossary).get();
675       Assert.fail("No exception raised");
676     } catch (ExecutionException e) {
677     }
678   }
679 
680   @Test
createGlossaryTest2()681   public void createGlossaryTest2() throws Exception {
682     Glossary expectedResponse =
683         Glossary.newBuilder()
684             .setName(GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]").toString())
685             .setInputConfig(GlossaryInputConfig.newBuilder().build())
686             .setEntryCount(-811131134)
687             .setSubmitTime(Timestamp.newBuilder().build())
688             .setEndTime(Timestamp.newBuilder().build())
689             .build();
690     Operation resultOperation =
691         Operation.newBuilder()
692             .setName("createGlossaryTest")
693             .setDone(true)
694             .setResponse(Any.pack(expectedResponse))
695             .build();
696     mockService.addResponse(resultOperation);
697 
698     String parent = "projects/project-5833/locations/location-5833";
699     Glossary glossary = Glossary.newBuilder().build();
700 
701     Glossary actualResponse = client.createGlossaryAsync(parent, glossary).get();
702     Assert.assertEquals(expectedResponse, actualResponse);
703 
704     List<String> actualRequests = mockService.getRequestPaths();
705     Assert.assertEquals(1, actualRequests.size());
706 
707     String apiClientHeaderKey =
708         mockService
709             .getRequestHeaders()
710             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
711             .iterator()
712             .next();
713     Assert.assertTrue(
714         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
715             .matcher(apiClientHeaderKey)
716             .matches());
717   }
718 
719   @Test
createGlossaryExceptionTest2()720   public void createGlossaryExceptionTest2() throws Exception {
721     ApiException exception =
722         ApiExceptionFactory.createException(
723             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
724     mockService.addException(exception);
725 
726     try {
727       String parent = "projects/project-5833/locations/location-5833";
728       Glossary glossary = Glossary.newBuilder().build();
729       client.createGlossaryAsync(parent, glossary).get();
730       Assert.fail("No exception raised");
731     } catch (ExecutionException e) {
732     }
733   }
734 
735   @Test
listGlossariesTest()736   public void listGlossariesTest() throws Exception {
737     Glossary responsesElement = Glossary.newBuilder().build();
738     ListGlossariesResponse expectedResponse =
739         ListGlossariesResponse.newBuilder()
740             .setNextPageToken("")
741             .addAllGlossaries(Arrays.asList(responsesElement))
742             .build();
743     mockService.addResponse(expectedResponse);
744 
745     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
746     String filter = "filter-1274492040";
747 
748     ListGlossariesPagedResponse pagedListResponse = client.listGlossaries(parent, filter);
749 
750     List<Glossary> resources = Lists.newArrayList(pagedListResponse.iterateAll());
751 
752     Assert.assertEquals(1, resources.size());
753     Assert.assertEquals(expectedResponse.getGlossariesList().get(0), resources.get(0));
754 
755     List<String> actualRequests = mockService.getRequestPaths();
756     Assert.assertEquals(1, actualRequests.size());
757 
758     String apiClientHeaderKey =
759         mockService
760             .getRequestHeaders()
761             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
762             .iterator()
763             .next();
764     Assert.assertTrue(
765         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
766             .matcher(apiClientHeaderKey)
767             .matches());
768   }
769 
770   @Test
listGlossariesExceptionTest()771   public void listGlossariesExceptionTest() throws Exception {
772     ApiException exception =
773         ApiExceptionFactory.createException(
774             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
775     mockService.addException(exception);
776 
777     try {
778       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
779       String filter = "filter-1274492040";
780       client.listGlossaries(parent, filter);
781       Assert.fail("No exception raised");
782     } catch (InvalidArgumentException e) {
783       // Expected exception.
784     }
785   }
786 
787   @Test
listGlossariesTest2()788   public void listGlossariesTest2() throws Exception {
789     Glossary responsesElement = Glossary.newBuilder().build();
790     ListGlossariesResponse expectedResponse =
791         ListGlossariesResponse.newBuilder()
792             .setNextPageToken("")
793             .addAllGlossaries(Arrays.asList(responsesElement))
794             .build();
795     mockService.addResponse(expectedResponse);
796 
797     String parent = "projects/project-5833/locations/location-5833";
798     String filter = "filter-1274492040";
799 
800     ListGlossariesPagedResponse pagedListResponse = client.listGlossaries(parent, filter);
801 
802     List<Glossary> resources = Lists.newArrayList(pagedListResponse.iterateAll());
803 
804     Assert.assertEquals(1, resources.size());
805     Assert.assertEquals(expectedResponse.getGlossariesList().get(0), resources.get(0));
806 
807     List<String> actualRequests = mockService.getRequestPaths();
808     Assert.assertEquals(1, actualRequests.size());
809 
810     String apiClientHeaderKey =
811         mockService
812             .getRequestHeaders()
813             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
814             .iterator()
815             .next();
816     Assert.assertTrue(
817         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
818             .matcher(apiClientHeaderKey)
819             .matches());
820   }
821 
822   @Test
listGlossariesExceptionTest2()823   public void listGlossariesExceptionTest2() throws Exception {
824     ApiException exception =
825         ApiExceptionFactory.createException(
826             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
827     mockService.addException(exception);
828 
829     try {
830       String parent = "projects/project-5833/locations/location-5833";
831       String filter = "filter-1274492040";
832       client.listGlossaries(parent, filter);
833       Assert.fail("No exception raised");
834     } catch (InvalidArgumentException e) {
835       // Expected exception.
836     }
837   }
838 
839   @Test
getGlossaryTest()840   public void getGlossaryTest() throws Exception {
841     Glossary expectedResponse =
842         Glossary.newBuilder()
843             .setName(GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]").toString())
844             .setInputConfig(GlossaryInputConfig.newBuilder().build())
845             .setEntryCount(-811131134)
846             .setSubmitTime(Timestamp.newBuilder().build())
847             .setEndTime(Timestamp.newBuilder().build())
848             .build();
849     mockService.addResponse(expectedResponse);
850 
851     GlossaryName name = GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]");
852 
853     Glossary actualResponse = client.getGlossary(name);
854     Assert.assertEquals(expectedResponse, actualResponse);
855 
856     List<String> actualRequests = mockService.getRequestPaths();
857     Assert.assertEquals(1, actualRequests.size());
858 
859     String apiClientHeaderKey =
860         mockService
861             .getRequestHeaders()
862             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
863             .iterator()
864             .next();
865     Assert.assertTrue(
866         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
867             .matcher(apiClientHeaderKey)
868             .matches());
869   }
870 
871   @Test
getGlossaryExceptionTest()872   public void getGlossaryExceptionTest() throws Exception {
873     ApiException exception =
874         ApiExceptionFactory.createException(
875             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
876     mockService.addException(exception);
877 
878     try {
879       GlossaryName name = GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]");
880       client.getGlossary(name);
881       Assert.fail("No exception raised");
882     } catch (InvalidArgumentException e) {
883       // Expected exception.
884     }
885   }
886 
887   @Test
getGlossaryTest2()888   public void getGlossaryTest2() throws Exception {
889     Glossary expectedResponse =
890         Glossary.newBuilder()
891             .setName(GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]").toString())
892             .setInputConfig(GlossaryInputConfig.newBuilder().build())
893             .setEntryCount(-811131134)
894             .setSubmitTime(Timestamp.newBuilder().build())
895             .setEndTime(Timestamp.newBuilder().build())
896             .build();
897     mockService.addResponse(expectedResponse);
898 
899     String name = "projects/project-9868/locations/location-9868/glossaries/glossarie-9868";
900 
901     Glossary actualResponse = client.getGlossary(name);
902     Assert.assertEquals(expectedResponse, actualResponse);
903 
904     List<String> actualRequests = mockService.getRequestPaths();
905     Assert.assertEquals(1, actualRequests.size());
906 
907     String apiClientHeaderKey =
908         mockService
909             .getRequestHeaders()
910             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
911             .iterator()
912             .next();
913     Assert.assertTrue(
914         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
915             .matcher(apiClientHeaderKey)
916             .matches());
917   }
918 
919   @Test
getGlossaryExceptionTest2()920   public void getGlossaryExceptionTest2() throws Exception {
921     ApiException exception =
922         ApiExceptionFactory.createException(
923             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
924     mockService.addException(exception);
925 
926     try {
927       String name = "projects/project-9868/locations/location-9868/glossaries/glossarie-9868";
928       client.getGlossary(name);
929       Assert.fail("No exception raised");
930     } catch (InvalidArgumentException e) {
931       // Expected exception.
932     }
933   }
934 
935   @Test
deleteGlossaryTest()936   public void deleteGlossaryTest() throws Exception {
937     DeleteGlossaryResponse expectedResponse =
938         DeleteGlossaryResponse.newBuilder()
939             .setName("name3373707")
940             .setSubmitTime(Timestamp.newBuilder().build())
941             .setEndTime(Timestamp.newBuilder().build())
942             .build();
943     Operation resultOperation =
944         Operation.newBuilder()
945             .setName("deleteGlossaryTest")
946             .setDone(true)
947             .setResponse(Any.pack(expectedResponse))
948             .build();
949     mockService.addResponse(resultOperation);
950 
951     GlossaryName name = GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]");
952 
953     DeleteGlossaryResponse actualResponse = client.deleteGlossaryAsync(name).get();
954     Assert.assertEquals(expectedResponse, actualResponse);
955 
956     List<String> actualRequests = mockService.getRequestPaths();
957     Assert.assertEquals(1, actualRequests.size());
958 
959     String apiClientHeaderKey =
960         mockService
961             .getRequestHeaders()
962             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
963             .iterator()
964             .next();
965     Assert.assertTrue(
966         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
967             .matcher(apiClientHeaderKey)
968             .matches());
969   }
970 
971   @Test
deleteGlossaryExceptionTest()972   public void deleteGlossaryExceptionTest() throws Exception {
973     ApiException exception =
974         ApiExceptionFactory.createException(
975             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
976     mockService.addException(exception);
977 
978     try {
979       GlossaryName name = GlossaryName.of("[PROJECT]", "[LOCATION]", "[GLOSSARY]");
980       client.deleteGlossaryAsync(name).get();
981       Assert.fail("No exception raised");
982     } catch (ExecutionException e) {
983     }
984   }
985 
986   @Test
deleteGlossaryTest2()987   public void deleteGlossaryTest2() throws Exception {
988     DeleteGlossaryResponse expectedResponse =
989         DeleteGlossaryResponse.newBuilder()
990             .setName("name3373707")
991             .setSubmitTime(Timestamp.newBuilder().build())
992             .setEndTime(Timestamp.newBuilder().build())
993             .build();
994     Operation resultOperation =
995         Operation.newBuilder()
996             .setName("deleteGlossaryTest")
997             .setDone(true)
998             .setResponse(Any.pack(expectedResponse))
999             .build();
1000     mockService.addResponse(resultOperation);
1001 
1002     String name = "projects/project-9868/locations/location-9868/glossaries/glossarie-9868";
1003 
1004     DeleteGlossaryResponse actualResponse = client.deleteGlossaryAsync(name).get();
1005     Assert.assertEquals(expectedResponse, actualResponse);
1006 
1007     List<String> actualRequests = mockService.getRequestPaths();
1008     Assert.assertEquals(1, actualRequests.size());
1009 
1010     String apiClientHeaderKey =
1011         mockService
1012             .getRequestHeaders()
1013             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
1014             .iterator()
1015             .next();
1016     Assert.assertTrue(
1017         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
1018             .matcher(apiClientHeaderKey)
1019             .matches());
1020   }
1021 
1022   @Test
deleteGlossaryExceptionTest2()1023   public void deleteGlossaryExceptionTest2() throws Exception {
1024     ApiException exception =
1025         ApiExceptionFactory.createException(
1026             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
1027     mockService.addException(exception);
1028 
1029     try {
1030       String name = "projects/project-9868/locations/location-9868/glossaries/glossarie-9868";
1031       client.deleteGlossaryAsync(name).get();
1032       Assert.fail("No exception raised");
1033     } catch (ExecutionException e) {
1034     }
1035   }
1036 }
1037