• 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.ids.v1;
18 
19 import static com.google.cloud.ids.v1.IDSClient.ListEndpointsPagedResponse;
20 
21 import com.google.api.gax.core.NoCredentialsProvider;
22 import com.google.api.gax.grpc.GaxGrpcProperties;
23 import com.google.api.gax.grpc.testing.LocalChannelProvider;
24 import com.google.api.gax.grpc.testing.MockGrpcService;
25 import com.google.api.gax.grpc.testing.MockServiceHelper;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.common.collect.Lists;
30 import com.google.longrunning.Operation;
31 import com.google.protobuf.AbstractMessage;
32 import com.google.protobuf.Any;
33 import com.google.protobuf.Empty;
34 import com.google.protobuf.Timestamp;
35 import io.grpc.StatusRuntimeException;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.UUID;
41 import java.util.concurrent.ExecutionException;
42 import javax.annotation.Generated;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 @Generated("by gapic-generator-java")
51 public class IDSClientTest {
52   private static MockIDS mockIDS;
53   private static MockServiceHelper mockServiceHelper;
54   private LocalChannelProvider channelProvider;
55   private IDSClient client;
56 
57   @BeforeClass
startStaticServer()58   public static void startStaticServer() {
59     mockIDS = new MockIDS();
60     mockServiceHelper =
61         new MockServiceHelper(
62             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockIDS));
63     mockServiceHelper.start();
64   }
65 
66   @AfterClass
stopServer()67   public static void stopServer() {
68     mockServiceHelper.stop();
69   }
70 
71   @Before
setUp()72   public void setUp() throws IOException {
73     mockServiceHelper.reset();
74     channelProvider = mockServiceHelper.createChannelProvider();
75     IDSSettings settings =
76         IDSSettings.newBuilder()
77             .setTransportChannelProvider(channelProvider)
78             .setCredentialsProvider(NoCredentialsProvider.create())
79             .build();
80     client = IDSClient.create(settings);
81   }
82 
83   @After
tearDown()84   public void tearDown() throws Exception {
85     client.close();
86   }
87 
88   @Test
listEndpointsTest()89   public void listEndpointsTest() throws Exception {
90     Endpoint responsesElement = Endpoint.newBuilder().build();
91     ListEndpointsResponse expectedResponse =
92         ListEndpointsResponse.newBuilder()
93             .setNextPageToken("")
94             .addAllEndpoints(Arrays.asList(responsesElement))
95             .build();
96     mockIDS.addResponse(expectedResponse);
97 
98     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
99 
100     ListEndpointsPagedResponse pagedListResponse = client.listEndpoints(parent);
101 
102     List<Endpoint> resources = Lists.newArrayList(pagedListResponse.iterateAll());
103 
104     Assert.assertEquals(1, resources.size());
105     Assert.assertEquals(expectedResponse.getEndpointsList().get(0), resources.get(0));
106 
107     List<AbstractMessage> actualRequests = mockIDS.getRequests();
108     Assert.assertEquals(1, actualRequests.size());
109     ListEndpointsRequest actualRequest = ((ListEndpointsRequest) actualRequests.get(0));
110 
111     Assert.assertEquals(parent.toString(), actualRequest.getParent());
112     Assert.assertTrue(
113         channelProvider.isHeaderSent(
114             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
115             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
116   }
117 
118   @Test
listEndpointsExceptionTest()119   public void listEndpointsExceptionTest() throws Exception {
120     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
121     mockIDS.addException(exception);
122 
123     try {
124       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
125       client.listEndpoints(parent);
126       Assert.fail("No exception raised");
127     } catch (InvalidArgumentException e) {
128       // Expected exception.
129     }
130   }
131 
132   @Test
listEndpointsTest2()133   public void listEndpointsTest2() throws Exception {
134     Endpoint responsesElement = Endpoint.newBuilder().build();
135     ListEndpointsResponse expectedResponse =
136         ListEndpointsResponse.newBuilder()
137             .setNextPageToken("")
138             .addAllEndpoints(Arrays.asList(responsesElement))
139             .build();
140     mockIDS.addResponse(expectedResponse);
141 
142     String parent = "parent-995424086";
143 
144     ListEndpointsPagedResponse pagedListResponse = client.listEndpoints(parent);
145 
146     List<Endpoint> resources = Lists.newArrayList(pagedListResponse.iterateAll());
147 
148     Assert.assertEquals(1, resources.size());
149     Assert.assertEquals(expectedResponse.getEndpointsList().get(0), resources.get(0));
150 
151     List<AbstractMessage> actualRequests = mockIDS.getRequests();
152     Assert.assertEquals(1, actualRequests.size());
153     ListEndpointsRequest actualRequest = ((ListEndpointsRequest) actualRequests.get(0));
154 
155     Assert.assertEquals(parent, actualRequest.getParent());
156     Assert.assertTrue(
157         channelProvider.isHeaderSent(
158             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
159             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
160   }
161 
162   @Test
listEndpointsExceptionTest2()163   public void listEndpointsExceptionTest2() throws Exception {
164     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
165     mockIDS.addException(exception);
166 
167     try {
168       String parent = "parent-995424086";
169       client.listEndpoints(parent);
170       Assert.fail("No exception raised");
171     } catch (InvalidArgumentException e) {
172       // Expected exception.
173     }
174   }
175 
176   @Test
getEndpointTest()177   public void getEndpointTest() throws Exception {
178     Endpoint expectedResponse =
179         Endpoint.newBuilder()
180             .setName(EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]").toString())
181             .setCreateTime(Timestamp.newBuilder().build())
182             .setUpdateTime(Timestamp.newBuilder().build())
183             .putAllLabels(new HashMap<String, String>())
184             .setNetwork("network1843485230")
185             .setEndpointForwardingRule("endpointForwardingRule1448974478")
186             .setEndpointIp("endpointIp-1837754980")
187             .setDescription("description-1724546052")
188             .setTrafficLogs(true)
189             .build();
190     mockIDS.addResponse(expectedResponse);
191 
192     EndpointName name = EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]");
193 
194     Endpoint actualResponse = client.getEndpoint(name);
195     Assert.assertEquals(expectedResponse, actualResponse);
196 
197     List<AbstractMessage> actualRequests = mockIDS.getRequests();
198     Assert.assertEquals(1, actualRequests.size());
199     GetEndpointRequest actualRequest = ((GetEndpointRequest) actualRequests.get(0));
200 
201     Assert.assertEquals(name.toString(), actualRequest.getName());
202     Assert.assertTrue(
203         channelProvider.isHeaderSent(
204             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
205             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
206   }
207 
208   @Test
getEndpointExceptionTest()209   public void getEndpointExceptionTest() throws Exception {
210     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
211     mockIDS.addException(exception);
212 
213     try {
214       EndpointName name = EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]");
215       client.getEndpoint(name);
216       Assert.fail("No exception raised");
217     } catch (InvalidArgumentException e) {
218       // Expected exception.
219     }
220   }
221 
222   @Test
getEndpointTest2()223   public void getEndpointTest2() throws Exception {
224     Endpoint expectedResponse =
225         Endpoint.newBuilder()
226             .setName(EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]").toString())
227             .setCreateTime(Timestamp.newBuilder().build())
228             .setUpdateTime(Timestamp.newBuilder().build())
229             .putAllLabels(new HashMap<String, String>())
230             .setNetwork("network1843485230")
231             .setEndpointForwardingRule("endpointForwardingRule1448974478")
232             .setEndpointIp("endpointIp-1837754980")
233             .setDescription("description-1724546052")
234             .setTrafficLogs(true)
235             .build();
236     mockIDS.addResponse(expectedResponse);
237 
238     String name = "name3373707";
239 
240     Endpoint actualResponse = client.getEndpoint(name);
241     Assert.assertEquals(expectedResponse, actualResponse);
242 
243     List<AbstractMessage> actualRequests = mockIDS.getRequests();
244     Assert.assertEquals(1, actualRequests.size());
245     GetEndpointRequest actualRequest = ((GetEndpointRequest) actualRequests.get(0));
246 
247     Assert.assertEquals(name, actualRequest.getName());
248     Assert.assertTrue(
249         channelProvider.isHeaderSent(
250             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
251             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
252   }
253 
254   @Test
getEndpointExceptionTest2()255   public void getEndpointExceptionTest2() throws Exception {
256     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
257     mockIDS.addException(exception);
258 
259     try {
260       String name = "name3373707";
261       client.getEndpoint(name);
262       Assert.fail("No exception raised");
263     } catch (InvalidArgumentException e) {
264       // Expected exception.
265     }
266   }
267 
268   @Test
createEndpointTest()269   public void createEndpointTest() throws Exception {
270     Endpoint expectedResponse =
271         Endpoint.newBuilder()
272             .setName(EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]").toString())
273             .setCreateTime(Timestamp.newBuilder().build())
274             .setUpdateTime(Timestamp.newBuilder().build())
275             .putAllLabels(new HashMap<String, String>())
276             .setNetwork("network1843485230")
277             .setEndpointForwardingRule("endpointForwardingRule1448974478")
278             .setEndpointIp("endpointIp-1837754980")
279             .setDescription("description-1724546052")
280             .setTrafficLogs(true)
281             .build();
282     Operation resultOperation =
283         Operation.newBuilder()
284             .setName("createEndpointTest")
285             .setDone(true)
286             .setResponse(Any.pack(expectedResponse))
287             .build();
288     mockIDS.addResponse(resultOperation);
289 
290     LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
291     Endpoint endpoint = Endpoint.newBuilder().build();
292     String endpointId = "endpointId-1837754992";
293 
294     Endpoint actualResponse = client.createEndpointAsync(parent, endpoint, endpointId).get();
295     Assert.assertEquals(expectedResponse, actualResponse);
296 
297     List<AbstractMessage> actualRequests = mockIDS.getRequests();
298     Assert.assertEquals(1, actualRequests.size());
299     CreateEndpointRequest actualRequest = ((CreateEndpointRequest) actualRequests.get(0));
300 
301     Assert.assertEquals(parent.toString(), actualRequest.getParent());
302     Assert.assertEquals(endpoint, actualRequest.getEndpoint());
303     Assert.assertEquals(endpointId, actualRequest.getEndpointId());
304     Assert.assertTrue(
305         channelProvider.isHeaderSent(
306             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
307             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
308   }
309 
310   @Test
createEndpointExceptionTest()311   public void createEndpointExceptionTest() throws Exception {
312     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
313     mockIDS.addException(exception);
314 
315     try {
316       LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]");
317       Endpoint endpoint = Endpoint.newBuilder().build();
318       String endpointId = "endpointId-1837754992";
319       client.createEndpointAsync(parent, endpoint, endpointId).get();
320       Assert.fail("No exception raised");
321     } catch (ExecutionException e) {
322       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
323       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
324       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
325     }
326   }
327 
328   @Test
createEndpointTest2()329   public void createEndpointTest2() throws Exception {
330     Endpoint expectedResponse =
331         Endpoint.newBuilder()
332             .setName(EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]").toString())
333             .setCreateTime(Timestamp.newBuilder().build())
334             .setUpdateTime(Timestamp.newBuilder().build())
335             .putAllLabels(new HashMap<String, String>())
336             .setNetwork("network1843485230")
337             .setEndpointForwardingRule("endpointForwardingRule1448974478")
338             .setEndpointIp("endpointIp-1837754980")
339             .setDescription("description-1724546052")
340             .setTrafficLogs(true)
341             .build();
342     Operation resultOperation =
343         Operation.newBuilder()
344             .setName("createEndpointTest")
345             .setDone(true)
346             .setResponse(Any.pack(expectedResponse))
347             .build();
348     mockIDS.addResponse(resultOperation);
349 
350     String parent = "parent-995424086";
351     Endpoint endpoint = Endpoint.newBuilder().build();
352     String endpointId = "endpointId-1837754992";
353 
354     Endpoint actualResponse = client.createEndpointAsync(parent, endpoint, endpointId).get();
355     Assert.assertEquals(expectedResponse, actualResponse);
356 
357     List<AbstractMessage> actualRequests = mockIDS.getRequests();
358     Assert.assertEquals(1, actualRequests.size());
359     CreateEndpointRequest actualRequest = ((CreateEndpointRequest) actualRequests.get(0));
360 
361     Assert.assertEquals(parent, actualRequest.getParent());
362     Assert.assertEquals(endpoint, actualRequest.getEndpoint());
363     Assert.assertEquals(endpointId, actualRequest.getEndpointId());
364     Assert.assertTrue(
365         channelProvider.isHeaderSent(
366             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
367             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
368   }
369 
370   @Test
createEndpointExceptionTest2()371   public void createEndpointExceptionTest2() throws Exception {
372     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
373     mockIDS.addException(exception);
374 
375     try {
376       String parent = "parent-995424086";
377       Endpoint endpoint = Endpoint.newBuilder().build();
378       String endpointId = "endpointId-1837754992";
379       client.createEndpointAsync(parent, endpoint, endpointId).get();
380       Assert.fail("No exception raised");
381     } catch (ExecutionException e) {
382       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
383       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
384       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
385     }
386   }
387 
388   @Test
deleteEndpointTest()389   public void deleteEndpointTest() throws Exception {
390     Empty expectedResponse = Empty.newBuilder().build();
391     Operation resultOperation =
392         Operation.newBuilder()
393             .setName("deleteEndpointTest")
394             .setDone(true)
395             .setResponse(Any.pack(expectedResponse))
396             .build();
397     mockIDS.addResponse(resultOperation);
398 
399     EndpointName name = EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]");
400 
401     client.deleteEndpointAsync(name).get();
402 
403     List<AbstractMessage> actualRequests = mockIDS.getRequests();
404     Assert.assertEquals(1, actualRequests.size());
405     DeleteEndpointRequest actualRequest = ((DeleteEndpointRequest) actualRequests.get(0));
406 
407     Assert.assertEquals(name.toString(), actualRequest.getName());
408     Assert.assertTrue(
409         channelProvider.isHeaderSent(
410             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
411             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
412   }
413 
414   @Test
deleteEndpointExceptionTest()415   public void deleteEndpointExceptionTest() throws Exception {
416     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
417     mockIDS.addException(exception);
418 
419     try {
420       EndpointName name = EndpointName.of("[PROJECT]", "[LOCATION]", "[ENDPOINT]");
421       client.deleteEndpointAsync(name).get();
422       Assert.fail("No exception raised");
423     } catch (ExecutionException e) {
424       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
425       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
426       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
427     }
428   }
429 
430   @Test
deleteEndpointTest2()431   public void deleteEndpointTest2() throws Exception {
432     Empty expectedResponse = Empty.newBuilder().build();
433     Operation resultOperation =
434         Operation.newBuilder()
435             .setName("deleteEndpointTest")
436             .setDone(true)
437             .setResponse(Any.pack(expectedResponse))
438             .build();
439     mockIDS.addResponse(resultOperation);
440 
441     String name = "name3373707";
442 
443     client.deleteEndpointAsync(name).get();
444 
445     List<AbstractMessage> actualRequests = mockIDS.getRequests();
446     Assert.assertEquals(1, actualRequests.size());
447     DeleteEndpointRequest actualRequest = ((DeleteEndpointRequest) actualRequests.get(0));
448 
449     Assert.assertEquals(name, actualRequest.getName());
450     Assert.assertTrue(
451         channelProvider.isHeaderSent(
452             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
453             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
454   }
455 
456   @Test
deleteEndpointExceptionTest2()457   public void deleteEndpointExceptionTest2() throws Exception {
458     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
459     mockIDS.addException(exception);
460 
461     try {
462       String name = "name3373707";
463       client.deleteEndpointAsync(name).get();
464       Assert.fail("No exception raised");
465     } catch (ExecutionException e) {
466       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
467       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
468       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
469     }
470   }
471 }
472