• 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.httpjson.GaxHttpJsonProperties;
23 import com.google.api.gax.httpjson.testing.MockHttpService;
24 import com.google.api.gax.rpc.ApiClientHeaderProvider;
25 import com.google.api.gax.rpc.ApiException;
26 import com.google.api.gax.rpc.ApiExceptionFactory;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.api.gax.rpc.testing.FakeStatusCode;
30 import com.google.cloud.networkmanagement.v1.stub.HttpJsonReachabilityServiceStub;
31 import com.google.common.collect.Lists;
32 import com.google.longrunning.Operation;
33 import com.google.protobuf.Any;
34 import com.google.protobuf.Empty;
35 import com.google.protobuf.FieldMask;
36 import com.google.protobuf.Timestamp;
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.concurrent.ExecutionException;
43 import javax.annotation.Generated;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 
51 @Generated("by gapic-generator-java")
52 public class ReachabilityServiceClientHttpJsonTest {
53   private static MockHttpService mockService;
54   private static ReachabilityServiceClient client;
55 
56   @BeforeClass
startStaticServer()57   public static void startStaticServer() throws IOException {
58     mockService =
59         new MockHttpService(
60             HttpJsonReachabilityServiceStub.getMethodDescriptors(),
61             ReachabilityServiceSettings.getDefaultEndpoint());
62     ReachabilityServiceSettings settings =
63         ReachabilityServiceSettings.newHttpJsonBuilder()
64             .setTransportChannelProvider(
65                 ReachabilityServiceSettings.defaultHttpJsonTransportProviderBuilder()
66                     .setHttpTransport(mockService)
67                     .build())
68             .setCredentialsProvider(NoCredentialsProvider.create())
69             .build();
70     client = ReachabilityServiceClient.create(settings);
71   }
72 
73   @AfterClass
stopServer()74   public static void stopServer() {
75     client.close();
76   }
77 
78   @Before
setUp()79   public void setUp() {}
80 
81   @After
tearDown()82   public void tearDown() throws Exception {
83     mockService.reset();
84   }
85 
86   @Test
listConnectivityTestsTest()87   public void listConnectivityTestsTest() throws Exception {
88     ConnectivityTest responsesElement = ConnectivityTest.newBuilder().build();
89     ListConnectivityTestsResponse expectedResponse =
90         ListConnectivityTestsResponse.newBuilder()
91             .setNextPageToken("")
92             .addAllResources(Arrays.asList(responsesElement))
93             .build();
94     mockService.addResponse(expectedResponse);
95 
96     String parent = "projects/project-6156/locations/global";
97 
98     ListConnectivityTestsPagedResponse pagedListResponse = client.listConnectivityTests(parent);
99 
100     List<ConnectivityTest> resources = Lists.newArrayList(pagedListResponse.iterateAll());
101 
102     Assert.assertEquals(1, resources.size());
103     Assert.assertEquals(expectedResponse.getResourcesList().get(0), resources.get(0));
104 
105     List<String> actualRequests = mockService.getRequestPaths();
106     Assert.assertEquals(1, actualRequests.size());
107 
108     String apiClientHeaderKey =
109         mockService
110             .getRequestHeaders()
111             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
112             .iterator()
113             .next();
114     Assert.assertTrue(
115         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
116             .matcher(apiClientHeaderKey)
117             .matches());
118   }
119 
120   @Test
listConnectivityTestsExceptionTest()121   public void listConnectivityTestsExceptionTest() throws Exception {
122     ApiException exception =
123         ApiExceptionFactory.createException(
124             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
125     mockService.addException(exception);
126 
127     try {
128       String parent = "projects/project-6156/locations/global";
129       client.listConnectivityTests(parent);
130       Assert.fail("No exception raised");
131     } catch (InvalidArgumentException e) {
132       // Expected exception.
133     }
134   }
135 
136   @Test
getConnectivityTestTest()137   public void getConnectivityTestTest() throws Exception {
138     ConnectivityTest expectedResponse =
139         ConnectivityTest.newBuilder()
140             .setName("name3373707")
141             .setDescription("description-1724546052")
142             .setSource(Endpoint.newBuilder().build())
143             .setDestination(Endpoint.newBuilder().build())
144             .setProtocol("protocol-989163880")
145             .addAllRelatedProjects(new ArrayList<String>())
146             .setDisplayName("displayName1714148973")
147             .putAllLabels(new HashMap<String, String>())
148             .setCreateTime(Timestamp.newBuilder().build())
149             .setUpdateTime(Timestamp.newBuilder().build())
150             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
151             .build();
152     mockService.addResponse(expectedResponse);
153 
154     String name = "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569";
155 
156     ConnectivityTest actualResponse = client.getConnectivityTest(name);
157     Assert.assertEquals(expectedResponse, actualResponse);
158 
159     List<String> actualRequests = mockService.getRequestPaths();
160     Assert.assertEquals(1, actualRequests.size());
161 
162     String apiClientHeaderKey =
163         mockService
164             .getRequestHeaders()
165             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
166             .iterator()
167             .next();
168     Assert.assertTrue(
169         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
170             .matcher(apiClientHeaderKey)
171             .matches());
172   }
173 
174   @Test
getConnectivityTestExceptionTest()175   public void getConnectivityTestExceptionTest() throws Exception {
176     ApiException exception =
177         ApiExceptionFactory.createException(
178             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
179     mockService.addException(exception);
180 
181     try {
182       String name =
183           "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569";
184       client.getConnectivityTest(name);
185       Assert.fail("No exception raised");
186     } catch (InvalidArgumentException e) {
187       // Expected exception.
188     }
189   }
190 
191   @Test
createConnectivityTestTest()192   public void createConnectivityTestTest() throws Exception {
193     ConnectivityTest expectedResponse =
194         ConnectivityTest.newBuilder()
195             .setName("name3373707")
196             .setDescription("description-1724546052")
197             .setSource(Endpoint.newBuilder().build())
198             .setDestination(Endpoint.newBuilder().build())
199             .setProtocol("protocol-989163880")
200             .addAllRelatedProjects(new ArrayList<String>())
201             .setDisplayName("displayName1714148973")
202             .putAllLabels(new HashMap<String, String>())
203             .setCreateTime(Timestamp.newBuilder().build())
204             .setUpdateTime(Timestamp.newBuilder().build())
205             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
206             .build();
207     Operation resultOperation =
208         Operation.newBuilder()
209             .setName("createConnectivityTestTest")
210             .setDone(true)
211             .setResponse(Any.pack(expectedResponse))
212             .build();
213     mockService.addResponse(resultOperation);
214 
215     String parent = "projects/project-6156/locations/global";
216     String testId = "testId-877170355";
217     ConnectivityTest resource = ConnectivityTest.newBuilder().build();
218 
219     ConnectivityTest actualResponse =
220         client.createConnectivityTestAsync(parent, testId, resource).get();
221     Assert.assertEquals(expectedResponse, actualResponse);
222 
223     List<String> actualRequests = mockService.getRequestPaths();
224     Assert.assertEquals(1, actualRequests.size());
225 
226     String apiClientHeaderKey =
227         mockService
228             .getRequestHeaders()
229             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
230             .iterator()
231             .next();
232     Assert.assertTrue(
233         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
234             .matcher(apiClientHeaderKey)
235             .matches());
236   }
237 
238   @Test
createConnectivityTestExceptionTest()239   public void createConnectivityTestExceptionTest() throws Exception {
240     ApiException exception =
241         ApiExceptionFactory.createException(
242             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
243     mockService.addException(exception);
244 
245     try {
246       String parent = "projects/project-6156/locations/global";
247       String testId = "testId-877170355";
248       ConnectivityTest resource = ConnectivityTest.newBuilder().build();
249       client.createConnectivityTestAsync(parent, testId, resource).get();
250       Assert.fail("No exception raised");
251     } catch (ExecutionException e) {
252     }
253   }
254 
255   @Test
updateConnectivityTestTest()256   public void updateConnectivityTestTest() throws Exception {
257     ConnectivityTest expectedResponse =
258         ConnectivityTest.newBuilder()
259             .setName("name3373707")
260             .setDescription("description-1724546052")
261             .setSource(Endpoint.newBuilder().build())
262             .setDestination(Endpoint.newBuilder().build())
263             .setProtocol("protocol-989163880")
264             .addAllRelatedProjects(new ArrayList<String>())
265             .setDisplayName("displayName1714148973")
266             .putAllLabels(new HashMap<String, String>())
267             .setCreateTime(Timestamp.newBuilder().build())
268             .setUpdateTime(Timestamp.newBuilder().build())
269             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
270             .build();
271     Operation resultOperation =
272         Operation.newBuilder()
273             .setName("updateConnectivityTestTest")
274             .setDone(true)
275             .setResponse(Any.pack(expectedResponse))
276             .build();
277     mockService.addResponse(resultOperation);
278 
279     FieldMask updateMask = FieldMask.newBuilder().build();
280     ConnectivityTest resource =
281         ConnectivityTest.newBuilder()
282             .setName(
283                 "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569")
284             .setDescription("description-1724546052")
285             .setSource(Endpoint.newBuilder().build())
286             .setDestination(Endpoint.newBuilder().build())
287             .setProtocol("protocol-989163880")
288             .addAllRelatedProjects(new ArrayList<String>())
289             .setDisplayName("displayName1714148973")
290             .putAllLabels(new HashMap<String, String>())
291             .setCreateTime(Timestamp.newBuilder().build())
292             .setUpdateTime(Timestamp.newBuilder().build())
293             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
294             .build();
295 
296     ConnectivityTest actualResponse =
297         client.updateConnectivityTestAsync(updateMask, resource).get();
298     Assert.assertEquals(expectedResponse, actualResponse);
299 
300     List<String> actualRequests = mockService.getRequestPaths();
301     Assert.assertEquals(1, actualRequests.size());
302 
303     String apiClientHeaderKey =
304         mockService
305             .getRequestHeaders()
306             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
307             .iterator()
308             .next();
309     Assert.assertTrue(
310         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
311             .matcher(apiClientHeaderKey)
312             .matches());
313   }
314 
315   @Test
updateConnectivityTestExceptionTest()316   public void updateConnectivityTestExceptionTest() throws Exception {
317     ApiException exception =
318         ApiExceptionFactory.createException(
319             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
320     mockService.addException(exception);
321 
322     try {
323       FieldMask updateMask = FieldMask.newBuilder().build();
324       ConnectivityTest resource =
325           ConnectivityTest.newBuilder()
326               .setName(
327                   "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569")
328               .setDescription("description-1724546052")
329               .setSource(Endpoint.newBuilder().build())
330               .setDestination(Endpoint.newBuilder().build())
331               .setProtocol("protocol-989163880")
332               .addAllRelatedProjects(new ArrayList<String>())
333               .setDisplayName("displayName1714148973")
334               .putAllLabels(new HashMap<String, String>())
335               .setCreateTime(Timestamp.newBuilder().build())
336               .setUpdateTime(Timestamp.newBuilder().build())
337               .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
338               .build();
339       client.updateConnectivityTestAsync(updateMask, resource).get();
340       Assert.fail("No exception raised");
341     } catch (ExecutionException e) {
342     }
343   }
344 
345   @Test
rerunConnectivityTestTest()346   public void rerunConnectivityTestTest() throws Exception {
347     ConnectivityTest expectedResponse =
348         ConnectivityTest.newBuilder()
349             .setName("name3373707")
350             .setDescription("description-1724546052")
351             .setSource(Endpoint.newBuilder().build())
352             .setDestination(Endpoint.newBuilder().build())
353             .setProtocol("protocol-989163880")
354             .addAllRelatedProjects(new ArrayList<String>())
355             .setDisplayName("displayName1714148973")
356             .putAllLabels(new HashMap<String, String>())
357             .setCreateTime(Timestamp.newBuilder().build())
358             .setUpdateTime(Timestamp.newBuilder().build())
359             .setReachabilityDetails(ReachabilityDetails.newBuilder().build())
360             .build();
361     Operation resultOperation =
362         Operation.newBuilder()
363             .setName("rerunConnectivityTestTest")
364             .setDone(true)
365             .setResponse(Any.pack(expectedResponse))
366             .build();
367     mockService.addResponse(resultOperation);
368 
369     RerunConnectivityTestRequest request =
370         RerunConnectivityTestRequest.newBuilder()
371             .setName(
372                 "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569")
373             .build();
374 
375     ConnectivityTest actualResponse = client.rerunConnectivityTestAsync(request).get();
376     Assert.assertEquals(expectedResponse, actualResponse);
377 
378     List<String> actualRequests = mockService.getRequestPaths();
379     Assert.assertEquals(1, actualRequests.size());
380 
381     String apiClientHeaderKey =
382         mockService
383             .getRequestHeaders()
384             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
385             .iterator()
386             .next();
387     Assert.assertTrue(
388         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
389             .matcher(apiClientHeaderKey)
390             .matches());
391   }
392 
393   @Test
rerunConnectivityTestExceptionTest()394   public void rerunConnectivityTestExceptionTest() throws Exception {
395     ApiException exception =
396         ApiExceptionFactory.createException(
397             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
398     mockService.addException(exception);
399 
400     try {
401       RerunConnectivityTestRequest request =
402           RerunConnectivityTestRequest.newBuilder()
403               .setName(
404                   "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569")
405               .build();
406       client.rerunConnectivityTestAsync(request).get();
407       Assert.fail("No exception raised");
408     } catch (ExecutionException e) {
409     }
410   }
411 
412   @Test
deleteConnectivityTestTest()413   public void deleteConnectivityTestTest() throws Exception {
414     Empty expectedResponse = Empty.newBuilder().build();
415     Operation resultOperation =
416         Operation.newBuilder()
417             .setName("deleteConnectivityTestTest")
418             .setDone(true)
419             .setResponse(Any.pack(expectedResponse))
420             .build();
421     mockService.addResponse(resultOperation);
422 
423     String name = "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569";
424 
425     client.deleteConnectivityTestAsync(name).get();
426 
427     List<String> actualRequests = mockService.getRequestPaths();
428     Assert.assertEquals(1, actualRequests.size());
429 
430     String apiClientHeaderKey =
431         mockService
432             .getRequestHeaders()
433             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
434             .iterator()
435             .next();
436     Assert.assertTrue(
437         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
438             .matcher(apiClientHeaderKey)
439             .matches());
440   }
441 
442   @Test
deleteConnectivityTestExceptionTest()443   public void deleteConnectivityTestExceptionTest() throws Exception {
444     ApiException exception =
445         ApiExceptionFactory.createException(
446             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
447     mockService.addException(exception);
448 
449     try {
450       String name =
451           "projects/project-4569/locations/global/connectivityTests/connectivityTest-4569";
452       client.deleteConnectivityTestAsync(name).get();
453       Assert.fail("No exception raised");
454     } catch (ExecutionException e) {
455     }
456   }
457 }
458