• 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.orchestration.airflow.service.v1;
18 
19 import static com.google.cloud.orchestration.airflow.service.v1.EnvironmentsClient.ListEnvironmentsPagedResponse;
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.orchestration.airflow.service.v1.stub.HttpJsonEnvironmentsStub;
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 EnvironmentsClientHttpJsonTest {
52   private static MockHttpService mockService;
53   private static EnvironmentsClient client;
54 
55   @BeforeClass
startStaticServer()56   public static void startStaticServer() throws IOException {
57     mockService =
58         new MockHttpService(
59             HttpJsonEnvironmentsStub.getMethodDescriptors(),
60             EnvironmentsSettings.getDefaultEndpoint());
61     EnvironmentsSettings settings =
62         EnvironmentsSettings.newHttpJsonBuilder()
63             .setTransportChannelProvider(
64                 EnvironmentsSettings.defaultHttpJsonTransportProviderBuilder()
65                     .setHttpTransport(mockService)
66                     .build())
67             .setCredentialsProvider(NoCredentialsProvider.create())
68             .build();
69     client = EnvironmentsClient.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
createEnvironmentTest()86   public void createEnvironmentTest() throws Exception {
87     Environment expectedResponse =
88         Environment.newBuilder()
89             .setName("name3373707")
90             .setConfig(EnvironmentConfig.newBuilder().build())
91             .setUuid("uuid3601339")
92             .setCreateTime(Timestamp.newBuilder().build())
93             .setUpdateTime(Timestamp.newBuilder().build())
94             .putAllLabels(new HashMap<String, String>())
95             .build();
96     Operation resultOperation =
97         Operation.newBuilder()
98             .setName("createEnvironmentTest")
99             .setDone(true)
100             .setResponse(Any.pack(expectedResponse))
101             .build();
102     mockService.addResponse(resultOperation);
103 
104     String parent = "projects/project-5833/locations/location-5833";
105     Environment environment = Environment.newBuilder().build();
106 
107     Environment actualResponse = client.createEnvironmentAsync(parent, environment).get();
108     Assert.assertEquals(expectedResponse, actualResponse);
109 
110     List<String> actualRequests = mockService.getRequestPaths();
111     Assert.assertEquals(1, actualRequests.size());
112 
113     String apiClientHeaderKey =
114         mockService
115             .getRequestHeaders()
116             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
117             .iterator()
118             .next();
119     Assert.assertTrue(
120         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
121             .matcher(apiClientHeaderKey)
122             .matches());
123   }
124 
125   @Test
createEnvironmentExceptionTest()126   public void createEnvironmentExceptionTest() throws Exception {
127     ApiException exception =
128         ApiExceptionFactory.createException(
129             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
130     mockService.addException(exception);
131 
132     try {
133       String parent = "projects/project-5833/locations/location-5833";
134       Environment environment = Environment.newBuilder().build();
135       client.createEnvironmentAsync(parent, environment).get();
136       Assert.fail("No exception raised");
137     } catch (ExecutionException e) {
138     }
139   }
140 
141   @Test
getEnvironmentTest()142   public void getEnvironmentTest() throws Exception {
143     Environment expectedResponse =
144         Environment.newBuilder()
145             .setName("name3373707")
146             .setConfig(EnvironmentConfig.newBuilder().build())
147             .setUuid("uuid3601339")
148             .setCreateTime(Timestamp.newBuilder().build())
149             .setUpdateTime(Timestamp.newBuilder().build())
150             .putAllLabels(new HashMap<String, String>())
151             .build();
152     mockService.addResponse(expectedResponse);
153 
154     String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
155 
156     Environment actualResponse = client.getEnvironment(name);
157     Assert.assertEquals(expectedResponse, actualResponse);
158 
159     List<String> actualRequests = mockService.getRequestPaths();
160     Assert.assertEquals(1, actualRequests.size());
161 
162     String apiClientHeaderKey =
163         mockService
164             .getRequestHeaders()
165             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
166             .iterator()
167             .next();
168     Assert.assertTrue(
169         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
170             .matcher(apiClientHeaderKey)
171             .matches());
172   }
173 
174   @Test
getEnvironmentExceptionTest()175   public void getEnvironmentExceptionTest() throws Exception {
176     ApiException exception =
177         ApiExceptionFactory.createException(
178             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
179     mockService.addException(exception);
180 
181     try {
182       String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
183       client.getEnvironment(name);
184       Assert.fail("No exception raised");
185     } catch (InvalidArgumentException e) {
186       // Expected exception.
187     }
188   }
189 
190   @Test
listEnvironmentsTest()191   public void listEnvironmentsTest() throws Exception {
192     Environment responsesElement = Environment.newBuilder().build();
193     ListEnvironmentsResponse expectedResponse =
194         ListEnvironmentsResponse.newBuilder()
195             .setNextPageToken("")
196             .addAllEnvironments(Arrays.asList(responsesElement))
197             .build();
198     mockService.addResponse(expectedResponse);
199 
200     String parent = "projects/project-5833/locations/location-5833";
201 
202     ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent);
203 
204     List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
205 
206     Assert.assertEquals(1, resources.size());
207     Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0));
208 
209     List<String> actualRequests = mockService.getRequestPaths();
210     Assert.assertEquals(1, actualRequests.size());
211 
212     String apiClientHeaderKey =
213         mockService
214             .getRequestHeaders()
215             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
216             .iterator()
217             .next();
218     Assert.assertTrue(
219         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
220             .matcher(apiClientHeaderKey)
221             .matches());
222   }
223 
224   @Test
listEnvironmentsExceptionTest()225   public void listEnvironmentsExceptionTest() throws Exception {
226     ApiException exception =
227         ApiExceptionFactory.createException(
228             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
229     mockService.addException(exception);
230 
231     try {
232       String parent = "projects/project-5833/locations/location-5833";
233       client.listEnvironments(parent);
234       Assert.fail("No exception raised");
235     } catch (InvalidArgumentException e) {
236       // Expected exception.
237     }
238   }
239 
240   @Test
updateEnvironmentTest()241   public void updateEnvironmentTest() throws Exception {
242     Environment expectedResponse =
243         Environment.newBuilder()
244             .setName("name3373707")
245             .setConfig(EnvironmentConfig.newBuilder().build())
246             .setUuid("uuid3601339")
247             .setCreateTime(Timestamp.newBuilder().build())
248             .setUpdateTime(Timestamp.newBuilder().build())
249             .putAllLabels(new HashMap<String, String>())
250             .build();
251     Operation resultOperation =
252         Operation.newBuilder()
253             .setName("updateEnvironmentTest")
254             .setDone(true)
255             .setResponse(Any.pack(expectedResponse))
256             .build();
257     mockService.addResponse(resultOperation);
258 
259     String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
260     Environment environment = Environment.newBuilder().build();
261     FieldMask updateMask = FieldMask.newBuilder().build();
262 
263     Environment actualResponse = client.updateEnvironmentAsync(name, environment, updateMask).get();
264     Assert.assertEquals(expectedResponse, actualResponse);
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
updateEnvironmentExceptionTest()282   public void updateEnvironmentExceptionTest() 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       String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
290       Environment environment = Environment.newBuilder().build();
291       FieldMask updateMask = FieldMask.newBuilder().build();
292       client.updateEnvironmentAsync(name, environment, updateMask).get();
293       Assert.fail("No exception raised");
294     } catch (ExecutionException e) {
295     }
296   }
297 
298   @Test
deleteEnvironmentTest()299   public void deleteEnvironmentTest() throws Exception {
300     Empty expectedResponse = Empty.newBuilder().build();
301     Operation resultOperation =
302         Operation.newBuilder()
303             .setName("deleteEnvironmentTest")
304             .setDone(true)
305             .setResponse(Any.pack(expectedResponse))
306             .build();
307     mockService.addResponse(resultOperation);
308 
309     String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
310 
311     client.deleteEnvironmentAsync(name).get();
312 
313     List<String> actualRequests = mockService.getRequestPaths();
314     Assert.assertEquals(1, actualRequests.size());
315 
316     String apiClientHeaderKey =
317         mockService
318             .getRequestHeaders()
319             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
320             .iterator()
321             .next();
322     Assert.assertTrue(
323         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
324             .matcher(apiClientHeaderKey)
325             .matches());
326   }
327 
328   @Test
deleteEnvironmentExceptionTest()329   public void deleteEnvironmentExceptionTest() throws Exception {
330     ApiException exception =
331         ApiExceptionFactory.createException(
332             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
333     mockService.addException(exception);
334 
335     try {
336       String name = "projects/project-7856/locations/location-7856/environments/environment-7856";
337       client.deleteEnvironmentAsync(name).get();
338       Assert.fail("No exception raised");
339     } catch (ExecutionException e) {
340     }
341   }
342 
343   @Test
saveSnapshotTest()344   public void saveSnapshotTest() throws Exception {
345     SaveSnapshotResponse expectedResponse =
346         SaveSnapshotResponse.newBuilder().setSnapshotPath("snapshotPath-931648503").build();
347     Operation resultOperation =
348         Operation.newBuilder()
349             .setName("saveSnapshotTest")
350             .setDone(true)
351             .setResponse(Any.pack(expectedResponse))
352             .build();
353     mockService.addResponse(resultOperation);
354 
355     SaveSnapshotRequest request =
356         SaveSnapshotRequest.newBuilder()
357             .setEnvironment(
358                 "projects/project-4088/locations/location-4088/environments/environment-4088")
359             .setSnapshotLocation("snapshotLocation1625609625")
360             .build();
361 
362     SaveSnapshotResponse actualResponse = client.saveSnapshotAsync(request).get();
363     Assert.assertEquals(expectedResponse, actualResponse);
364 
365     List<String> actualRequests = mockService.getRequestPaths();
366     Assert.assertEquals(1, actualRequests.size());
367 
368     String apiClientHeaderKey =
369         mockService
370             .getRequestHeaders()
371             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
372             .iterator()
373             .next();
374     Assert.assertTrue(
375         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
376             .matcher(apiClientHeaderKey)
377             .matches());
378   }
379 
380   @Test
saveSnapshotExceptionTest()381   public void saveSnapshotExceptionTest() throws Exception {
382     ApiException exception =
383         ApiExceptionFactory.createException(
384             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
385     mockService.addException(exception);
386 
387     try {
388       SaveSnapshotRequest request =
389           SaveSnapshotRequest.newBuilder()
390               .setEnvironment(
391                   "projects/project-4088/locations/location-4088/environments/environment-4088")
392               .setSnapshotLocation("snapshotLocation1625609625")
393               .build();
394       client.saveSnapshotAsync(request).get();
395       Assert.fail("No exception raised");
396     } catch (ExecutionException e) {
397     }
398   }
399 
400   @Test
loadSnapshotTest()401   public void loadSnapshotTest() throws Exception {
402     LoadSnapshotResponse expectedResponse = LoadSnapshotResponse.newBuilder().build();
403     Operation resultOperation =
404         Operation.newBuilder()
405             .setName("loadSnapshotTest")
406             .setDone(true)
407             .setResponse(Any.pack(expectedResponse))
408             .build();
409     mockService.addResponse(resultOperation);
410 
411     LoadSnapshotRequest request =
412         LoadSnapshotRequest.newBuilder()
413             .setEnvironment(
414                 "projects/project-4088/locations/location-4088/environments/environment-4088")
415             .setSnapshotPath("snapshotPath-931648503")
416             .setSkipPypiPackagesInstallation(true)
417             .setSkipEnvironmentVariablesSetting(true)
418             .setSkipAirflowOverridesSetting(true)
419             .setSkipGcsDataCopying(true)
420             .build();
421 
422     LoadSnapshotResponse actualResponse = client.loadSnapshotAsync(request).get();
423     Assert.assertEquals(expectedResponse, actualResponse);
424 
425     List<String> actualRequests = mockService.getRequestPaths();
426     Assert.assertEquals(1, actualRequests.size());
427 
428     String apiClientHeaderKey =
429         mockService
430             .getRequestHeaders()
431             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
432             .iterator()
433             .next();
434     Assert.assertTrue(
435         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
436             .matcher(apiClientHeaderKey)
437             .matches());
438   }
439 
440   @Test
loadSnapshotExceptionTest()441   public void loadSnapshotExceptionTest() throws Exception {
442     ApiException exception =
443         ApiExceptionFactory.createException(
444             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
445     mockService.addException(exception);
446 
447     try {
448       LoadSnapshotRequest request =
449           LoadSnapshotRequest.newBuilder()
450               .setEnvironment(
451                   "projects/project-4088/locations/location-4088/environments/environment-4088")
452               .setSnapshotPath("snapshotPath-931648503")
453               .setSkipPypiPackagesInstallation(true)
454               .setSkipEnvironmentVariablesSetting(true)
455               .setSkipAirflowOverridesSetting(true)
456               .setSkipGcsDataCopying(true)
457               .build();
458       client.loadSnapshotAsync(request).get();
459       Assert.fail("No exception raised");
460     } catch (ExecutionException e) {
461     }
462   }
463 }
464