• 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.batch.v1;
18 
19 import static com.google.cloud.batch.v1.BatchServiceClient.ListJobsPagedResponse;
20 import static com.google.cloud.batch.v1.BatchServiceClient.ListLocationsPagedResponse;
21 import static com.google.cloud.batch.v1.BatchServiceClient.ListTasksPagedResponse;
22 
23 import com.google.api.gax.core.NoCredentialsProvider;
24 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
25 import com.google.api.gax.httpjson.testing.MockHttpService;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.ApiException;
28 import com.google.api.gax.rpc.ApiExceptionFactory;
29 import com.google.api.gax.rpc.InvalidArgumentException;
30 import com.google.api.gax.rpc.StatusCode;
31 import com.google.api.gax.rpc.testing.FakeStatusCode;
32 import com.google.cloud.batch.v1.stub.HttpJsonBatchServiceStub;
33 import com.google.cloud.location.GetLocationRequest;
34 import com.google.cloud.location.ListLocationsRequest;
35 import com.google.cloud.location.ListLocationsResponse;
36 import com.google.cloud.location.Location;
37 import com.google.common.collect.Lists;
38 import com.google.longrunning.Operation;
39 import com.google.protobuf.Any;
40 import com.google.protobuf.Empty;
41 import com.google.protobuf.Timestamp;
42 import java.io.IOException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.concurrent.ExecutionException;
48 import javax.annotation.Generated;
49 import org.junit.After;
50 import org.junit.AfterClass;
51 import org.junit.Assert;
52 import org.junit.Before;
53 import org.junit.BeforeClass;
54 import org.junit.Test;
55 
56 @Generated("by gapic-generator-java")
57 public class BatchServiceClientHttpJsonTest {
58   private static MockHttpService mockService;
59   private static BatchServiceClient client;
60 
61   @BeforeClass
startStaticServer()62   public static void startStaticServer() throws IOException {
63     mockService =
64         new MockHttpService(
65             HttpJsonBatchServiceStub.getMethodDescriptors(),
66             BatchServiceSettings.getDefaultEndpoint());
67     BatchServiceSettings settings =
68         BatchServiceSettings.newHttpJsonBuilder()
69             .setTransportChannelProvider(
70                 BatchServiceSettings.defaultHttpJsonTransportProviderBuilder()
71                     .setHttpTransport(mockService)
72                     .build())
73             .setCredentialsProvider(NoCredentialsProvider.create())
74             .build();
75     client = BatchServiceClient.create(settings);
76   }
77 
78   @AfterClass
stopServer()79   public static void stopServer() {
80     client.close();
81   }
82 
83   @Before
setUp()84   public void setUp() {}
85 
86   @After
tearDown()87   public void tearDown() throws Exception {
88     mockService.reset();
89   }
90 
91   @Test
createJobTest()92   public void createJobTest() throws Exception {
93     Job expectedResponse =
94         Job.newBuilder()
95             .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString())
96             .setUid("uid115792")
97             .setPriority(-1165461084)
98             .addAllTaskGroups(new ArrayList<TaskGroup>())
99             .setAllocationPolicy(AllocationPolicy.newBuilder().build())
100             .putAllLabels(new HashMap<String, String>())
101             .setStatus(JobStatus.newBuilder().build())
102             .setCreateTime(Timestamp.newBuilder().build())
103             .setUpdateTime(Timestamp.newBuilder().build())
104             .setLogsPolicy(LogsPolicy.newBuilder().build())
105             .addAllNotifications(new ArrayList<JobNotification>())
106             .build();
107     mockService.addResponse(expectedResponse);
108 
109     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
110     Job job = Job.newBuilder().build();
111     String jobId = "jobId101296568";
112 
113     Job actualResponse = client.createJob(parent, job, jobId);
114     Assert.assertEquals(expectedResponse, actualResponse);
115 
116     List<String> actualRequests = mockService.getRequestPaths();
117     Assert.assertEquals(1, actualRequests.size());
118 
119     String apiClientHeaderKey =
120         mockService
121             .getRequestHeaders()
122             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
123             .iterator()
124             .next();
125     Assert.assertTrue(
126         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
127             .matcher(apiClientHeaderKey)
128             .matches());
129   }
130 
131   @Test
createJobExceptionTest()132   public void createJobExceptionTest() throws Exception {
133     ApiException exception =
134         ApiExceptionFactory.createException(
135             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
136     mockService.addException(exception);
137 
138     try {
139       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
140       Job job = Job.newBuilder().build();
141       String jobId = "jobId101296568";
142       client.createJob(parent, job, jobId);
143       Assert.fail("No exception raised");
144     } catch (InvalidArgumentException e) {
145       // Expected exception.
146     }
147   }
148 
149   @Test
createJobTest2()150   public void createJobTest2() throws Exception {
151     Job expectedResponse =
152         Job.newBuilder()
153             .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString())
154             .setUid("uid115792")
155             .setPriority(-1165461084)
156             .addAllTaskGroups(new ArrayList<TaskGroup>())
157             .setAllocationPolicy(AllocationPolicy.newBuilder().build())
158             .putAllLabels(new HashMap<String, String>())
159             .setStatus(JobStatus.newBuilder().build())
160             .setCreateTime(Timestamp.newBuilder().build())
161             .setUpdateTime(Timestamp.newBuilder().build())
162             .setLogsPolicy(LogsPolicy.newBuilder().build())
163             .addAllNotifications(new ArrayList<JobNotification>())
164             .build();
165     mockService.addResponse(expectedResponse);
166 
167     String parent = "projects/project-5833/locations/location-5833";
168     Job job = Job.newBuilder().build();
169     String jobId = "jobId101296568";
170 
171     Job actualResponse = client.createJob(parent, job, jobId);
172     Assert.assertEquals(expectedResponse, actualResponse);
173 
174     List<String> actualRequests = mockService.getRequestPaths();
175     Assert.assertEquals(1, actualRequests.size());
176 
177     String apiClientHeaderKey =
178         mockService
179             .getRequestHeaders()
180             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
181             .iterator()
182             .next();
183     Assert.assertTrue(
184         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
185             .matcher(apiClientHeaderKey)
186             .matches());
187   }
188 
189   @Test
createJobExceptionTest2()190   public void createJobExceptionTest2() throws Exception {
191     ApiException exception =
192         ApiExceptionFactory.createException(
193             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
194     mockService.addException(exception);
195 
196     try {
197       String parent = "projects/project-5833/locations/location-5833";
198       Job job = Job.newBuilder().build();
199       String jobId = "jobId101296568";
200       client.createJob(parent, job, jobId);
201       Assert.fail("No exception raised");
202     } catch (InvalidArgumentException e) {
203       // Expected exception.
204     }
205   }
206 
207   @Test
getJobTest()208   public void getJobTest() throws Exception {
209     Job expectedResponse =
210         Job.newBuilder()
211             .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString())
212             .setUid("uid115792")
213             .setPriority(-1165461084)
214             .addAllTaskGroups(new ArrayList<TaskGroup>())
215             .setAllocationPolicy(AllocationPolicy.newBuilder().build())
216             .putAllLabels(new HashMap<String, String>())
217             .setStatus(JobStatus.newBuilder().build())
218             .setCreateTime(Timestamp.newBuilder().build())
219             .setUpdateTime(Timestamp.newBuilder().build())
220             .setLogsPolicy(LogsPolicy.newBuilder().build())
221             .addAllNotifications(new ArrayList<JobNotification>())
222             .build();
223     mockService.addResponse(expectedResponse);
224 
225     JobName name = JobName.of("[PROJECT]", "[LOCATION]", "[JOB]");
226 
227     Job actualResponse = client.getJob(name);
228     Assert.assertEquals(expectedResponse, actualResponse);
229 
230     List<String> actualRequests = mockService.getRequestPaths();
231     Assert.assertEquals(1, actualRequests.size());
232 
233     String apiClientHeaderKey =
234         mockService
235             .getRequestHeaders()
236             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
237             .iterator()
238             .next();
239     Assert.assertTrue(
240         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
241             .matcher(apiClientHeaderKey)
242             .matches());
243   }
244 
245   @Test
getJobExceptionTest()246   public void getJobExceptionTest() throws Exception {
247     ApiException exception =
248         ApiExceptionFactory.createException(
249             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
250     mockService.addException(exception);
251 
252     try {
253       JobName name = JobName.of("[PROJECT]", "[LOCATION]", "[JOB]");
254       client.getJob(name);
255       Assert.fail("No exception raised");
256     } catch (InvalidArgumentException e) {
257       // Expected exception.
258     }
259   }
260 
261   @Test
getJobTest2()262   public void getJobTest2() throws Exception {
263     Job expectedResponse =
264         Job.newBuilder()
265             .setName(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString())
266             .setUid("uid115792")
267             .setPriority(-1165461084)
268             .addAllTaskGroups(new ArrayList<TaskGroup>())
269             .setAllocationPolicy(AllocationPolicy.newBuilder().build())
270             .putAllLabels(new HashMap<String, String>())
271             .setStatus(JobStatus.newBuilder().build())
272             .setCreateTime(Timestamp.newBuilder().build())
273             .setUpdateTime(Timestamp.newBuilder().build())
274             .setLogsPolicy(LogsPolicy.newBuilder().build())
275             .addAllNotifications(new ArrayList<JobNotification>())
276             .build();
277     mockService.addResponse(expectedResponse);
278 
279     String name = "projects/project-3306/locations/location-3306/jobs/job-3306";
280 
281     Job actualResponse = client.getJob(name);
282     Assert.assertEquals(expectedResponse, actualResponse);
283 
284     List<String> actualRequests = mockService.getRequestPaths();
285     Assert.assertEquals(1, actualRequests.size());
286 
287     String apiClientHeaderKey =
288         mockService
289             .getRequestHeaders()
290             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
291             .iterator()
292             .next();
293     Assert.assertTrue(
294         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
295             .matcher(apiClientHeaderKey)
296             .matches());
297   }
298 
299   @Test
getJobExceptionTest2()300   public void getJobExceptionTest2() throws Exception {
301     ApiException exception =
302         ApiExceptionFactory.createException(
303             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
304     mockService.addException(exception);
305 
306     try {
307       String name = "projects/project-3306/locations/location-3306/jobs/job-3306";
308       client.getJob(name);
309       Assert.fail("No exception raised");
310     } catch (InvalidArgumentException e) {
311       // Expected exception.
312     }
313   }
314 
315   @Test
deleteJobTest()316   public void deleteJobTest() throws Exception {
317     Empty expectedResponse = Empty.newBuilder().build();
318     Operation resultOperation =
319         Operation.newBuilder()
320             .setName("deleteJobTest")
321             .setDone(true)
322             .setResponse(Any.pack(expectedResponse))
323             .build();
324     mockService.addResponse(resultOperation);
325 
326     String name = "projects/project-3306/locations/location-3306/jobs/job-3306";
327 
328     client.deleteJobAsync(name).get();
329 
330     List<String> actualRequests = mockService.getRequestPaths();
331     Assert.assertEquals(1, actualRequests.size());
332 
333     String apiClientHeaderKey =
334         mockService
335             .getRequestHeaders()
336             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
337             .iterator()
338             .next();
339     Assert.assertTrue(
340         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
341             .matcher(apiClientHeaderKey)
342             .matches());
343   }
344 
345   @Test
deleteJobExceptionTest()346   public void deleteJobExceptionTest() throws Exception {
347     ApiException exception =
348         ApiExceptionFactory.createException(
349             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
350     mockService.addException(exception);
351 
352     try {
353       String name = "projects/project-3306/locations/location-3306/jobs/job-3306";
354       client.deleteJobAsync(name).get();
355       Assert.fail("No exception raised");
356     } catch (ExecutionException e) {
357     }
358   }
359 
360   @Test
listJobsTest()361   public void listJobsTest() throws Exception {
362     Job responsesElement = Job.newBuilder().build();
363     ListJobsResponse expectedResponse =
364         ListJobsResponse.newBuilder()
365             .setNextPageToken("")
366             .addAllJobs(Arrays.asList(responsesElement))
367             .build();
368     mockService.addResponse(expectedResponse);
369 
370     String parent = "projects/project-5833/locations/location-5833";
371 
372     ListJobsPagedResponse pagedListResponse = client.listJobs(parent);
373 
374     List<Job> resources = Lists.newArrayList(pagedListResponse.iterateAll());
375 
376     Assert.assertEquals(1, resources.size());
377     Assert.assertEquals(expectedResponse.getJobsList().get(0), resources.get(0));
378 
379     List<String> actualRequests = mockService.getRequestPaths();
380     Assert.assertEquals(1, actualRequests.size());
381 
382     String apiClientHeaderKey =
383         mockService
384             .getRequestHeaders()
385             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
386             .iterator()
387             .next();
388     Assert.assertTrue(
389         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
390             .matcher(apiClientHeaderKey)
391             .matches());
392   }
393 
394   @Test
listJobsExceptionTest()395   public void listJobsExceptionTest() throws Exception {
396     ApiException exception =
397         ApiExceptionFactory.createException(
398             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
399     mockService.addException(exception);
400 
401     try {
402       String parent = "projects/project-5833/locations/location-5833";
403       client.listJobs(parent);
404       Assert.fail("No exception raised");
405     } catch (InvalidArgumentException e) {
406       // Expected exception.
407     }
408   }
409 
410   @Test
getTaskTest()411   public void getTaskTest() throws Exception {
412     Task expectedResponse =
413         Task.newBuilder()
414             .setName(
415                 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]")
416                     .toString())
417             .setStatus(TaskStatus.newBuilder().build())
418             .build();
419     mockService.addResponse(expectedResponse);
420 
421     TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]");
422 
423     Task actualResponse = client.getTask(name);
424     Assert.assertEquals(expectedResponse, actualResponse);
425 
426     List<String> actualRequests = mockService.getRequestPaths();
427     Assert.assertEquals(1, actualRequests.size());
428 
429     String apiClientHeaderKey =
430         mockService
431             .getRequestHeaders()
432             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
433             .iterator()
434             .next();
435     Assert.assertTrue(
436         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
437             .matcher(apiClientHeaderKey)
438             .matches());
439   }
440 
441   @Test
getTaskExceptionTest()442   public void getTaskExceptionTest() throws Exception {
443     ApiException exception =
444         ApiExceptionFactory.createException(
445             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
446     mockService.addException(exception);
447 
448     try {
449       TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]");
450       client.getTask(name);
451       Assert.fail("No exception raised");
452     } catch (InvalidArgumentException e) {
453       // Expected exception.
454     }
455   }
456 
457   @Test
getTaskTest2()458   public void getTaskTest2() throws Exception {
459     Task expectedResponse =
460         Task.newBuilder()
461             .setName(
462                 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]", "[TASK]")
463                     .toString())
464             .setStatus(TaskStatus.newBuilder().build())
465             .build();
466     mockService.addResponse(expectedResponse);
467 
468     String name =
469         "projects/project-5433/locations/location-5433/jobs/job-5433/taskGroups/taskGroup-5433/tasks/task-5433";
470 
471     Task actualResponse = client.getTask(name);
472     Assert.assertEquals(expectedResponse, actualResponse);
473 
474     List<String> actualRequests = mockService.getRequestPaths();
475     Assert.assertEquals(1, actualRequests.size());
476 
477     String apiClientHeaderKey =
478         mockService
479             .getRequestHeaders()
480             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
481             .iterator()
482             .next();
483     Assert.assertTrue(
484         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
485             .matcher(apiClientHeaderKey)
486             .matches());
487   }
488 
489   @Test
getTaskExceptionTest2()490   public void getTaskExceptionTest2() throws Exception {
491     ApiException exception =
492         ApiExceptionFactory.createException(
493             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
494     mockService.addException(exception);
495 
496     try {
497       String name =
498           "projects/project-5433/locations/location-5433/jobs/job-5433/taskGroups/taskGroup-5433/tasks/task-5433";
499       client.getTask(name);
500       Assert.fail("No exception raised");
501     } catch (InvalidArgumentException e) {
502       // Expected exception.
503     }
504   }
505 
506   @Test
listTasksTest()507   public void listTasksTest() throws Exception {
508     Task responsesElement = Task.newBuilder().build();
509     ListTasksResponse expectedResponse =
510         ListTasksResponse.newBuilder()
511             .setNextPageToken("")
512             .addAllTasks(Arrays.asList(responsesElement))
513             .build();
514     mockService.addResponse(expectedResponse);
515 
516     TaskGroupName parent = TaskGroupName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]");
517 
518     ListTasksPagedResponse pagedListResponse = client.listTasks(parent);
519 
520     List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll());
521 
522     Assert.assertEquals(1, resources.size());
523     Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0));
524 
525     List<String> actualRequests = mockService.getRequestPaths();
526     Assert.assertEquals(1, actualRequests.size());
527 
528     String apiClientHeaderKey =
529         mockService
530             .getRequestHeaders()
531             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
532             .iterator()
533             .next();
534     Assert.assertTrue(
535         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
536             .matcher(apiClientHeaderKey)
537             .matches());
538   }
539 
540   @Test
listTasksExceptionTest()541   public void listTasksExceptionTest() throws Exception {
542     ApiException exception =
543         ApiExceptionFactory.createException(
544             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
545     mockService.addException(exception);
546 
547     try {
548       TaskGroupName parent = TaskGroupName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[TASK_GROUP]");
549       client.listTasks(parent);
550       Assert.fail("No exception raised");
551     } catch (InvalidArgumentException e) {
552       // Expected exception.
553     }
554   }
555 
556   @Test
listTasksTest2()557   public void listTasksTest2() throws Exception {
558     Task responsesElement = Task.newBuilder().build();
559     ListTasksResponse expectedResponse =
560         ListTasksResponse.newBuilder()
561             .setNextPageToken("")
562             .addAllTasks(Arrays.asList(responsesElement))
563             .build();
564     mockService.addResponse(expectedResponse);
565 
566     String parent =
567         "projects/project-6196/locations/location-6196/jobs/job-6196/taskGroups/taskGroup-6196";
568 
569     ListTasksPagedResponse pagedListResponse = client.listTasks(parent);
570 
571     List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll());
572 
573     Assert.assertEquals(1, resources.size());
574     Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0));
575 
576     List<String> actualRequests = mockService.getRequestPaths();
577     Assert.assertEquals(1, actualRequests.size());
578 
579     String apiClientHeaderKey =
580         mockService
581             .getRequestHeaders()
582             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
583             .iterator()
584             .next();
585     Assert.assertTrue(
586         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
587             .matcher(apiClientHeaderKey)
588             .matches());
589   }
590 
591   @Test
listTasksExceptionTest2()592   public void listTasksExceptionTest2() throws Exception {
593     ApiException exception =
594         ApiExceptionFactory.createException(
595             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
596     mockService.addException(exception);
597 
598     try {
599       String parent =
600           "projects/project-6196/locations/location-6196/jobs/job-6196/taskGroups/taskGroup-6196";
601       client.listTasks(parent);
602       Assert.fail("No exception raised");
603     } catch (InvalidArgumentException e) {
604       // Expected exception.
605     }
606   }
607 
608   @Test
listLocationsTest()609   public void listLocationsTest() throws Exception {
610     Location responsesElement = Location.newBuilder().build();
611     ListLocationsResponse expectedResponse =
612         ListLocationsResponse.newBuilder()
613             .setNextPageToken("")
614             .addAllLocations(Arrays.asList(responsesElement))
615             .build();
616     mockService.addResponse(expectedResponse);
617 
618     ListLocationsRequest request =
619         ListLocationsRequest.newBuilder()
620             .setName("projects/project-3664")
621             .setFilter("filter-1274492040")
622             .setPageSize(883849137)
623             .setPageToken("pageToken873572522")
624             .build();
625 
626     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
627 
628     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
629 
630     Assert.assertEquals(1, resources.size());
631     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
632 
633     List<String> actualRequests = mockService.getRequestPaths();
634     Assert.assertEquals(1, actualRequests.size());
635 
636     String apiClientHeaderKey =
637         mockService
638             .getRequestHeaders()
639             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
640             .iterator()
641             .next();
642     Assert.assertTrue(
643         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
644             .matcher(apiClientHeaderKey)
645             .matches());
646   }
647 
648   @Test
listLocationsExceptionTest()649   public void listLocationsExceptionTest() throws Exception {
650     ApiException exception =
651         ApiExceptionFactory.createException(
652             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
653     mockService.addException(exception);
654 
655     try {
656       ListLocationsRequest request =
657           ListLocationsRequest.newBuilder()
658               .setName("projects/project-3664")
659               .setFilter("filter-1274492040")
660               .setPageSize(883849137)
661               .setPageToken("pageToken873572522")
662               .build();
663       client.listLocations(request);
664       Assert.fail("No exception raised");
665     } catch (InvalidArgumentException e) {
666       // Expected exception.
667     }
668   }
669 
670   @Test
getLocationTest()671   public void getLocationTest() throws Exception {
672     Location expectedResponse =
673         Location.newBuilder()
674             .setName("name3373707")
675             .setLocationId("locationId1541836720")
676             .setDisplayName("displayName1714148973")
677             .putAllLabels(new HashMap<String, String>())
678             .setMetadata(Any.newBuilder().build())
679             .build();
680     mockService.addResponse(expectedResponse);
681 
682     GetLocationRequest request =
683         GetLocationRequest.newBuilder()
684             .setName("projects/project-9062/locations/location-9062")
685             .build();
686 
687     Location actualResponse = client.getLocation(request);
688     Assert.assertEquals(expectedResponse, actualResponse);
689 
690     List<String> actualRequests = mockService.getRequestPaths();
691     Assert.assertEquals(1, actualRequests.size());
692 
693     String apiClientHeaderKey =
694         mockService
695             .getRequestHeaders()
696             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
697             .iterator()
698             .next();
699     Assert.assertTrue(
700         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
701             .matcher(apiClientHeaderKey)
702             .matches());
703   }
704 
705   @Test
getLocationExceptionTest()706   public void getLocationExceptionTest() throws Exception {
707     ApiException exception =
708         ApiExceptionFactory.createException(
709             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
710     mockService.addException(exception);
711 
712     try {
713       GetLocationRequest request =
714           GetLocationRequest.newBuilder()
715               .setName("projects/project-9062/locations/location-9062")
716               .build();
717       client.getLocation(request);
718       Assert.fail("No exception raised");
719     } catch (InvalidArgumentException e) {
720       // Expected exception.
721     }
722   }
723 }
724