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