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