• 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.grpc.GaxGrpcProperties;
23 import com.google.api.gax.grpc.testing.LocalChannelProvider;
24 import com.google.api.gax.grpc.testing.MockGrpcService;
25 import com.google.api.gax.grpc.testing.MockServiceHelper;
26 import com.google.api.gax.rpc.ApiClientHeaderProvider;
27 import com.google.api.gax.rpc.InvalidArgumentException;
28 import com.google.api.gax.rpc.StatusCode;
29 import com.google.common.collect.Lists;
30 import com.google.longrunning.Operation;
31 import com.google.protobuf.AbstractMessage;
32 import com.google.protobuf.Any;
33 import com.google.protobuf.Empty;
34 import com.google.protobuf.FieldMask;
35 import com.google.protobuf.Timestamp;
36 import io.grpc.StatusRuntimeException;
37 import java.io.IOException;
38 import java.util.Arrays;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.UUID;
42 import java.util.concurrent.ExecutionException;
43 import javax.annotation.Generated;
44 import org.junit.After;
45 import org.junit.AfterClass;
46 import org.junit.Assert;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 
51 @Generated("by gapic-generator-java")
52 public class EnvironmentsClientTest {
53   private static MockEnvironments mockEnvironments;
54   private static MockServiceHelper mockServiceHelper;
55   private LocalChannelProvider channelProvider;
56   private EnvironmentsClient client;
57 
58   @BeforeClass
startStaticServer()59   public static void startStaticServer() {
60     mockEnvironments = new MockEnvironments();
61     mockServiceHelper =
62         new MockServiceHelper(
63             UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockEnvironments));
64     mockServiceHelper.start();
65   }
66 
67   @AfterClass
stopServer()68   public static void stopServer() {
69     mockServiceHelper.stop();
70   }
71 
72   @Before
setUp()73   public void setUp() throws IOException {
74     mockServiceHelper.reset();
75     channelProvider = mockServiceHelper.createChannelProvider();
76     EnvironmentsSettings settings =
77         EnvironmentsSettings.newBuilder()
78             .setTransportChannelProvider(channelProvider)
79             .setCredentialsProvider(NoCredentialsProvider.create())
80             .build();
81     client = EnvironmentsClient.create(settings);
82   }
83 
84   @After
tearDown()85   public void tearDown() throws Exception {
86     client.close();
87   }
88 
89   @Test
createEnvironmentTest()90   public void createEnvironmentTest() throws Exception {
91     Environment expectedResponse =
92         Environment.newBuilder()
93             .setName("name3373707")
94             .setConfig(EnvironmentConfig.newBuilder().build())
95             .setUuid("uuid3601339")
96             .setCreateTime(Timestamp.newBuilder().build())
97             .setUpdateTime(Timestamp.newBuilder().build())
98             .putAllLabels(new HashMap<String, String>())
99             .build();
100     Operation resultOperation =
101         Operation.newBuilder()
102             .setName("createEnvironmentTest")
103             .setDone(true)
104             .setResponse(Any.pack(expectedResponse))
105             .build();
106     mockEnvironments.addResponse(resultOperation);
107 
108     String parent = "parent-995424086";
109     Environment environment = Environment.newBuilder().build();
110 
111     Environment actualResponse = client.createEnvironmentAsync(parent, environment).get();
112     Assert.assertEquals(expectedResponse, actualResponse);
113 
114     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
115     Assert.assertEquals(1, actualRequests.size());
116     CreateEnvironmentRequest actualRequest = ((CreateEnvironmentRequest) actualRequests.get(0));
117 
118     Assert.assertEquals(parent, actualRequest.getParent());
119     Assert.assertEquals(environment, actualRequest.getEnvironment());
120     Assert.assertTrue(
121         channelProvider.isHeaderSent(
122             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
123             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
124   }
125 
126   @Test
createEnvironmentExceptionTest()127   public void createEnvironmentExceptionTest() throws Exception {
128     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
129     mockEnvironments.addException(exception);
130 
131     try {
132       String parent = "parent-995424086";
133       Environment environment = Environment.newBuilder().build();
134       client.createEnvironmentAsync(parent, environment).get();
135       Assert.fail("No exception raised");
136     } catch (ExecutionException e) {
137       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
138       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
139       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
140     }
141   }
142 
143   @Test
getEnvironmentTest()144   public void getEnvironmentTest() throws Exception {
145     Environment expectedResponse =
146         Environment.newBuilder()
147             .setName("name3373707")
148             .setConfig(EnvironmentConfig.newBuilder().build())
149             .setUuid("uuid3601339")
150             .setCreateTime(Timestamp.newBuilder().build())
151             .setUpdateTime(Timestamp.newBuilder().build())
152             .putAllLabels(new HashMap<String, String>())
153             .build();
154     mockEnvironments.addResponse(expectedResponse);
155 
156     String name = "name3373707";
157 
158     Environment actualResponse = client.getEnvironment(name);
159     Assert.assertEquals(expectedResponse, actualResponse);
160 
161     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
162     Assert.assertEquals(1, actualRequests.size());
163     GetEnvironmentRequest actualRequest = ((GetEnvironmentRequest) actualRequests.get(0));
164 
165     Assert.assertEquals(name, actualRequest.getName());
166     Assert.assertTrue(
167         channelProvider.isHeaderSent(
168             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
169             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
170   }
171 
172   @Test
getEnvironmentExceptionTest()173   public void getEnvironmentExceptionTest() throws Exception {
174     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
175     mockEnvironments.addException(exception);
176 
177     try {
178       String name = "name3373707";
179       client.getEnvironment(name);
180       Assert.fail("No exception raised");
181     } catch (InvalidArgumentException e) {
182       // Expected exception.
183     }
184   }
185 
186   @Test
listEnvironmentsTest()187   public void listEnvironmentsTest() throws Exception {
188     Environment responsesElement = Environment.newBuilder().build();
189     ListEnvironmentsResponse expectedResponse =
190         ListEnvironmentsResponse.newBuilder()
191             .setNextPageToken("")
192             .addAllEnvironments(Arrays.asList(responsesElement))
193             .build();
194     mockEnvironments.addResponse(expectedResponse);
195 
196     String parent = "parent-995424086";
197 
198     ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent);
199 
200     List<Environment> resources = Lists.newArrayList(pagedListResponse.iterateAll());
201 
202     Assert.assertEquals(1, resources.size());
203     Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0));
204 
205     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
206     Assert.assertEquals(1, actualRequests.size());
207     ListEnvironmentsRequest actualRequest = ((ListEnvironmentsRequest) actualRequests.get(0));
208 
209     Assert.assertEquals(parent, actualRequest.getParent());
210     Assert.assertTrue(
211         channelProvider.isHeaderSent(
212             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
213             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
214   }
215 
216   @Test
listEnvironmentsExceptionTest()217   public void listEnvironmentsExceptionTest() throws Exception {
218     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
219     mockEnvironments.addException(exception);
220 
221     try {
222       String parent = "parent-995424086";
223       client.listEnvironments(parent);
224       Assert.fail("No exception raised");
225     } catch (InvalidArgumentException e) {
226       // Expected exception.
227     }
228   }
229 
230   @Test
updateEnvironmentTest()231   public void updateEnvironmentTest() throws Exception {
232     Environment expectedResponse =
233         Environment.newBuilder()
234             .setName("name3373707")
235             .setConfig(EnvironmentConfig.newBuilder().build())
236             .setUuid("uuid3601339")
237             .setCreateTime(Timestamp.newBuilder().build())
238             .setUpdateTime(Timestamp.newBuilder().build())
239             .putAllLabels(new HashMap<String, String>())
240             .build();
241     Operation resultOperation =
242         Operation.newBuilder()
243             .setName("updateEnvironmentTest")
244             .setDone(true)
245             .setResponse(Any.pack(expectedResponse))
246             .build();
247     mockEnvironments.addResponse(resultOperation);
248 
249     String name = "name3373707";
250     Environment environment = Environment.newBuilder().build();
251     FieldMask updateMask = FieldMask.newBuilder().build();
252 
253     Environment actualResponse = client.updateEnvironmentAsync(name, environment, updateMask).get();
254     Assert.assertEquals(expectedResponse, actualResponse);
255 
256     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
257     Assert.assertEquals(1, actualRequests.size());
258     UpdateEnvironmentRequest actualRequest = ((UpdateEnvironmentRequest) actualRequests.get(0));
259 
260     Assert.assertEquals(name, actualRequest.getName());
261     Assert.assertEquals(environment, actualRequest.getEnvironment());
262     Assert.assertEquals(updateMask, actualRequest.getUpdateMask());
263     Assert.assertTrue(
264         channelProvider.isHeaderSent(
265             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
266             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
267   }
268 
269   @Test
updateEnvironmentExceptionTest()270   public void updateEnvironmentExceptionTest() throws Exception {
271     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
272     mockEnvironments.addException(exception);
273 
274     try {
275       String name = "name3373707";
276       Environment environment = Environment.newBuilder().build();
277       FieldMask updateMask = FieldMask.newBuilder().build();
278       client.updateEnvironmentAsync(name, environment, updateMask).get();
279       Assert.fail("No exception raised");
280     } catch (ExecutionException e) {
281       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
282       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
283       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
284     }
285   }
286 
287   @Test
deleteEnvironmentTest()288   public void deleteEnvironmentTest() throws Exception {
289     Empty expectedResponse = Empty.newBuilder().build();
290     Operation resultOperation =
291         Operation.newBuilder()
292             .setName("deleteEnvironmentTest")
293             .setDone(true)
294             .setResponse(Any.pack(expectedResponse))
295             .build();
296     mockEnvironments.addResponse(resultOperation);
297 
298     String name = "name3373707";
299 
300     client.deleteEnvironmentAsync(name).get();
301 
302     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
303     Assert.assertEquals(1, actualRequests.size());
304     DeleteEnvironmentRequest actualRequest = ((DeleteEnvironmentRequest) actualRequests.get(0));
305 
306     Assert.assertEquals(name, actualRequest.getName());
307     Assert.assertTrue(
308         channelProvider.isHeaderSent(
309             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
310             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
311   }
312 
313   @Test
deleteEnvironmentExceptionTest()314   public void deleteEnvironmentExceptionTest() throws Exception {
315     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
316     mockEnvironments.addException(exception);
317 
318     try {
319       String name = "name3373707";
320       client.deleteEnvironmentAsync(name).get();
321       Assert.fail("No exception raised");
322     } catch (ExecutionException e) {
323       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
324       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
325       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
326     }
327   }
328 
329   @Test
saveSnapshotTest()330   public void saveSnapshotTest() throws Exception {
331     SaveSnapshotResponse expectedResponse =
332         SaveSnapshotResponse.newBuilder().setSnapshotPath("snapshotPath-931648503").build();
333     Operation resultOperation =
334         Operation.newBuilder()
335             .setName("saveSnapshotTest")
336             .setDone(true)
337             .setResponse(Any.pack(expectedResponse))
338             .build();
339     mockEnvironments.addResponse(resultOperation);
340 
341     SaveSnapshotRequest request =
342         SaveSnapshotRequest.newBuilder()
343             .setEnvironment("environment-85904877")
344             .setSnapshotLocation("snapshotLocation1625609625")
345             .build();
346 
347     SaveSnapshotResponse actualResponse = client.saveSnapshotAsync(request).get();
348     Assert.assertEquals(expectedResponse, actualResponse);
349 
350     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
351     Assert.assertEquals(1, actualRequests.size());
352     SaveSnapshotRequest actualRequest = ((SaveSnapshotRequest) actualRequests.get(0));
353 
354     Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment());
355     Assert.assertEquals(request.getSnapshotLocation(), actualRequest.getSnapshotLocation());
356     Assert.assertTrue(
357         channelProvider.isHeaderSent(
358             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
359             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
360   }
361 
362   @Test
saveSnapshotExceptionTest()363   public void saveSnapshotExceptionTest() throws Exception {
364     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
365     mockEnvironments.addException(exception);
366 
367     try {
368       SaveSnapshotRequest request =
369           SaveSnapshotRequest.newBuilder()
370               .setEnvironment("environment-85904877")
371               .setSnapshotLocation("snapshotLocation1625609625")
372               .build();
373       client.saveSnapshotAsync(request).get();
374       Assert.fail("No exception raised");
375     } catch (ExecutionException e) {
376       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
377       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
378       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
379     }
380   }
381 
382   @Test
loadSnapshotTest()383   public void loadSnapshotTest() throws Exception {
384     LoadSnapshotResponse expectedResponse = LoadSnapshotResponse.newBuilder().build();
385     Operation resultOperation =
386         Operation.newBuilder()
387             .setName("loadSnapshotTest")
388             .setDone(true)
389             .setResponse(Any.pack(expectedResponse))
390             .build();
391     mockEnvironments.addResponse(resultOperation);
392 
393     LoadSnapshotRequest request =
394         LoadSnapshotRequest.newBuilder()
395             .setEnvironment("environment-85904877")
396             .setSnapshotPath("snapshotPath-931648503")
397             .setSkipPypiPackagesInstallation(true)
398             .setSkipEnvironmentVariablesSetting(true)
399             .setSkipAirflowOverridesSetting(true)
400             .setSkipGcsDataCopying(true)
401             .build();
402 
403     LoadSnapshotResponse actualResponse = client.loadSnapshotAsync(request).get();
404     Assert.assertEquals(expectedResponse, actualResponse);
405 
406     List<AbstractMessage> actualRequests = mockEnvironments.getRequests();
407     Assert.assertEquals(1, actualRequests.size());
408     LoadSnapshotRequest actualRequest = ((LoadSnapshotRequest) actualRequests.get(0));
409 
410     Assert.assertEquals(request.getEnvironment(), actualRequest.getEnvironment());
411     Assert.assertEquals(request.getSnapshotPath(), actualRequest.getSnapshotPath());
412     Assert.assertEquals(
413         request.getSkipPypiPackagesInstallation(), actualRequest.getSkipPypiPackagesInstallation());
414     Assert.assertEquals(
415         request.getSkipEnvironmentVariablesSetting(),
416         actualRequest.getSkipEnvironmentVariablesSetting());
417     Assert.assertEquals(
418         request.getSkipAirflowOverridesSetting(), actualRequest.getSkipAirflowOverridesSetting());
419     Assert.assertEquals(request.getSkipGcsDataCopying(), actualRequest.getSkipGcsDataCopying());
420     Assert.assertTrue(
421         channelProvider.isHeaderSent(
422             ApiClientHeaderProvider.getDefaultApiClientHeaderKey(),
423             GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
424   }
425 
426   @Test
loadSnapshotExceptionTest()427   public void loadSnapshotExceptionTest() throws Exception {
428     StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
429     mockEnvironments.addException(exception);
430 
431     try {
432       LoadSnapshotRequest request =
433           LoadSnapshotRequest.newBuilder()
434               .setEnvironment("environment-85904877")
435               .setSnapshotPath("snapshotPath-931648503")
436               .setSkipPypiPackagesInstallation(true)
437               .setSkipEnvironmentVariablesSetting(true)
438               .setSkipAirflowOverridesSetting(true)
439               .setSkipGcsDataCopying(true)
440               .build();
441       client.loadSnapshotAsync(request).get();
442       Assert.fail("No exception raised");
443     } catch (ExecutionException e) {
444       Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass());
445       InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause());
446       Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode());
447     }
448   }
449 }
450