• 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.RegionsClient.ListPagedResponse;
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.compute.v1.stub.HttpJsonRegionsStub;
31 import com.google.common.collect.Lists;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import javax.annotation.Generated;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Assert;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 
44 @Generated("by gapic-generator-java")
45 public class RegionsClientTest {
46   private static MockHttpService mockService;
47   private static RegionsClient client;
48 
49   @BeforeClass
startStaticServer()50   public static void startStaticServer() throws IOException {
51     mockService =
52         new MockHttpService(
53             HttpJsonRegionsStub.getMethodDescriptors(), RegionsSettings.getDefaultEndpoint());
54     RegionsSettings settings =
55         RegionsSettings.newBuilder()
56             .setTransportChannelProvider(
57                 RegionsSettings.defaultHttpJsonTransportProviderBuilder()
58                     .setHttpTransport(mockService)
59                     .build())
60             .setCredentialsProvider(NoCredentialsProvider.create())
61             .build();
62     client = RegionsClient.create(settings);
63   }
64 
65   @AfterClass
stopServer()66   public static void stopServer() {
67     client.close();
68   }
69 
70   @Before
setUp()71   public void setUp() {}
72 
73   @After
tearDown()74   public void tearDown() throws Exception {
75     mockService.reset();
76   }
77 
78   @Test
getTest()79   public void getTest() throws Exception {
80     Region expectedResponse =
81         Region.newBuilder()
82             .setCreationTimestamp("creationTimestamp-370203401")
83             .setDeprecated(DeprecationStatus.newBuilder().build())
84             .setDescription("description-1724546052")
85             .setId(3355)
86             .setKind("kind3292052")
87             .setName("name3373707")
88             .addAllQuotas(new ArrayList<Quota>())
89             .setSelfLink("selfLink1191800166")
90             .setStatus("status-892481550")
91             .setSupportsPzs(true)
92             .addAllZones(new ArrayList<String>())
93             .build();
94     mockService.addResponse(expectedResponse);
95 
96     String project = "project-6911";
97     String region = "region-9622";
98 
99     Region actualResponse = client.get(project, region);
100     Assert.assertEquals(expectedResponse, actualResponse);
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
getExceptionTest()118   public void getExceptionTest() 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       String project = "project-6911";
126       String region = "region-9622";
127       client.get(project, region);
128       Assert.fail("No exception raised");
129     } catch (InvalidArgumentException e) {
130       // Expected exception.
131     }
132   }
133 
134   @Test
listTest()135   public void listTest() throws Exception {
136     Region responsesElement = Region.newBuilder().build();
137     RegionList expectedResponse =
138         RegionList.newBuilder()
139             .setNextPageToken("")
140             .addAllItems(Arrays.asList(responsesElement))
141             .build();
142     mockService.addResponse(expectedResponse);
143 
144     String project = "project-6911";
145 
146     ListPagedResponse pagedListResponse = client.list(project);
147 
148     List<Region> resources = Lists.newArrayList(pagedListResponse.iterateAll());
149 
150     Assert.assertEquals(1, resources.size());
151     Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0));
152 
153     List<String> actualRequests = mockService.getRequestPaths();
154     Assert.assertEquals(1, actualRequests.size());
155 
156     String apiClientHeaderKey =
157         mockService
158             .getRequestHeaders()
159             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
160             .iterator()
161             .next();
162     Assert.assertTrue(
163         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
164             .matcher(apiClientHeaderKey)
165             .matches());
166   }
167 
168   @Test
listExceptionTest()169   public void listExceptionTest() throws Exception {
170     ApiException exception =
171         ApiExceptionFactory.createException(
172             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
173     mockService.addException(exception);
174 
175     try {
176       String project = "project-6911";
177       client.list(project);
178       Assert.fail("No exception raised");
179     } catch (InvalidArgumentException e) {
180       // Expected exception.
181     }
182   }
183 }
184