• 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.api.serviceusage.v1;
18 
19 import static com.google.api.serviceusage.v1.ServiceUsageClient.ListServicesPagedResponse;
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.api.serviceusage.v1.stub.HttpJsonServiceUsageStub;
31 import com.google.common.collect.Lists;
32 import com.google.longrunning.Operation;
33 import com.google.protobuf.Any;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.List;
38 import java.util.concurrent.ExecutionException;
39 import javax.annotation.Generated;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Assert;
43 import org.junit.Before;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 
47 @Generated("by gapic-generator-java")
48 public class ServiceUsageClientHttpJsonTest {
49   private static MockHttpService mockService;
50   private static ServiceUsageClient client;
51 
52   @BeforeClass
startStaticServer()53   public static void startStaticServer() throws IOException {
54     mockService =
55         new MockHttpService(
56             HttpJsonServiceUsageStub.getMethodDescriptors(),
57             ServiceUsageSettings.getDefaultEndpoint());
58     ServiceUsageSettings settings =
59         ServiceUsageSettings.newHttpJsonBuilder()
60             .setTransportChannelProvider(
61                 ServiceUsageSettings.defaultHttpJsonTransportProviderBuilder()
62                     .setHttpTransport(mockService)
63                     .build())
64             .setCredentialsProvider(NoCredentialsProvider.create())
65             .build();
66     client = ServiceUsageClient.create(settings);
67   }
68 
69   @AfterClass
stopServer()70   public static void stopServer() {
71     client.close();
72   }
73 
74   @Before
setUp()75   public void setUp() {}
76 
77   @After
tearDown()78   public void tearDown() throws Exception {
79     mockService.reset();
80   }
81 
82   @Test
enableServiceTest()83   public void enableServiceTest() throws Exception {
84     EnableServiceResponse expectedResponse =
85         EnableServiceResponse.newBuilder().setService(Service.newBuilder().build()).build();
86     Operation resultOperation =
87         Operation.newBuilder()
88             .setName("enableServiceTest")
89             .setDone(true)
90             .setResponse(Any.pack(expectedResponse))
91             .build();
92     mockService.addResponse(resultOperation);
93 
94     EnableServiceRequest request =
95         EnableServiceRequest.newBuilder()
96             .setName("name-7410/name-7410/services/service-7410")
97             .build();
98 
99     EnableServiceResponse actualResponse = client.enableServiceAsync(request).get();
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
enableServiceExceptionTest()118   public void enableServiceExceptionTest() 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       EnableServiceRequest request =
126           EnableServiceRequest.newBuilder()
127               .setName("name-7410/name-7410/services/service-7410")
128               .build();
129       client.enableServiceAsync(request).get();
130       Assert.fail("No exception raised");
131     } catch (ExecutionException e) {
132     }
133   }
134 
135   @Test
disableServiceTest()136   public void disableServiceTest() throws Exception {
137     DisableServiceResponse expectedResponse =
138         DisableServiceResponse.newBuilder().setService(Service.newBuilder().build()).build();
139     Operation resultOperation =
140         Operation.newBuilder()
141             .setName("disableServiceTest")
142             .setDone(true)
143             .setResponse(Any.pack(expectedResponse))
144             .build();
145     mockService.addResponse(resultOperation);
146 
147     DisableServiceRequest request =
148         DisableServiceRequest.newBuilder()
149             .setName("name-7410/name-7410/services/service-7410")
150             .setDisableDependentServices(true)
151             .build();
152 
153     DisableServiceResponse actualResponse = client.disableServiceAsync(request).get();
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
disableServiceExceptionTest()172   public void disableServiceExceptionTest() 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       DisableServiceRequest request =
180           DisableServiceRequest.newBuilder()
181               .setName("name-7410/name-7410/services/service-7410")
182               .setDisableDependentServices(true)
183               .build();
184       client.disableServiceAsync(request).get();
185       Assert.fail("No exception raised");
186     } catch (ExecutionException e) {
187     }
188   }
189 
190   @Test
getServiceTest()191   public void getServiceTest() throws Exception {
192     Service expectedResponse =
193         Service.newBuilder()
194             .setName("name3373707")
195             .setParent("parent-995424086")
196             .setConfig(ServiceConfig.newBuilder().build())
197             .setState(State.forNumber(0))
198             .build();
199     mockService.addResponse(expectedResponse);
200 
201     GetServiceRequest request =
202         GetServiceRequest.newBuilder().setName("name-7410/name-7410/services/service-7410").build();
203 
204     Service actualResponse = client.getService(request);
205     Assert.assertEquals(expectedResponse, actualResponse);
206 
207     List<String> actualRequests = mockService.getRequestPaths();
208     Assert.assertEquals(1, actualRequests.size());
209 
210     String apiClientHeaderKey =
211         mockService
212             .getRequestHeaders()
213             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
214             .iterator()
215             .next();
216     Assert.assertTrue(
217         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
218             .matcher(apiClientHeaderKey)
219             .matches());
220   }
221 
222   @Test
getServiceExceptionTest()223   public void getServiceExceptionTest() throws Exception {
224     ApiException exception =
225         ApiExceptionFactory.createException(
226             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
227     mockService.addException(exception);
228 
229     try {
230       GetServiceRequest request =
231           GetServiceRequest.newBuilder()
232               .setName("name-7410/name-7410/services/service-7410")
233               .build();
234       client.getService(request);
235       Assert.fail("No exception raised");
236     } catch (InvalidArgumentException e) {
237       // Expected exception.
238     }
239   }
240 
241   @Test
listServicesTest()242   public void listServicesTest() throws Exception {
243     Service responsesElement = Service.newBuilder().build();
244     ListServicesResponse expectedResponse =
245         ListServicesResponse.newBuilder()
246             .setNextPageToken("")
247             .addAllServices(Arrays.asList(responsesElement))
248             .build();
249     mockService.addResponse(expectedResponse);
250 
251     ListServicesRequest request =
252         ListServicesRequest.newBuilder()
253             .setParent("parent-4715/parent-4715")
254             .setPageSize(883849137)
255             .setPageToken("pageToken873572522")
256             .setFilter("filter-1274492040")
257             .build();
258 
259     ListServicesPagedResponse pagedListResponse = client.listServices(request);
260 
261     List<Service> resources = Lists.newArrayList(pagedListResponse.iterateAll());
262 
263     Assert.assertEquals(1, resources.size());
264     Assert.assertEquals(expectedResponse.getServicesList().get(0), resources.get(0));
265 
266     List<String> actualRequests = mockService.getRequestPaths();
267     Assert.assertEquals(1, actualRequests.size());
268 
269     String apiClientHeaderKey =
270         mockService
271             .getRequestHeaders()
272             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
273             .iterator()
274             .next();
275     Assert.assertTrue(
276         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
277             .matcher(apiClientHeaderKey)
278             .matches());
279   }
280 
281   @Test
listServicesExceptionTest()282   public void listServicesExceptionTest() throws Exception {
283     ApiException exception =
284         ApiExceptionFactory.createException(
285             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
286     mockService.addException(exception);
287 
288     try {
289       ListServicesRequest request =
290           ListServicesRequest.newBuilder()
291               .setParent("parent-4715/parent-4715")
292               .setPageSize(883849137)
293               .setPageToken("pageToken873572522")
294               .setFilter("filter-1274492040")
295               .build();
296       client.listServices(request);
297       Assert.fail("No exception raised");
298     } catch (InvalidArgumentException e) {
299       // Expected exception.
300     }
301   }
302 
303   @Test
batchEnableServicesTest()304   public void batchEnableServicesTest() throws Exception {
305     BatchEnableServicesResponse expectedResponse =
306         BatchEnableServicesResponse.newBuilder()
307             .addAllServices(new ArrayList<Service>())
308             .addAllFailures(new ArrayList<BatchEnableServicesResponse.EnableFailure>())
309             .build();
310     Operation resultOperation =
311         Operation.newBuilder()
312             .setName("batchEnableServicesTest")
313             .setDone(true)
314             .setResponse(Any.pack(expectedResponse))
315             .build();
316     mockService.addResponse(resultOperation);
317 
318     BatchEnableServicesRequest request =
319         BatchEnableServicesRequest.newBuilder()
320             .setParent("parent-4715/parent-4715")
321             .addAllServiceIds(new ArrayList<String>())
322             .build();
323 
324     BatchEnableServicesResponse actualResponse = client.batchEnableServicesAsync(request).get();
325     Assert.assertEquals(expectedResponse, actualResponse);
326 
327     List<String> actualRequests = mockService.getRequestPaths();
328     Assert.assertEquals(1, actualRequests.size());
329 
330     String apiClientHeaderKey =
331         mockService
332             .getRequestHeaders()
333             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
334             .iterator()
335             .next();
336     Assert.assertTrue(
337         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
338             .matcher(apiClientHeaderKey)
339             .matches());
340   }
341 
342   @Test
batchEnableServicesExceptionTest()343   public void batchEnableServicesExceptionTest() throws Exception {
344     ApiException exception =
345         ApiExceptionFactory.createException(
346             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
347     mockService.addException(exception);
348 
349     try {
350       BatchEnableServicesRequest request =
351           BatchEnableServicesRequest.newBuilder()
352               .setParent("parent-4715/parent-4715")
353               .addAllServiceIds(new ArrayList<String>())
354               .build();
355       client.batchEnableServicesAsync(request).get();
356       Assert.fail("No exception raised");
357     } catch (ExecutionException e) {
358     }
359   }
360 
361   @Test
batchGetServicesTest()362   public void batchGetServicesTest() throws Exception {
363     BatchGetServicesResponse expectedResponse =
364         BatchGetServicesResponse.newBuilder().addAllServices(new ArrayList<Service>()).build();
365     mockService.addResponse(expectedResponse);
366 
367     BatchGetServicesRequest request =
368         BatchGetServicesRequest.newBuilder()
369             .setParent("parent-4715/parent-4715")
370             .addAllNames(new ArrayList<String>())
371             .build();
372 
373     BatchGetServicesResponse actualResponse = client.batchGetServices(request);
374     Assert.assertEquals(expectedResponse, actualResponse);
375 
376     List<String> actualRequests = mockService.getRequestPaths();
377     Assert.assertEquals(1, actualRequests.size());
378 
379     String apiClientHeaderKey =
380         mockService
381             .getRequestHeaders()
382             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
383             .iterator()
384             .next();
385     Assert.assertTrue(
386         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
387             .matcher(apiClientHeaderKey)
388             .matches());
389   }
390 
391   @Test
batchGetServicesExceptionTest()392   public void batchGetServicesExceptionTest() throws Exception {
393     ApiException exception =
394         ApiExceptionFactory.createException(
395             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
396     mockService.addException(exception);
397 
398     try {
399       BatchGetServicesRequest request =
400           BatchGetServicesRequest.newBuilder()
401               .setParent("parent-4715/parent-4715")
402               .addAllNames(new ArrayList<String>())
403               .build();
404       client.batchGetServices(request);
405       Assert.fail("No exception raised");
406     } catch (InvalidArgumentException e) {
407       // Expected exception.
408     }
409   }
410 }
411