• 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.compute.v1;
18 
19 import static com.google.cloud.compute.v1.SslPoliciesClient.AggregatedListPagedResponse;
20 import static com.google.cloud.compute.v1.SslPoliciesClient.ListPagedResponse;
21 
22 import com.google.api.gax.core.NoCredentialsProvider;
23 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
24 import com.google.api.gax.httpjson.testing.MockHttpService;
25 import com.google.api.gax.rpc.ApiClientHeaderProvider;
26 import com.google.api.gax.rpc.ApiException;
27 import com.google.api.gax.rpc.ApiExceptionFactory;
28 import com.google.api.gax.rpc.InvalidArgumentException;
29 import com.google.api.gax.rpc.StatusCode;
30 import com.google.api.gax.rpc.testing.FakeStatusCode;
31 import com.google.cloud.compute.v1.Operation.Status;
32 import com.google.cloud.compute.v1.stub.HttpJsonSslPoliciesStub;
33 import com.google.common.collect.Lists;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Map;
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 SslPoliciesClientTest {
51   private static MockHttpService mockService;
52   private static SslPoliciesClient client;
53 
54   @BeforeClass
startStaticServer()55   public static void startStaticServer() throws IOException {
56     mockService =
57         new MockHttpService(
58             HttpJsonSslPoliciesStub.getMethodDescriptors(),
59             SslPoliciesSettings.getDefaultEndpoint());
60     SslPoliciesSettings settings =
61         SslPoliciesSettings.newBuilder()
62             .setTransportChannelProvider(
63                 SslPoliciesSettings.defaultHttpJsonTransportProviderBuilder()
64                     .setHttpTransport(mockService)
65                     .build())
66             .setCredentialsProvider(NoCredentialsProvider.create())
67             .build();
68     client = SslPoliciesClient.create(settings);
69   }
70 
71   @AfterClass
stopServer()72   public static void stopServer() {
73     client.close();
74   }
75 
76   @Before
setUp()77   public void setUp() {}
78 
79   @After
tearDown()80   public void tearDown() throws Exception {
81     mockService.reset();
82   }
83 
84   @Test
aggregatedListTest()85   public void aggregatedListTest() throws Exception {
86     SslPoliciesScopedList responsesElement = SslPoliciesScopedList.newBuilder().build();
87     SslPoliciesAggregatedList expectedResponse =
88         SslPoliciesAggregatedList.newBuilder()
89             .setNextPageToken("")
90             .putAllItems(Collections.singletonMap("items", responsesElement))
91             .build();
92     mockService.addResponse(expectedResponse);
93 
94     String project = "project-6911";
95 
96     AggregatedListPagedResponse pagedListResponse = client.aggregatedList(project);
97 
98     List<Map.Entry<String, SslPoliciesScopedList>> resources =
99         Lists.newArrayList(pagedListResponse.iterateAll());
100 
101     Assert.assertEquals(1, resources.size());
102     Assert.assertEquals(
103         expectedResponse.getItemsMap().entrySet().iterator().next(), 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
aggregatedListExceptionTest()121   public void aggregatedListExceptionTest() 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 project = "project-6911";
129       client.aggregatedList(project);
130       Assert.fail("No exception raised");
131     } catch (InvalidArgumentException e) {
132       // Expected exception.
133     }
134   }
135 
136   @Test
deleteTest()137   public void deleteTest() throws Exception {
138     Operation expectedResponse =
139         Operation.newBuilder()
140             .setClientOperationId("clientOperationId-1230366697")
141             .setCreationTimestamp("creationTimestamp-370203401")
142             .setDescription("description-1724546052")
143             .setEndTime("endTime-1607243192")
144             .setError(Error.newBuilder().build())
145             .setHttpErrorMessage("httpErrorMessage1577303431")
146             .setHttpErrorStatusCode(0)
147             .setId(3355)
148             .setInsertTime("insertTime966165798")
149             .setKind("kind3292052")
150             .setName("name3373707")
151             .setOperationGroupId("operationGroupId1716161683")
152             .setOperationType("operationType91999553")
153             .setProgress(-1001078227)
154             .setRegion("region-934795532")
155             .setSelfLink("selfLink1191800166")
156             .setStartTime("startTime-2129294769")
157             .setStatus(Status.DONE)
158             .setStatusMessage("statusMessage-958704715")
159             .setTargetId(-815576439)
160             .setTargetLink("targetLink486368555")
161             .setUser("user3599307")
162             .addAllWarnings(new ArrayList<Warnings>())
163             .setZone("zone3744684")
164             .build();
165     mockService.addResponse(expectedResponse);
166 
167     String project = "project-6911";
168     String sslPolicy = "sslPolicy-6284";
169 
170     Operation actualResponse = client.deleteAsync(project, sslPolicy).get();
171     Assert.assertEquals(expectedResponse, actualResponse);
172 
173     List<String> actualRequests = mockService.getRequestPaths();
174     Assert.assertEquals(1, actualRequests.size());
175 
176     String apiClientHeaderKey =
177         mockService
178             .getRequestHeaders()
179             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
180             .iterator()
181             .next();
182     Assert.assertTrue(
183         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
184             .matcher(apiClientHeaderKey)
185             .matches());
186   }
187 
188   @Test
deleteExceptionTest()189   public void deleteExceptionTest() throws Exception {
190     ApiException exception =
191         ApiExceptionFactory.createException(
192             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
193     mockService.addException(exception);
194 
195     try {
196       String project = "project-6911";
197       String sslPolicy = "sslPolicy-6284";
198       client.deleteAsync(project, sslPolicy).get();
199       Assert.fail("No exception raised");
200     } catch (ExecutionException e) {
201     }
202   }
203 
204   @Test
getTest()205   public void getTest() throws Exception {
206     SslPolicy expectedResponse =
207         SslPolicy.newBuilder()
208             .setCreationTimestamp("creationTimestamp-370203401")
209             .addAllCustomFeatures(new ArrayList<String>())
210             .setDescription("description-1724546052")
211             .addAllEnabledFeatures(new ArrayList<String>())
212             .setFingerprint("fingerprint-1375934236")
213             .setId(3355)
214             .setKind("kind3292052")
215             .setMinTlsVersion("minTlsVersion-2026358961")
216             .setName("name3373707")
217             .setProfile("profile-309425751")
218             .setRegion("region-934795532")
219             .setSelfLink("selfLink1191800166")
220             .addAllWarnings(new ArrayList<Warnings>())
221             .build();
222     mockService.addResponse(expectedResponse);
223 
224     String project = "project-6911";
225     String sslPolicy = "sslPolicy-6284";
226 
227     SslPolicy actualResponse = client.get(project, sslPolicy);
228     Assert.assertEquals(expectedResponse, actualResponse);
229 
230     List<String> actualRequests = mockService.getRequestPaths();
231     Assert.assertEquals(1, actualRequests.size());
232 
233     String apiClientHeaderKey =
234         mockService
235             .getRequestHeaders()
236             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
237             .iterator()
238             .next();
239     Assert.assertTrue(
240         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
241             .matcher(apiClientHeaderKey)
242             .matches());
243   }
244 
245   @Test
getExceptionTest()246   public void getExceptionTest() throws Exception {
247     ApiException exception =
248         ApiExceptionFactory.createException(
249             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
250     mockService.addException(exception);
251 
252     try {
253       String project = "project-6911";
254       String sslPolicy = "sslPolicy-6284";
255       client.get(project, sslPolicy);
256       Assert.fail("No exception raised");
257     } catch (InvalidArgumentException e) {
258       // Expected exception.
259     }
260   }
261 
262   @Test
insertTest()263   public void insertTest() throws Exception {
264     Operation expectedResponse =
265         Operation.newBuilder()
266             .setClientOperationId("clientOperationId-1230366697")
267             .setCreationTimestamp("creationTimestamp-370203401")
268             .setDescription("description-1724546052")
269             .setEndTime("endTime-1607243192")
270             .setError(Error.newBuilder().build())
271             .setHttpErrorMessage("httpErrorMessage1577303431")
272             .setHttpErrorStatusCode(0)
273             .setId(3355)
274             .setInsertTime("insertTime966165798")
275             .setKind("kind3292052")
276             .setName("name3373707")
277             .setOperationGroupId("operationGroupId1716161683")
278             .setOperationType("operationType91999553")
279             .setProgress(-1001078227)
280             .setRegion("region-934795532")
281             .setSelfLink("selfLink1191800166")
282             .setStartTime("startTime-2129294769")
283             .setStatus(Status.DONE)
284             .setStatusMessage("statusMessage-958704715")
285             .setTargetId(-815576439)
286             .setTargetLink("targetLink486368555")
287             .setUser("user3599307")
288             .addAllWarnings(new ArrayList<Warnings>())
289             .setZone("zone3744684")
290             .build();
291     mockService.addResponse(expectedResponse);
292 
293     String project = "project-6911";
294     SslPolicy sslPolicyResource = SslPolicy.newBuilder().build();
295 
296     Operation actualResponse = client.insertAsync(project, sslPolicyResource).get();
297     Assert.assertEquals(expectedResponse, actualResponse);
298 
299     List<String> actualRequests = mockService.getRequestPaths();
300     Assert.assertEquals(1, actualRequests.size());
301 
302     String apiClientHeaderKey =
303         mockService
304             .getRequestHeaders()
305             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
306             .iterator()
307             .next();
308     Assert.assertTrue(
309         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
310             .matcher(apiClientHeaderKey)
311             .matches());
312   }
313 
314   @Test
insertExceptionTest()315   public void insertExceptionTest() throws Exception {
316     ApiException exception =
317         ApiExceptionFactory.createException(
318             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
319     mockService.addException(exception);
320 
321     try {
322       String project = "project-6911";
323       SslPolicy sslPolicyResource = SslPolicy.newBuilder().build();
324       client.insertAsync(project, sslPolicyResource).get();
325       Assert.fail("No exception raised");
326     } catch (ExecutionException e) {
327     }
328   }
329 
330   @Test
listTest()331   public void listTest() throws Exception {
332     SslPolicy responsesElement = SslPolicy.newBuilder().build();
333     SslPoliciesList expectedResponse =
334         SslPoliciesList.newBuilder()
335             .setNextPageToken("")
336             .addAllItems(Arrays.asList(responsesElement))
337             .build();
338     mockService.addResponse(expectedResponse);
339 
340     String project = "project-6911";
341 
342     ListPagedResponse pagedListResponse = client.list(project);
343 
344     List<SslPolicy> resources = Lists.newArrayList(pagedListResponse.iterateAll());
345 
346     Assert.assertEquals(1, resources.size());
347     Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0));
348 
349     List<String> actualRequests = mockService.getRequestPaths();
350     Assert.assertEquals(1, actualRequests.size());
351 
352     String apiClientHeaderKey =
353         mockService
354             .getRequestHeaders()
355             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
356             .iterator()
357             .next();
358     Assert.assertTrue(
359         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
360             .matcher(apiClientHeaderKey)
361             .matches());
362   }
363 
364   @Test
listExceptionTest()365   public void listExceptionTest() throws Exception {
366     ApiException exception =
367         ApiExceptionFactory.createException(
368             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
369     mockService.addException(exception);
370 
371     try {
372       String project = "project-6911";
373       client.list(project);
374       Assert.fail("No exception raised");
375     } catch (InvalidArgumentException e) {
376       // Expected exception.
377     }
378   }
379 
380   @Test
listAvailableFeaturesTest()381   public void listAvailableFeaturesTest() throws Exception {
382     SslPoliciesListAvailableFeaturesResponse expectedResponse =
383         SslPoliciesListAvailableFeaturesResponse.newBuilder()
384             .addAllFeatures(new ArrayList<String>())
385             .build();
386     mockService.addResponse(expectedResponse);
387 
388     String project = "project-6911";
389 
390     SslPoliciesListAvailableFeaturesResponse actualResponse = client.listAvailableFeatures(project);
391     Assert.assertEquals(expectedResponse, actualResponse);
392 
393     List<String> actualRequests = mockService.getRequestPaths();
394     Assert.assertEquals(1, actualRequests.size());
395 
396     String apiClientHeaderKey =
397         mockService
398             .getRequestHeaders()
399             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
400             .iterator()
401             .next();
402     Assert.assertTrue(
403         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
404             .matcher(apiClientHeaderKey)
405             .matches());
406   }
407 
408   @Test
listAvailableFeaturesExceptionTest()409   public void listAvailableFeaturesExceptionTest() throws Exception {
410     ApiException exception =
411         ApiExceptionFactory.createException(
412             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
413     mockService.addException(exception);
414 
415     try {
416       String project = "project-6911";
417       client.listAvailableFeatures(project);
418       Assert.fail("No exception raised");
419     } catch (InvalidArgumentException e) {
420       // Expected exception.
421     }
422   }
423 
424   @Test
patchTest()425   public void patchTest() throws Exception {
426     Operation expectedResponse =
427         Operation.newBuilder()
428             .setClientOperationId("clientOperationId-1230366697")
429             .setCreationTimestamp("creationTimestamp-370203401")
430             .setDescription("description-1724546052")
431             .setEndTime("endTime-1607243192")
432             .setError(Error.newBuilder().build())
433             .setHttpErrorMessage("httpErrorMessage1577303431")
434             .setHttpErrorStatusCode(0)
435             .setId(3355)
436             .setInsertTime("insertTime966165798")
437             .setKind("kind3292052")
438             .setName("name3373707")
439             .setOperationGroupId("operationGroupId1716161683")
440             .setOperationType("operationType91999553")
441             .setProgress(-1001078227)
442             .setRegion("region-934795532")
443             .setSelfLink("selfLink1191800166")
444             .setStartTime("startTime-2129294769")
445             .setStatus(Status.DONE)
446             .setStatusMessage("statusMessage-958704715")
447             .setTargetId(-815576439)
448             .setTargetLink("targetLink486368555")
449             .setUser("user3599307")
450             .addAllWarnings(new ArrayList<Warnings>())
451             .setZone("zone3744684")
452             .build();
453     mockService.addResponse(expectedResponse);
454 
455     String project = "project-6911";
456     String sslPolicy = "sslPolicy-6284";
457     SslPolicy sslPolicyResource = SslPolicy.newBuilder().build();
458 
459     Operation actualResponse = client.patchAsync(project, sslPolicy, sslPolicyResource).get();
460     Assert.assertEquals(expectedResponse, actualResponse);
461 
462     List<String> actualRequests = mockService.getRequestPaths();
463     Assert.assertEquals(1, actualRequests.size());
464 
465     String apiClientHeaderKey =
466         mockService
467             .getRequestHeaders()
468             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
469             .iterator()
470             .next();
471     Assert.assertTrue(
472         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
473             .matcher(apiClientHeaderKey)
474             .matches());
475   }
476 
477   @Test
patchExceptionTest()478   public void patchExceptionTest() throws Exception {
479     ApiException exception =
480         ApiExceptionFactory.createException(
481             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
482     mockService.addException(exception);
483 
484     try {
485       String project = "project-6911";
486       String sslPolicy = "sslPolicy-6284";
487       SslPolicy sslPolicyResource = SslPolicy.newBuilder().build();
488       client.patchAsync(project, sslPolicy, sslPolicyResource).get();
489       Assert.fail("No exception raised");
490     } catch (ExecutionException e) {
491     }
492   }
493 }
494