• 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.networkmanagement.v1;
18 
19 import static com.google.cloud.networkmanagement.v1.ReachabilityServiceClient.ListConnectivityTestsPagedResponse;
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.FieldMask;
35 import com.google.protobuf.Timestamp;
36 import io.grpc.StatusRuntimeException;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.UUID;
43 import java.util.concurrent.ExecutionException;
44 import javax.annotation.Generated;
45 import org.junit.After;
46 import org.junit.AfterClass;
47 import org.junit.Assert;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 
52 @Generated("by gapic-generator-java")
53 public class ReachabilityServiceClientTest {
54   private static MockReachabilityService mockReachabilityService;
55   private static MockServiceHelper mockServiceHelper;
56   private LocalChannelProvider channelProvider;
57   private ReachabilityServiceClient client;
58 
59   @BeforeClass
startStaticServer()60   public static void startStaticServer() {
61     mockReachabilityService = new MockReachabilityService();
62     mockServiceHelper =
63         new MockServiceHelper(
64             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockReachabilityService));
65     mockServiceHelper.start();
66   }
67 
68   @AfterClass
stopServer()69   public static void stopServer() {
70     mockServiceHelper.stop();
71   }
72 
73   @Before
setUp()74   public void setUp() throws IOException {
75     mockServiceHelper.reset();
76     channelProvider = mockServiceHelper.createChannelProvider();
77     ReachabilityServiceSettings settings =
78         ReachabilityServiceSettings.newBuilder()
79             .setTransportChannelProvider(channelProvider)
80             .setCredentialsProvider(NoCredentialsProvider.create())
81             .build();
82     client = ReachabilityServiceClient.create(settings);
83   }
84 
85   @After
tearDown()86   public void tearDown() throws Exception {
87     client.close();
88   }
89 
90   @Test
listConnectivityTestsTest()91   public void listConnectivityTestsTest() throws Exception {
92     ConnectivityTest responsesElement = ConnectivityTest.newBuilder().build();
93     ListConnectivityTestsResponse expectedResponse =
94         ListConnectivityTestsResponse.newBuilder()
95             .setNextPageToken("")
96             .addAllResources(Arrays.asList(responsesElement))
97             .build();
98     mockReachabilityService.addResponse(expectedResponse);
99 
100     String parent = "parent-995424086";
101 
102     ListConnectivityTestsPagedResponse pagedListResponse = client.listConnectivityTests(parent);
103 
104     List<ConnectivityTest> resources = Lists.newArrayList(pagedListResponse.iterateAll());
105 
106     Assert.assertEquals(1, resources.size());
107     Assert.assertEquals(expectedResponse.getResourcesList().get(0), resources.get(0));
108 
109     List<AbstractMessage> actualRequests = mockReachabilityService.getRequests();
110     Assert.assertEquals(1, actualRequests.size());
111     ListConnectivityTestsRequest actualRequest =
112         ((ListConnectivityTestsRequest) actualRequests.get(0));
113 
114     Assert.assertEquals(parent, actualRequest.getParent());
115     Assert.assertTrue(
116         channelProvider.isHeaderSent(
117             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
118             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
119   }
120 
121   @Test
listConnectivityTestsExceptionTest()122   public void listConnectivityTestsExceptionTest() throws Exception {
123     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
124     mockReachabilityService.addException(exception);
125 
126     try {
127       String parent = "parent-995424086";
128       client.listConnectivityTests(parent);
129       Assert.fail("No exception raised");
130     } catch (InvalidArgumentException e) {
131       // Expected exception.
132     }
133   }
134 
135   @Test
getConnectivityTestTest()136   public void getConnectivityTestTest() throws Exception {
137     ConnectivityTest expectedResponse =
138         ConnectivityTest.newBuilder()
139             .setName("name3373707")
140             .setDescription("description-1724546052")
141             .setSource(Endpoint.newBuilder().build())
142             .setDestination(Endpoint.newBuilder().build())
143             .setProtocol("protocol-989163880")
144             .addAllRelatedProjects(new ArrayList<String>())
145             .setDisplayName("displayName1714148973")
146             .putAllLabels(new HashMap<String, String>())
147             .setCreateTime(Timestamp.newBuilder().build())
148             .setUpdateTime(Timestamp.newBuilder().build())
149             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
150             .build();
151     mockReachabilityService.addResponse(expectedResponse);
152 
153     String name = "name3373707";
154 
155     ConnectivityTest actualResponse = client.getConnectivityTest(name);
156     Assert.assertEquals(expectedResponse, actualResponse);
157 
158     List<AbstractMessage> actualRequests = mockReachabilityService.getRequests();
159     Assert.assertEquals(1, actualRequests.size());
160     GetConnectivityTestRequest actualRequest = ((GetConnectivityTestRequest) actualRequests.get(0));
161 
162     Assert.assertEquals(name, actualRequest.getName());
163     Assert.assertTrue(
164         channelProvider.isHeaderSent(
165             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
166             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
167   }
168 
169   @Test
getConnectivityTestExceptionTest()170   public void getConnectivityTestExceptionTest() throws Exception {
171     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
172     mockReachabilityService.addException(exception);
173 
174     try {
175       String name = "name3373707";
176       client.getConnectivityTest(name);
177       Assert.fail("No exception raised");
178     } catch (InvalidArgumentException e) {
179       // Expected exception.
180     }
181   }
182 
183   @Test
createConnectivityTestTest()184   public void createConnectivityTestTest() throws Exception {
185     ConnectivityTest expectedResponse =
186         ConnectivityTest.newBuilder()
187             .setName("name3373707")
188             .setDescription("description-1724546052")
189             .setSource(Endpoint.newBuilder().build())
190             .setDestination(Endpoint.newBuilder().build())
191             .setProtocol("protocol-989163880")
192             .addAllRelatedProjects(new ArrayList<String>())
193             .setDisplayName("displayName1714148973")
194             .putAllLabels(new HashMap<String, String>())
195             .setCreateTime(Timestamp.newBuilder().build())
196             .setUpdateTime(Timestamp.newBuilder().build())
197             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
198             .build();
199     Operation resultOperation =
200         Operation.newBuilder()
201             .setName("createConnectivityTestTest")
202             .setDone(true)
203             .setResponse(Any.pack(expectedResponse))
204             .build();
205     mockReachabilityService.addResponse(resultOperation);
206 
207     String parent = "parent-995424086";
208     String testId = "testId-877170355";
209     ConnectivityTest resource = ConnectivityTest.newBuilder().build();
210 
211     ConnectivityTest actualResponse =
212         client.createConnectivityTestAsync(parent, testId, resource).get();
213     Assert.assertEquals(expectedResponse, actualResponse);
214 
215     List<AbstractMessage> actualRequests = mockReachabilityService.getRequests();
216     Assert.assertEquals(1, actualRequests.size());
217     CreateConnectivityTestRequest actualRequest =
218         ((CreateConnectivityTestRequest) actualRequests.get(0));
219 
220     Assert.assertEquals(parent, actualRequest.getParent());
221     Assert.assertEquals(testId, actualRequest.getTestId());
222     Assert.assertEquals(resource, actualRequest.getResource());
223     Assert.assertTrue(
224         channelProvider.isHeaderSent(
225             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
226             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
227   }
228 
229   @Test
createConnectivityTestExceptionTest()230   public void createConnectivityTestExceptionTest() throws Exception {
231     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
232     mockReachabilityService.addException(exception);
233 
234     try {
235       String parent = "parent-995424086";
236       String testId = "testId-877170355";
237       ConnectivityTest resource = ConnectivityTest.newBuilder().build();
238       client.createConnectivityTestAsync(parent, testId, resource).get();
239       Assert.fail("No exception raised");
240     } catch (ExecutionException e) {
241       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
242       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
243       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
244     }
245   }
246 
247   @Test
updateConnectivityTestTest()248   public void updateConnectivityTestTest() throws Exception {
249     ConnectivityTest expectedResponse =
250         ConnectivityTest.newBuilder()
251             .setName("name3373707")
252             .setDescription("description-1724546052")
253             .setSource(Endpoint.newBuilder().build())
254             .setDestination(Endpoint.newBuilder().build())
255             .setProtocol("protocol-989163880")
256             .addAllRelatedProjects(new ArrayList<String>())
257             .setDisplayName("displayName1714148973")
258             .putAllLabels(new HashMap<String, String>())
259             .setCreateTime(Timestamp.newBuilder().build())
260             .setUpdateTime(Timestamp.newBuilder().build())
261             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
262             .build();
263     Operation resultOperation =
264         Operation.newBuilder()
265             .setName("updateConnectivityTestTest")
266             .setDone(true)
267             .setResponse(Any.pack(expectedResponse))
268             .build();
269     mockReachabilityService.addResponse(resultOperation);
270 
271     FieldMask updateMask = FieldMask.newBuilder().build();
272     ConnectivityTest resource = ConnectivityTest.newBuilder().build();
273 
274     ConnectivityTest actualResponse =
275         client.updateConnectivityTestAsync(updateMask, resource).get();
276     Assert.assertEquals(expectedResponse, actualResponse);
277 
278     List<AbstractMessage> actualRequests = mockReachabilityService.getRequests();
279     Assert.assertEquals(1, actualRequests.size());
280     UpdateConnectivityTestRequest actualRequest =
281         ((UpdateConnectivityTestRequest) actualRequests.get(0));
282 
283     Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
284     Assert.assertEquals(resource, actualRequest.getResource());
285     Assert.assertTrue(
286         channelProvider.isHeaderSent(
287             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
288             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
289   }
290 
291   @Test
updateConnectivityTestExceptionTest()292   public void updateConnectivityTestExceptionTest() throws Exception {
293     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
294     mockReachabilityService.addException(exception);
295 
296     try {
297       FieldMask updateMask = FieldMask.newBuilder().build();
298       ConnectivityTest resource = ConnectivityTest.newBuilder().build();
299       client.updateConnectivityTestAsync(updateMask, resource).get();
300       Assert.fail("No exception raised");
301     } catch (ExecutionException e) {
302       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
303       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
304       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
305     }
306   }
307 
308   @Test
rerunConnectivityTestTest()309   public void rerunConnectivityTestTest() throws Exception {
310     ConnectivityTest expectedResponse =
311         ConnectivityTest.newBuilder()
312             .setName("name3373707")
313             .setDescription("description-1724546052")
314             .setSource(Endpoint.newBuilder().build())
315             .setDestination(Endpoint.newBuilder().build())
316             .setProtocol("protocol-989163880")
317             .addAllRelatedProjects(new ArrayList<String>())
318             .setDisplayName("displayName1714148973")
319             .putAllLabels(new HashMap<String, String>())
320             .setCreateTime(Timestamp.newBuilder().build())
321             .setUpdateTime(Timestamp.newBuilder().build())
322             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
323             .build();
324     Operation resultOperation =
325         Operation.newBuilder()
326             .setName("rerunConnectivityTestTest")
327             .setDone(true)
328             .setResponse(Any.pack(expectedResponse))
329             .build();
330     mockReachabilityService.addResponse(resultOperation);
331 
332     RerunConnectivityTestRequest request =
333         RerunConnectivityTestRequest.newBuilder().setName("name3373707").build();
334 
335     ConnectivityTest actualResponse = client.rerunConnectivityTestAsync(request).get();
336     Assert.assertEquals(expectedResponse, actualResponse);
337 
338     List<AbstractMessage> actualRequests = mockReachabilityService.getRequests();
339     Assert.assertEquals(1, actualRequests.size());
340     RerunConnectivityTestRequest actualRequest =
341         ((RerunConnectivityTestRequest) actualRequests.get(0));
342 
343     Assert.assertEquals(request.getName(), actualRequest.getName());
344     Assert.assertTrue(
345         channelProvider.isHeaderSent(
346             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
347             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
348   }
349 
350   @Test
rerunConnectivityTestExceptionTest()351   public void rerunConnectivityTestExceptionTest() throws Exception {
352     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
353     mockReachabilityService.addException(exception);
354 
355     try {
356       RerunConnectivityTestRequest request =
357           RerunConnectivityTestRequest.newBuilder().setName("name3373707").build();
358       client.rerunConnectivityTestAsync(request).get();
359       Assert.fail("No exception raised");
360     } catch (ExecutionException e) {
361       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
362       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
363       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
364     }
365   }
366 
367   @Test
deleteConnectivityTestTest()368   public void deleteConnectivityTestTest() throws Exception {
369     Empty expectedResponse = Empty.newBuilder().build();
370     Operation resultOperation =
371         Operation.newBuilder()
372             .setName("deleteConnectivityTestTest")
373             .setDone(true)
374             .setResponse(Any.pack(expectedResponse))
375             .build();
376     mockReachabilityService.addResponse(resultOperation);
377 
378     String name = "name3373707";
379 
380     client.deleteConnectivityTestAsync(name).get();
381 
382     List<AbstractMessage> actualRequests = mockReachabilityService.getRequests();
383     Assert.assertEquals(1, actualRequests.size());
384     DeleteConnectivityTestRequest actualRequest =
385         ((DeleteConnectivityTestRequest) actualRequests.get(0));
386 
387     Assert.assertEquals(name, actualRequest.getName());
388     Assert.assertTrue(
389         channelProvider.isHeaderSent(
390             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
391             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
392   }
393 
394   @Test
deleteConnectivityTestExceptionTest()395   public void deleteConnectivityTestExceptionTest() throws Exception {
396     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
397     mockReachabilityService.addException(exception);
398 
399     try {
400       String name = "name3373707";
401       client.deleteConnectivityTestAsync(name).get();
402       Assert.fail("No exception raised");
403     } catch (ExecutionException e) {
404       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
405       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
406       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
407     }
408   }
409 }
410