• 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.aiplatform.v1;
18 
19 import static com.google.cloud.aiplatform.v1.IndexServiceClient.ListIndexesPagedResponse;
20 import static com.google.cloud.aiplatform.v1.IndexServiceClient.ListLocationsPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.grpc.GaxGrpcProperties;
24 import com.google.api.gax.grpc.testing.LocalChannelProvider;
25 import com.google.api.gax.grpc.testing.MockGrpcService;
26 import com.google.api.gax.grpc.testing.MockServiceHelper;
27 import com.google.api.gax.rpc.ApiClientHeaderProvider;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.cloud.location.GetLocationRequest;
31 import com.google.cloud.location.ListLocationsRequest;
32 import com.google.cloud.location.ListLocationsResponse;
33 import com.google.cloud.location.Location;
34 import com.google.common.collect.Lists;
35 import com.google.iam.v1.AuditConfig;
36 import com.google.iam.v1.Binding;
37 import com.google.iam.v1.GetIamPolicyRequest;
38 import com.google.iam.v1.GetPolicyOptions;
39 import com.google.iam.v1.Policy;
40 import com.google.iam.v1.SetIamPolicyRequest;
41 import com.google.iam.v1.TestIamPermissionsRequest;
42 import com.google.iam.v1.TestIamPermissionsResponse;
43 import com.google.longrunning.Operation;
44 import com.google.protobuf.AbstractMessage;
45 import com.google.protobuf.Any;
46 import com.google.protobuf.ByteString;
47 import com.google.protobuf.Empty;
48 import com.google.protobuf.FieldMask;
49 import com.google.protobuf.Timestamp;
50 import com.google.protobuf.Value;
51 import io.grpc.StatusRuntimeException;
52 import java.io.IOException;
53 import java.util.ArrayList;
54 import java.util.Arrays;
55 import java.util.HashMap;
56 import java.util.List;
57 import java.util.UUID;
58 import java.util.concurrent.ExecutionException;
59 import javax.annotation.Generated;
60 import org.junit.After;
61 import org.junit.AfterClass;
62 import org.junit.Assert;
63 import org.junit.Before;
64 import org.junit.BeforeClass;
65 import org.junit.Test;
66 
67 @Generated("by gapic-generator-java")
68 public class IndexServiceClientTest {
69   private static MockIAMPolicy mockIAMPolicy;
70   private static MockIndexService mockIndexService;
71   private static MockLocations mockLocations;
72   private static MockServiceHelper mockServiceHelper;
73   private LocalChannelProvider channelProvider;
74   private IndexServiceClient client;
75 
76   @BeforeClass
startStaticServer()77   public static void startStaticServer() {
78     mockIndexService = new MockIndexService();
79     mockLocations = new MockLocations();
80     mockIAMPolicy = new MockIAMPolicy();
81     mockServiceHelper =
82         new MockServiceHelper(
83             UUID.randomUUID().toString(),
84             Arrays.<MockGrpcService>asList(mockIndexService, mockLocations, mockIAMPolicy));
85     mockServiceHelper.start();
86   }
87 
88   @AfterClass
stopServer()89   public static void stopServer() {
90     mockServiceHelper.stop();
91   }
92 
93   @Before
setUp()94   public void setUp() throws IOException {
95     mockServiceHelper.reset();
96     channelProvider = mockServiceHelper.createChannelProvider();
97     IndexServiceSettings settings =
98         IndexServiceSettings.newBuilder()
99             .setTransportChannelProvider(channelProvider)
100             .setCredentialsProvider(NoCredentialsProvider.create())
101             .build();
102     client = IndexServiceClient.create(settings);
103   }
104 
105   @After
tearDown()106   public void tearDown() throws Exception {
107     client.close();
108   }
109 
110   @Test
createIndexTest()111   public void createIndexTest() throws Exception {
112     Index expectedResponse =
113         Index.newBuilder()
114             .setName(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
115             .setDisplayName("displayName1714148973")
116             .setDescription("description-1724546052")
117             .setMetadataSchemaUri("metadataSchemaUri781971868")
118             .setMetadata(Value.newBuilder().setBoolValue(true).build())
119             .addAllDeployedIndexes(new ArrayList<DeployedIndexRef>())
120             .setEtag("etag3123477")
121             .putAllLabels(new HashMap<String, String>())
122             .setCreateTime(Timestamp.newBuilder().build())
123             .setUpdateTime(Timestamp.newBuilder().build())
124             .setIndexStats(IndexStats.newBuilder().build())
125             .build();
126     Operation resultOperation =
127         Operation.newBuilder()
128             .setName("createIndexTest")
129             .setDone(true)
130             .setResponse(Any.pack(expectedResponse))
131             .build();
132     mockIndexService.addResponse(resultOperation);
133 
134     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
135     Index index = Index.newBuilder().build();
136 
137     Index actualResponse = client.createIndexAsync(parent, index).get();
138     Assert.assertEquals(expectedResponse, actualResponse);
139 
140     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
141     Assert.assertEquals(1, actualRequests.size());
142     CreateIndexRequest actualRequest = ((CreateIndexRequest) actualRequests.get(0));
143 
144     Assert.assertEquals(parent.toString(), actualRequest.getParent());
145     Assert.assertEquals(index, actualRequest.getIndex());
146     Assert.assertTrue(
147         channelProvider.isHeaderSent(
148             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
149             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
150   }
151 
152   @Test
createIndexExceptionTest()153   public void createIndexExceptionTest() throws Exception {
154     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
155     mockIndexService.addException(exception);
156 
157     try {
158       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
159       Index index = Index.newBuilder().build();
160       client.createIndexAsync(parent, index).get();
161       Assert.fail("No exception raised");
162     } catch (ExecutionException e) {
163       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
164       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
165       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
166     }
167   }
168 
169   @Test
createIndexTest2()170   public void createIndexTest2() throws Exception {
171     Index expectedResponse =
172         Index.newBuilder()
173             .setName(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
174             .setDisplayName("displayName1714148973")
175             .setDescription("description-1724546052")
176             .setMetadataSchemaUri("metadataSchemaUri781971868")
177             .setMetadata(Value.newBuilder().setBoolValue(true).build())
178             .addAllDeployedIndexes(new ArrayList<DeployedIndexRef>())
179             .setEtag("etag3123477")
180             .putAllLabels(new HashMap<String, String>())
181             .setCreateTime(Timestamp.newBuilder().build())
182             .setUpdateTime(Timestamp.newBuilder().build())
183             .setIndexStats(IndexStats.newBuilder().build())
184             .build();
185     Operation resultOperation =
186         Operation.newBuilder()
187             .setName("createIndexTest")
188             .setDone(true)
189             .setResponse(Any.pack(expectedResponse))
190             .build();
191     mockIndexService.addResponse(resultOperation);
192 
193     String parent = "parent-995424086";
194     Index index = Index.newBuilder().build();
195 
196     Index actualResponse = client.createIndexAsync(parent, index).get();
197     Assert.assertEquals(expectedResponse, actualResponse);
198 
199     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
200     Assert.assertEquals(1, actualRequests.size());
201     CreateIndexRequest actualRequest = ((CreateIndexRequest) actualRequests.get(0));
202 
203     Assert.assertEquals(parent, actualRequest.getParent());
204     Assert.assertEquals(index, actualRequest.getIndex());
205     Assert.assertTrue(
206         channelProvider.isHeaderSent(
207             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
208             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
209   }
210 
211   @Test
createIndexExceptionTest2()212   public void createIndexExceptionTest2() throws Exception {
213     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
214     mockIndexService.addException(exception);
215 
216     try {
217       String parent = "parent-995424086";
218       Index index = Index.newBuilder().build();
219       client.createIndexAsync(parent, index).get();
220       Assert.fail("No exception raised");
221     } catch (ExecutionException e) {
222       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
223       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
224       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
225     }
226   }
227 
228   @Test
getIndexTest()229   public void getIndexTest() throws Exception {
230     Index expectedResponse =
231         Index.newBuilder()
232             .setName(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
233             .setDisplayName("displayName1714148973")
234             .setDescription("description-1724546052")
235             .setMetadataSchemaUri("metadataSchemaUri781971868")
236             .setMetadata(Value.newBuilder().setBoolValue(true).build())
237             .addAllDeployedIndexes(new ArrayList<DeployedIndexRef>())
238             .setEtag("etag3123477")
239             .putAllLabels(new HashMap<String, String>())
240             .setCreateTime(Timestamp.newBuilder().build())
241             .setUpdateTime(Timestamp.newBuilder().build())
242             .setIndexStats(IndexStats.newBuilder().build())
243             .build();
244     mockIndexService.addResponse(expectedResponse);
245 
246     IndexName name = IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]");
247 
248     Index actualResponse = client.getIndex(name);
249     Assert.assertEquals(expectedResponse, actualResponse);
250 
251     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
252     Assert.assertEquals(1, actualRequests.size());
253     GetIndexRequest actualRequest = ((GetIndexRequest) actualRequests.get(0));
254 
255     Assert.assertEquals(name.toString(), actualRequest.getName());
256     Assert.assertTrue(
257         channelProvider.isHeaderSent(
258             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
259             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
260   }
261 
262   @Test
getIndexExceptionTest()263   public void getIndexExceptionTest() throws Exception {
264     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
265     mockIndexService.addException(exception);
266 
267     try {
268       IndexName name = IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]");
269       client.getIndex(name);
270       Assert.fail("No exception raised");
271     } catch (InvalidArgumentException e) {
272       // Expected exception.
273     }
274   }
275 
276   @Test
getIndexTest2()277   public void getIndexTest2() throws Exception {
278     Index expectedResponse =
279         Index.newBuilder()
280             .setName(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
281             .setDisplayName("displayName1714148973")
282             .setDescription("description-1724546052")
283             .setMetadataSchemaUri("metadataSchemaUri781971868")
284             .setMetadata(Value.newBuilder().setBoolValue(true).build())
285             .addAllDeployedIndexes(new ArrayList<DeployedIndexRef>())
286             .setEtag("etag3123477")
287             .putAllLabels(new HashMap<String, String>())
288             .setCreateTime(Timestamp.newBuilder().build())
289             .setUpdateTime(Timestamp.newBuilder().build())
290             .setIndexStats(IndexStats.newBuilder().build())
291             .build();
292     mockIndexService.addResponse(expectedResponse);
293 
294     String name = "name3373707";
295 
296     Index actualResponse = client.getIndex(name);
297     Assert.assertEquals(expectedResponse, actualResponse);
298 
299     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
300     Assert.assertEquals(1, actualRequests.size());
301     GetIndexRequest actualRequest = ((GetIndexRequest) actualRequests.get(0));
302 
303     Assert.assertEquals(name, actualRequest.getName());
304     Assert.assertTrue(
305         channelProvider.isHeaderSent(
306             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
307             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
308   }
309 
310   @Test
getIndexExceptionTest2()311   public void getIndexExceptionTest2() throws Exception {
312     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
313     mockIndexService.addException(exception);
314 
315     try {
316       String name = "name3373707";
317       client.getIndex(name);
318       Assert.fail("No exception raised");
319     } catch (InvalidArgumentException e) {
320       // Expected exception.
321     }
322   }
323 
324   @Test
listIndexesTest()325   public void listIndexesTest() throws Exception {
326     Index responsesElement = Index.newBuilder().build();
327     ListIndexesResponse expectedResponse =
328         ListIndexesResponse.newBuilder()
329             .setNextPageToken("")
330             .addAllIndexes(Arrays.asList(responsesElement))
331             .build();
332     mockIndexService.addResponse(expectedResponse);
333 
334     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
335 
336     ListIndexesPagedResponse pagedListResponse = client.listIndexes(parent);
337 
338     List<Index> resources = Lists.newArrayList(pagedListResponse.iterateAll());
339 
340     Assert.assertEquals(1, resources.size());
341     Assert.assertEquals(expectedResponse.getIndexesList().get(0), resources.get(0));
342 
343     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
344     Assert.assertEquals(1, actualRequests.size());
345     ListIndexesRequest actualRequest = ((ListIndexesRequest) actualRequests.get(0));
346 
347     Assert.assertEquals(parent.toString(), actualRequest.getParent());
348     Assert.assertTrue(
349         channelProvider.isHeaderSent(
350             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
351             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
352   }
353 
354   @Test
listIndexesExceptionTest()355   public void listIndexesExceptionTest() throws Exception {
356     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
357     mockIndexService.addException(exception);
358 
359     try {
360       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
361       client.listIndexes(parent);
362       Assert.fail("No exception raised");
363     } catch (InvalidArgumentException e) {
364       // Expected exception.
365     }
366   }
367 
368   @Test
listIndexesTest2()369   public void listIndexesTest2() throws Exception {
370     Index responsesElement = Index.newBuilder().build();
371     ListIndexesResponse expectedResponse =
372         ListIndexesResponse.newBuilder()
373             .setNextPageToken("")
374             .addAllIndexes(Arrays.asList(responsesElement))
375             .build();
376     mockIndexService.addResponse(expectedResponse);
377 
378     String parent = "parent-995424086";
379 
380     ListIndexesPagedResponse pagedListResponse = client.listIndexes(parent);
381 
382     List<Index> resources = Lists.newArrayList(pagedListResponse.iterateAll());
383 
384     Assert.assertEquals(1, resources.size());
385     Assert.assertEquals(expectedResponse.getIndexesList().get(0), resources.get(0));
386 
387     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
388     Assert.assertEquals(1, actualRequests.size());
389     ListIndexesRequest actualRequest = ((ListIndexesRequest) actualRequests.get(0));
390 
391     Assert.assertEquals(parent, actualRequest.getParent());
392     Assert.assertTrue(
393         channelProvider.isHeaderSent(
394             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
395             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
396   }
397 
398   @Test
listIndexesExceptionTest2()399   public void listIndexesExceptionTest2() throws Exception {
400     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
401     mockIndexService.addException(exception);
402 
403     try {
404       String parent = "parent-995424086";
405       client.listIndexes(parent);
406       Assert.fail("No exception raised");
407     } catch (InvalidArgumentException e) {
408       // Expected exception.
409     }
410   }
411 
412   @Test
updateIndexTest()413   public void updateIndexTest() throws Exception {
414     Index expectedResponse =
415         Index.newBuilder()
416             .setName(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
417             .setDisplayName("displayName1714148973")
418             .setDescription("description-1724546052")
419             .setMetadataSchemaUri("metadataSchemaUri781971868")
420             .setMetadata(Value.newBuilder().setBoolValue(true).build())
421             .addAllDeployedIndexes(new ArrayList<DeployedIndexRef>())
422             .setEtag("etag3123477")
423             .putAllLabels(new HashMap<String, String>())
424             .setCreateTime(Timestamp.newBuilder().build())
425             .setUpdateTime(Timestamp.newBuilder().build())
426             .setIndexStats(IndexStats.newBuilder().build())
427             .build();
428     Operation resultOperation =
429         Operation.newBuilder()
430             .setName("updateIndexTest")
431             .setDone(true)
432             .setResponse(Any.pack(expectedResponse))
433             .build();
434     mockIndexService.addResponse(resultOperation);
435 
436     Index index = Index.newBuilder().build();
437     FieldMask updateMask = FieldMask.newBuilder().build();
438 
439     Index actualResponse = client.updateIndexAsync(index, updateMask).get();
440     Assert.assertEquals(expectedResponse, actualResponse);
441 
442     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
443     Assert.assertEquals(1, actualRequests.size());
444     UpdateIndexRequest actualRequest = ((UpdateIndexRequest) actualRequests.get(0));
445 
446     Assert.assertEquals(index, actualRequest.getIndex());
447     Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
448     Assert.assertTrue(
449         channelProvider.isHeaderSent(
450             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
451             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
452   }
453 
454   @Test
updateIndexExceptionTest()455   public void updateIndexExceptionTest() throws Exception {
456     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
457     mockIndexService.addException(exception);
458 
459     try {
460       Index index = Index.newBuilder().build();
461       FieldMask updateMask = FieldMask.newBuilder().build();
462       client.updateIndexAsync(index, updateMask).get();
463       Assert.fail("No exception raised");
464     } catch (ExecutionException e) {
465       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
466       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
467       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
468     }
469   }
470 
471   @Test
deleteIndexTest()472   public void deleteIndexTest() throws Exception {
473     Empty expectedResponse = Empty.newBuilder().build();
474     Operation resultOperation =
475         Operation.newBuilder()
476             .setName("deleteIndexTest")
477             .setDone(true)
478             .setResponse(Any.pack(expectedResponse))
479             .build();
480     mockIndexService.addResponse(resultOperation);
481 
482     IndexName name = IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]");
483 
484     client.deleteIndexAsync(name).get();
485 
486     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
487     Assert.assertEquals(1, actualRequests.size());
488     DeleteIndexRequest actualRequest = ((DeleteIndexRequest) actualRequests.get(0));
489 
490     Assert.assertEquals(name.toString(), actualRequest.getName());
491     Assert.assertTrue(
492         channelProvider.isHeaderSent(
493             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
494             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
495   }
496 
497   @Test
deleteIndexExceptionTest()498   public void deleteIndexExceptionTest() throws Exception {
499     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
500     mockIndexService.addException(exception);
501 
502     try {
503       IndexName name = IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]");
504       client.deleteIndexAsync(name).get();
505       Assert.fail("No exception raised");
506     } catch (ExecutionException e) {
507       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
508       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
509       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
510     }
511   }
512 
513   @Test
deleteIndexTest2()514   public void deleteIndexTest2() throws Exception {
515     Empty expectedResponse = Empty.newBuilder().build();
516     Operation resultOperation =
517         Operation.newBuilder()
518             .setName("deleteIndexTest")
519             .setDone(true)
520             .setResponse(Any.pack(expectedResponse))
521             .build();
522     mockIndexService.addResponse(resultOperation);
523 
524     String name = "name3373707";
525 
526     client.deleteIndexAsync(name).get();
527 
528     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
529     Assert.assertEquals(1, actualRequests.size());
530     DeleteIndexRequest actualRequest = ((DeleteIndexRequest) actualRequests.get(0));
531 
532     Assert.assertEquals(name, actualRequest.getName());
533     Assert.assertTrue(
534         channelProvider.isHeaderSent(
535             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
536             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
537   }
538 
539   @Test
deleteIndexExceptionTest2()540   public void deleteIndexExceptionTest2() throws Exception {
541     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
542     mockIndexService.addException(exception);
543 
544     try {
545       String name = "name3373707";
546       client.deleteIndexAsync(name).get();
547       Assert.fail("No exception raised");
548     } catch (ExecutionException e) {
549       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
550       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
551       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
552     }
553   }
554 
555   @Test
upsertDatapointsTest()556   public void upsertDatapointsTest() throws Exception {
557     UpsertDatapointsResponse expectedResponse = UpsertDatapointsResponse.newBuilder().build();
558     mockIndexService.addResponse(expectedResponse);
559 
560     UpsertDatapointsRequest request =
561         UpsertDatapointsRequest.newBuilder()
562             .setIndex(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
563             .addAllDatapoints(new ArrayList<IndexDatapoint>())
564             .build();
565 
566     UpsertDatapointsResponse actualResponse = client.upsertDatapoints(request);
567     Assert.assertEquals(expectedResponse, actualResponse);
568 
569     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
570     Assert.assertEquals(1, actualRequests.size());
571     UpsertDatapointsRequest actualRequest = ((UpsertDatapointsRequest) actualRequests.get(0));
572 
573     Assert.assertEquals(request.getIndex(), actualRequest.getIndex());
574     Assert.assertEquals(request.getDatapointsList(), actualRequest.getDatapointsList());
575     Assert.assertTrue(
576         channelProvider.isHeaderSent(
577             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
578             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
579   }
580 
581   @Test
upsertDatapointsExceptionTest()582   public void upsertDatapointsExceptionTest() throws Exception {
583     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
584     mockIndexService.addException(exception);
585 
586     try {
587       UpsertDatapointsRequest request =
588           UpsertDatapointsRequest.newBuilder()
589               .setIndex(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
590               .addAllDatapoints(new ArrayList<IndexDatapoint>())
591               .build();
592       client.upsertDatapoints(request);
593       Assert.fail("No exception raised");
594     } catch (InvalidArgumentException e) {
595       // Expected exception.
596     }
597   }
598 
599   @Test
removeDatapointsTest()600   public void removeDatapointsTest() throws Exception {
601     RemoveDatapointsResponse expectedResponse = RemoveDatapointsResponse.newBuilder().build();
602     mockIndexService.addResponse(expectedResponse);
603 
604     RemoveDatapointsRequest request =
605         RemoveDatapointsRequest.newBuilder()
606             .setIndex(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
607             .addAllDatapointIds(new ArrayList<String>())
608             .build();
609 
610     RemoveDatapointsResponse actualResponse = client.removeDatapoints(request);
611     Assert.assertEquals(expectedResponse, actualResponse);
612 
613     List<AbstractMessage> actualRequests = mockIndexService.getRequests();
614     Assert.assertEquals(1, actualRequests.size());
615     RemoveDatapointsRequest actualRequest = ((RemoveDatapointsRequest) actualRequests.get(0));
616 
617     Assert.assertEquals(request.getIndex(), actualRequest.getIndex());
618     Assert.assertEquals(request.getDatapointIdsList(), actualRequest.getDatapointIdsList());
619     Assert.assertTrue(
620         channelProvider.isHeaderSent(
621             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
622             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
623   }
624 
625   @Test
removeDatapointsExceptionTest()626   public void removeDatapointsExceptionTest() throws Exception {
627     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
628     mockIndexService.addException(exception);
629 
630     try {
631       RemoveDatapointsRequest request =
632           RemoveDatapointsRequest.newBuilder()
633               .setIndex(IndexName.of("[PROJECT]", "[LOCATION]", "[INDEX]").toString())
634               .addAllDatapointIds(new ArrayList<String>())
635               .build();
636       client.removeDatapoints(request);
637       Assert.fail("No exception raised");
638     } catch (InvalidArgumentException e) {
639       // Expected exception.
640     }
641   }
642 
643   @Test
listLocationsTest()644   public void listLocationsTest() throws Exception {
645     Location responsesElement = Location.newBuilder().build();
646     ListLocationsResponse expectedResponse =
647         ListLocationsResponse.newBuilder()
648             .setNextPageToken("")
649             .addAllLocations(Arrays.asList(responsesElement))
650             .build();
651     mockLocations.addResponse(expectedResponse);
652 
653     ListLocationsRequest request =
654         ListLocationsRequest.newBuilder()
655             .setName("name3373707")
656             .setFilter("filter-1274492040")
657             .setPageSize(883849137)
658             .setPageToken("pageToken873572522")
659             .build();
660 
661     ListLocationsPagedResponse pagedListResponse = client.listLocations(request);
662 
663     List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll());
664 
665     Assert.assertEquals(1, resources.size());
666     Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0));
667 
668     List<AbstractMessage> actualRequests = mockLocations.getRequests();
669     Assert.assertEquals(1, actualRequests.size());
670     ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0));
671 
672     Assert.assertEquals(request.getName(), actualRequest.getName());
673     Assert.assertEquals(request.getFilter(), actualRequest.getFilter());
674     Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize());
675     Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken());
676     Assert.assertTrue(
677         channelProvider.isHeaderSent(
678             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
679             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
680   }
681 
682   @Test
listLocationsExceptionTest()683   public void listLocationsExceptionTest() throws Exception {
684     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
685     mockLocations.addException(exception);
686 
687     try {
688       ListLocationsRequest request =
689           ListLocationsRequest.newBuilder()
690               .setName("name3373707")
691               .setFilter("filter-1274492040")
692               .setPageSize(883849137)
693               .setPageToken("pageToken873572522")
694               .build();
695       client.listLocations(request);
696       Assert.fail("No exception raised");
697     } catch (InvalidArgumentException e) {
698       // Expected exception.
699     }
700   }
701 
702   @Test
getLocationTest()703   public void getLocationTest() throws Exception {
704     Location expectedResponse =
705         Location.newBuilder()
706             .setName("name3373707")
707             .setLocationId("locationId1541836720")
708             .setDisplayName("displayName1714148973")
709             .putAllLabels(new HashMap<String, String>())
710             .setMetadata(Any.newBuilder().build())
711             .build();
712     mockLocations.addResponse(expectedResponse);
713 
714     GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
715 
716     Location actualResponse = client.getLocation(request);
717     Assert.assertEquals(expectedResponse, actualResponse);
718 
719     List<AbstractMessage> actualRequests = mockLocations.getRequests();
720     Assert.assertEquals(1, actualRequests.size());
721     GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0));
722 
723     Assert.assertEquals(request.getName(), actualRequest.getName());
724     Assert.assertTrue(
725         channelProvider.isHeaderSent(
726             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
727             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
728   }
729 
730   @Test
getLocationExceptionTest()731   public void getLocationExceptionTest() throws Exception {
732     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
733     mockLocations.addException(exception);
734 
735     try {
736       GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
737       client.getLocation(request);
738       Assert.fail("No exception raised");
739     } catch (InvalidArgumentException e) {
740       // Expected exception.
741     }
742   }
743 
744   @Test
setIamPolicyTest()745   public void setIamPolicyTest() throws Exception {
746     Policy expectedResponse =
747         Policy.newBuilder()
748             .setVersion(351608024)
749             .addAllBindings(new ArrayList<Binding>())
750             .addAllAuditConfigs(new ArrayList<AuditConfig>())
751             .setEtag(ByteString.EMPTY)
752             .build();
753     mockIAMPolicy.addResponse(expectedResponse);
754 
755     SetIamPolicyRequest request =
756         SetIamPolicyRequest.newBuilder()
757             .setResource(
758                 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]")
759                     .toString())
760             .setPolicy(Policy.newBuilder().build())
761             .setUpdateMask(FieldMask.newBuilder().build())
762             .build();
763 
764     Policy actualResponse = client.setIamPolicy(request);
765     Assert.assertEquals(expectedResponse, actualResponse);
766 
767     List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests();
768     Assert.assertEquals(1, actualRequests.size());
769     SetIamPolicyRequest actualRequest = ((SetIamPolicyRequest) actualRequests.get(0));
770 
771     Assert.assertEquals(request.getResource(), actualRequest.getResource());
772     Assert.assertEquals(request.getPolicy(), actualRequest.getPolicy());
773     Assert.assertEquals(request.getUpdateMask(), actualRequest.getUpdateMask());
774     Assert.assertTrue(
775         channelProvider.isHeaderSent(
776             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
777             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
778   }
779 
780   @Test
setIamPolicyExceptionTest()781   public void setIamPolicyExceptionTest() throws Exception {
782     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
783     mockIAMPolicy.addException(exception);
784 
785     try {
786       SetIamPolicyRequest request =
787           SetIamPolicyRequest.newBuilder()
788               .setResource(
789                   EndpointName.ofProjectLocationEndpointName(
790                           "[PROJECT]", "[LOCATION]", "[ENDPOINT]")
791                       .toString())
792               .setPolicy(Policy.newBuilder().build())
793               .setUpdateMask(FieldMask.newBuilder().build())
794               .build();
795       client.setIamPolicy(request);
796       Assert.fail("No exception raised");
797     } catch (InvalidArgumentException e) {
798       // Expected exception.
799     }
800   }
801 
802   @Test
getIamPolicyTest()803   public void getIamPolicyTest() throws Exception {
804     Policy expectedResponse =
805         Policy.newBuilder()
806             .setVersion(351608024)
807             .addAllBindings(new ArrayList<Binding>())
808             .addAllAuditConfigs(new ArrayList<AuditConfig>())
809             .setEtag(ByteString.EMPTY)
810             .build();
811     mockIAMPolicy.addResponse(expectedResponse);
812 
813     GetIamPolicyRequest request =
814         GetIamPolicyRequest.newBuilder()
815             .setResource(
816                 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]")
817                     .toString())
818             .setOptions(GetPolicyOptions.newBuilder().build())
819             .build();
820 
821     Policy actualResponse = client.getIamPolicy(request);
822     Assert.assertEquals(expectedResponse, actualResponse);
823 
824     List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests();
825     Assert.assertEquals(1, actualRequests.size());
826     GetIamPolicyRequest actualRequest = ((GetIamPolicyRequest) actualRequests.get(0));
827 
828     Assert.assertEquals(request.getResource(), actualRequest.getResource());
829     Assert.assertEquals(request.getOptions(), actualRequest.getOptions());
830     Assert.assertTrue(
831         channelProvider.isHeaderSent(
832             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
833             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
834   }
835 
836   @Test
getIamPolicyExceptionTest()837   public void getIamPolicyExceptionTest() throws Exception {
838     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
839     mockIAMPolicy.addException(exception);
840 
841     try {
842       GetIamPolicyRequest request =
843           GetIamPolicyRequest.newBuilder()
844               .setResource(
845                   EndpointName.ofProjectLocationEndpointName(
846                           "[PROJECT]", "[LOCATION]", "[ENDPOINT]")
847                       .toString())
848               .setOptions(GetPolicyOptions.newBuilder().build())
849               .build();
850       client.getIamPolicy(request);
851       Assert.fail("No exception raised");
852     } catch (InvalidArgumentException e) {
853       // Expected exception.
854     }
855   }
856 
857   @Test
testIamPermissionsTest()858   public void testIamPermissionsTest() throws Exception {
859     TestIamPermissionsResponse expectedResponse =
860         TestIamPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build();
861     mockIAMPolicy.addResponse(expectedResponse);
862 
863     TestIamPermissionsRequest request =
864         TestIamPermissionsRequest.newBuilder()
865             .setResource(
866                 EndpointName.ofProjectLocationEndpointName("[PROJECT]", "[LOCATION]", "[ENDPOINT]")
867                     .toString())
868             .addAllPermissions(new ArrayList<String>())
869             .build();
870 
871     TestIamPermissionsResponse actualResponse = client.testIamPermissions(request);
872     Assert.assertEquals(expectedResponse, actualResponse);
873 
874     List<AbstractMessage> actualRequests = mockIAMPolicy.getRequests();
875     Assert.assertEquals(1, actualRequests.size());
876     TestIamPermissionsRequest actualRequest = ((TestIamPermissionsRequest) actualRequests.get(0));
877 
878     Assert.assertEquals(request.getResource(), actualRequest.getResource());
879     Assert.assertEquals(request.getPermissionsList(), actualRequest.getPermissionsList());
880     Assert.assertTrue(
881         channelProvider.isHeaderSent(
882             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
883             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
884   }
885 
886   @Test
testIamPermissionsExceptionTest()887   public void testIamPermissionsExceptionTest() throws Exception {
888     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
889     mockIAMPolicy.addException(exception);
890 
891     try {
892       TestIamPermissionsRequest request =
893           TestIamPermissionsRequest.newBuilder()
894               .setResource(
895                   EndpointName.ofProjectLocationEndpointName(
896                           "[PROJECT]", "[LOCATION]", "[ENDPOINT]")
897                       .toString())
898               .addAllPermissions(new ArrayList<String>())
899               .build();
900       client.testIamPermissions(request);
901       Assert.fail("No exception raised");
902     } catch (InvalidArgumentException e) {
903       // Expected exception.
904     }
905   }
906 }
907