• 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.gkehub.v1alpha;
18 
19 import static com.google.cloud.gkehub.v1alpha.GkeHubClient.ListFeaturesPagedResponse;
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.gkehub.v1alpha.stub.HttpJsonGkeHubStub;
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.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.concurrent.ExecutionException;
42 import javax.annotation.Generated;
43 import org.junit.After;
44 import org.junit.AfterClass;
45 import org.junit.Assert;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 
50 @Generated("by gapic-generator-java")
51 public class GkeHubClientHttpJsonTest {
52   private static MockHttpService mockService;
53   private static GkeHubClient client;
54 
55   @BeforeClass
startStaticServer()56   public static void startStaticServer() throws IOException {
57     mockService =
58         new MockHttpService(
59             HttpJsonGkeHubStub.getMethodDescriptors(), GkeHubSettings.getDefaultEndpoint());
60     GkeHubSettings settings =
61         GkeHubSettings.newHttpJsonBuilder()
62             .setTransportChannelProvider(
63                 GkeHubSettings.defaultHttpJsonTransportProviderBuilder()
64                     .setHttpTransport(mockService)
65                     .build())
66             .setCredentialsProvider(NoCredentialsProvider.create())
67             .build();
68     client = GkeHubClient.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
listFeaturesTest()85   public void listFeaturesTest() throws Exception {
86     Feature responsesElement = Feature.newBuilder().build();
87     ListFeaturesResponse expectedResponse =
88         ListFeaturesResponse.newBuilder()
89             .setNextPageToken("")
90             .addAllResources(Arrays.asList(responsesElement))
91             .build();
92     mockService.addResponse(expectedResponse);
93 
94     String parent = "projects/project-5833/locations/location-5833";
95 
96     ListFeaturesPagedResponse pagedListResponse = client.listFeatures(parent);
97 
98     List<Feature> resources = Lists.newArrayList(pagedListResponse.iterateAll());
99 
100     Assert.assertEquals(1, resources.size());
101     Assert.assertEquals(expectedResponse.getResourcesList().get(0), resources.get(0));
102 
103     List<String> actualRequests = mockService.getRequestPaths();
104     Assert.assertEquals(1, actualRequests.size());
105 
106     String apiClientHeaderKey =
107         mockService
108             .getRequestHeaders()
109             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
110             .iterator()
111             .next();
112     Assert.assertTrue(
113         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
114             .matcher(apiClientHeaderKey)
115             .matches());
116   }
117 
118   @Test
listFeaturesExceptionTest()119   public void listFeaturesExceptionTest() throws Exception {
120     ApiException exception =
121         ApiExceptionFactory.createException(
122             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
123     mockService.addException(exception);
124 
125     try {
126       String parent = "projects/project-5833/locations/location-5833";
127       client.listFeatures(parent);
128       Assert.fail("No exception raised");
129     } catch (InvalidArgumentException e) {
130       // Expected exception.
131     }
132   }
133 
134   @Test
getFeatureTest()135   public void getFeatureTest() throws Exception {
136     Feature expectedResponse =
137         Feature.newBuilder()
138             .setName("name3373707")
139             .putAllLabels(new HashMap<String, String>())
140             .setResourceState(FeatureResourceState.newBuilder().build())
141             .setSpec(CommonFeatureSpec.newBuilder().build())
142             .putAllMembershipSpecs(new HashMap<String, MembershipFeatureSpec>())
143             .setState(CommonFeatureState.newBuilder().build())
144             .putAllMembershipStates(new HashMap<String, MembershipFeatureState>())
145             .setCreateTime(Timestamp.newBuilder().build())
146             .setUpdateTime(Timestamp.newBuilder().build())
147             .setDeleteTime(Timestamp.newBuilder().build())
148             .build();
149     mockService.addResponse(expectedResponse);
150 
151     String name = "projects/project-8365/locations/location-8365/features/feature-8365";
152 
153     Feature actualResponse = client.getFeature(name);
154     Assert.assertEquals(expectedResponse, actualResponse);
155 
156     List<String> actualRequests = mockService.getRequestPaths();
157     Assert.assertEquals(1, actualRequests.size());
158 
159     String apiClientHeaderKey =
160         mockService
161             .getRequestHeaders()
162             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
163             .iterator()
164             .next();
165     Assert.assertTrue(
166         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
167             .matcher(apiClientHeaderKey)
168             .matches());
169   }
170 
171   @Test
getFeatureExceptionTest()172   public void getFeatureExceptionTest() throws Exception {
173     ApiException exception =
174         ApiExceptionFactory.createException(
175             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
176     mockService.addException(exception);
177 
178     try {
179       String name = "projects/project-8365/locations/location-8365/features/feature-8365";
180       client.getFeature(name);
181       Assert.fail("No exception raised");
182     } catch (InvalidArgumentException e) {
183       // Expected exception.
184     }
185   }
186 
187   @Test
createFeatureTest()188   public void createFeatureTest() throws Exception {
189     Feature expectedResponse =
190         Feature.newBuilder()
191             .setName("name3373707")
192             .putAllLabels(new HashMap<String, String>())
193             .setResourceState(FeatureResourceState.newBuilder().build())
194             .setSpec(CommonFeatureSpec.newBuilder().build())
195             .putAllMembershipSpecs(new HashMap<String, MembershipFeatureSpec>())
196             .setState(CommonFeatureState.newBuilder().build())
197             .putAllMembershipStates(new HashMap<String, MembershipFeatureState>())
198             .setCreateTime(Timestamp.newBuilder().build())
199             .setUpdateTime(Timestamp.newBuilder().build())
200             .setDeleteTime(Timestamp.newBuilder().build())
201             .build();
202     Operation resultOperation =
203         Operation.newBuilder()
204             .setName("createFeatureTest")
205             .setDone(true)
206             .setResponse(Any.pack(expectedResponse))
207             .build();
208     mockService.addResponse(resultOperation);
209 
210     String parent = "projects/project-5833/locations/location-5833";
211     Feature resource = Feature.newBuilder().build();
212     String featureId = "featureId-420503887";
213 
214     Feature actualResponse = client.createFeatureAsync(parent, resource, featureId).get();
215     Assert.assertEquals(expectedResponse, actualResponse);
216 
217     List<String> actualRequests = mockService.getRequestPaths();
218     Assert.assertEquals(1, actualRequests.size());
219 
220     String apiClientHeaderKey =
221         mockService
222             .getRequestHeaders()
223             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
224             .iterator()
225             .next();
226     Assert.assertTrue(
227         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
228             .matcher(apiClientHeaderKey)
229             .matches());
230   }
231 
232   @Test
createFeatureExceptionTest()233   public void createFeatureExceptionTest() throws Exception {
234     ApiException exception =
235         ApiExceptionFactory.createException(
236             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
237     mockService.addException(exception);
238 
239     try {
240       String parent = "projects/project-5833/locations/location-5833";
241       Feature resource = Feature.newBuilder().build();
242       String featureId = "featureId-420503887";
243       client.createFeatureAsync(parent, resource, featureId).get();
244       Assert.fail("No exception raised");
245     } catch (ExecutionException e) {
246     }
247   }
248 
249   @Test
deleteFeatureTest()250   public void deleteFeatureTest() throws Exception {
251     Empty expectedResponse = Empty.newBuilder().build();
252     Operation resultOperation =
253         Operation.newBuilder()
254             .setName("deleteFeatureTest")
255             .setDone(true)
256             .setResponse(Any.pack(expectedResponse))
257             .build();
258     mockService.addResponse(resultOperation);
259 
260     String name = "projects/project-8365/locations/location-8365/features/feature-8365";
261 
262     client.deleteFeatureAsync(name).get();
263 
264     List<String> actualRequests = mockService.getRequestPaths();
265     Assert.assertEquals(1, actualRequests.size());
266 
267     String apiClientHeaderKey =
268         mockService
269             .getRequestHeaders()
270             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
271             .iterator()
272             .next();
273     Assert.assertTrue(
274         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
275             .matcher(apiClientHeaderKey)
276             .matches());
277   }
278 
279   @Test
deleteFeatureExceptionTest()280   public void deleteFeatureExceptionTest() throws Exception {
281     ApiException exception =
282         ApiExceptionFactory.createException(
283             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
284     mockService.addException(exception);
285 
286     try {
287       String name = "projects/project-8365/locations/location-8365/features/feature-8365";
288       client.deleteFeatureAsync(name).get();
289       Assert.fail("No exception raised");
290     } catch (ExecutionException e) {
291     }
292   }
293 
294   @Test
updateFeatureTest()295   public void updateFeatureTest() throws Exception {
296     Feature expectedResponse =
297         Feature.newBuilder()
298             .setName("name3373707")
299             .putAllLabels(new HashMap<String, String>())
300             .setResourceState(FeatureResourceState.newBuilder().build())
301             .setSpec(CommonFeatureSpec.newBuilder().build())
302             .putAllMembershipSpecs(new HashMap<String, MembershipFeatureSpec>())
303             .setState(CommonFeatureState.newBuilder().build())
304             .putAllMembershipStates(new HashMap<String, MembershipFeatureState>())
305             .setCreateTime(Timestamp.newBuilder().build())
306             .setUpdateTime(Timestamp.newBuilder().build())
307             .setDeleteTime(Timestamp.newBuilder().build())
308             .build();
309     Operation resultOperation =
310         Operation.newBuilder()
311             .setName("updateFeatureTest")
312             .setDone(true)
313             .setResponse(Any.pack(expectedResponse))
314             .build();
315     mockService.addResponse(resultOperation);
316 
317     String name = "projects/project-8365/locations/location-8365/features/feature-8365";
318     Feature resource = Feature.newBuilder().build();
319     FieldMask updateMask = FieldMask.newBuilder().build();
320 
321     Feature actualResponse = client.updateFeatureAsync(name, resource, updateMask).get();
322     Assert.assertEquals(expectedResponse, actualResponse);
323 
324     List<String> actualRequests = mockService.getRequestPaths();
325     Assert.assertEquals(1, actualRequests.size());
326 
327     String apiClientHeaderKey =
328         mockService
329             .getRequestHeaders()
330             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
331             .iterator()
332             .next();
333     Assert.assertTrue(
334         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
335             .matcher(apiClientHeaderKey)
336             .matches());
337   }
338 
339   @Test
updateFeatureExceptionTest()340   public void updateFeatureExceptionTest() throws Exception {
341     ApiException exception =
342         ApiExceptionFactory.createException(
343             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
344     mockService.addException(exception);
345 
346     try {
347       String name = "projects/project-8365/locations/location-8365/features/feature-8365";
348       Feature resource = Feature.newBuilder().build();
349       FieldMask updateMask = FieldMask.newBuilder().build();
350       client.updateFeatureAsync(name, resource, updateMask).get();
351       Assert.fail("No exception raised");
352     } catch (ExecutionException e) {
353     }
354   }
355 }
356