• 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.run.v2;
18 
19 import static com.google.cloud.run.v2.TasksClient.ListTasksPagedResponse;
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.run.v2.stub.HttpJsonTasksStub;
31 import com.google.common.collect.Lists;
32 import com.google.protobuf.Duration;
33 import com.google.protobuf.Timestamp;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.List;
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 TasksClientHttpJsonTest {
49   private static MockHttpService mockService;
50   private static TasksClient client;
51 
52   @BeforeClass
startStaticServer()53   public static void startStaticServer() throws IOException {
54     mockService =
55         new MockHttpService(
56             HttpJsonTasksStub.getMethodDescriptors(), TasksSettings.getDefaultEndpoint());
57     TasksSettings settings =
58         TasksSettings.newHttpJsonBuilder()
59             .setTransportChannelProvider(
60                 TasksSettings.defaultHttpJsonTransportProviderBuilder()
61                     .setHttpTransport(mockService)
62                     .build())
63             .setCredentialsProvider(NoCredentialsProvider.create())
64             .build();
65     client = TasksClient.create(settings);
66   }
67 
68   @AfterClass
stopServer()69   public static void stopServer() {
70     client.close();
71   }
72 
73   @Before
setUp()74   public void setUp() {}
75 
76   @After
tearDown()77   public void tearDown() throws Exception {
78     mockService.reset();
79   }
80 
81   @Test
getTaskTest()82   public void getTaskTest() throws Exception {
83     Task expectedResponse =
84         Task.newBuilder()
85             .setName(
86                 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]", "[TASK]").toString())
87             .setUid("uid115792")
88             .setGeneration(305703192)
89             .putAllLabels(new HashMap<String, String>())
90             .putAllAnnotations(new HashMap<String, String>())
91             .setCreateTime(Timestamp.newBuilder().build())
92             .setStartTime(Timestamp.newBuilder().build())
93             .setCompletionTime(Timestamp.newBuilder().build())
94             .setUpdateTime(Timestamp.newBuilder().build())
95             .setDeleteTime(Timestamp.newBuilder().build())
96             .setExpireTime(Timestamp.newBuilder().build())
97             .setJob(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString())
98             .setExecution(
99                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]").toString())
100             .addAllContainers(new ArrayList<Container>())
101             .addAllVolumes(new ArrayList<Volume>())
102             .setMaxRetries(1129288043)
103             .setTimeout(Duration.newBuilder().build())
104             .setServiceAccount("serviceAccount1079137720")
105             .setExecutionEnvironment(ExecutionEnvironment.forNumber(0))
106             .setReconciling(true)
107             .addAllConditions(new ArrayList<Condition>())
108             .setObservedGeneration(900833007)
109             .setIndex(100346066)
110             .setRetried(1098377527)
111             .setLastAttemptResult(TaskAttemptResult.newBuilder().build())
112             .setEncryptionKey("encryptionKey-36224036")
113             .setVpcAccess(VpcAccess.newBuilder().build())
114             .setLogUri("logUri-1097354360")
115             .setSatisfiesPzs(true)
116             .setEtag("etag3123477")
117             .build();
118     mockService.addResponse(expectedResponse);
119 
120     TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]", "[TASK]");
121 
122     Task actualResponse = client.getTask(name);
123     Assert.assertEquals(expectedResponse, actualResponse);
124 
125     List<String> actualRequests = mockService.getRequestPaths();
126     Assert.assertEquals(1, actualRequests.size());
127 
128     String apiClientHeaderKey =
129         mockService
130             .getRequestHeaders()
131             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
132             .iterator()
133             .next();
134     Assert.assertTrue(
135         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
136             .matcher(apiClientHeaderKey)
137             .matches());
138   }
139 
140   @Test
getTaskExceptionTest()141   public void getTaskExceptionTest() throws Exception {
142     ApiException exception =
143         ApiExceptionFactory.createException(
144             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
145     mockService.addException(exception);
146 
147     try {
148       TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]", "[TASK]");
149       client.getTask(name);
150       Assert.fail("No exception raised");
151     } catch (InvalidArgumentException e) {
152       // Expected exception.
153     }
154   }
155 
156   @Test
getTaskTest2()157   public void getTaskTest2() throws Exception {
158     Task expectedResponse =
159         Task.newBuilder()
160             .setName(
161                 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]", "[TASK]").toString())
162             .setUid("uid115792")
163             .setGeneration(305703192)
164             .putAllLabels(new HashMap<String, String>())
165             .putAllAnnotations(new HashMap<String, String>())
166             .setCreateTime(Timestamp.newBuilder().build())
167             .setStartTime(Timestamp.newBuilder().build())
168             .setCompletionTime(Timestamp.newBuilder().build())
169             .setUpdateTime(Timestamp.newBuilder().build())
170             .setDeleteTime(Timestamp.newBuilder().build())
171             .setExpireTime(Timestamp.newBuilder().build())
172             .setJob(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString())
173             .setExecution(
174                 ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]").toString())
175             .addAllContainers(new ArrayList<Container>())
176             .addAllVolumes(new ArrayList<Volume>())
177             .setMaxRetries(1129288043)
178             .setTimeout(Duration.newBuilder().build())
179             .setServiceAccount("serviceAccount1079137720")
180             .setExecutionEnvironment(ExecutionEnvironment.forNumber(0))
181             .setReconciling(true)
182             .addAllConditions(new ArrayList<Condition>())
183             .setObservedGeneration(900833007)
184             .setIndex(100346066)
185             .setRetried(1098377527)
186             .setLastAttemptResult(TaskAttemptResult.newBuilder().build())
187             .setEncryptionKey("encryptionKey-36224036")
188             .setVpcAccess(VpcAccess.newBuilder().build())
189             .setLogUri("logUri-1097354360")
190             .setSatisfiesPzs(true)
191             .setEtag("etag3123477")
192             .build();
193     mockService.addResponse(expectedResponse);
194 
195     String name =
196         "projects/project-119/locations/location-119/jobs/job-119/executions/execution-119/tasks/task-119";
197 
198     Task actualResponse = client.getTask(name);
199     Assert.assertEquals(expectedResponse, actualResponse);
200 
201     List<String> actualRequests = mockService.getRequestPaths();
202     Assert.assertEquals(1, actualRequests.size());
203 
204     String apiClientHeaderKey =
205         mockService
206             .getRequestHeaders()
207             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
208             .iterator()
209             .next();
210     Assert.assertTrue(
211         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
212             .matcher(apiClientHeaderKey)
213             .matches());
214   }
215 
216   @Test
getTaskExceptionTest2()217   public void getTaskExceptionTest2() throws Exception {
218     ApiException exception =
219         ApiExceptionFactory.createException(
220             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
221     mockService.addException(exception);
222 
223     try {
224       String name =
225           "projects/project-119/locations/location-119/jobs/job-119/executions/execution-119/tasks/task-119";
226       client.getTask(name);
227       Assert.fail("No exception raised");
228     } catch (InvalidArgumentException e) {
229       // Expected exception.
230     }
231   }
232 
233   @Test
listTasksTest()234   public void listTasksTest() throws Exception {
235     Task responsesElement = Task.newBuilder().build();
236     ListTasksResponse expectedResponse =
237         ListTasksResponse.newBuilder()
238             .setNextPageToken("")
239             .addAllTasks(Arrays.asList(responsesElement))
240             .build();
241     mockService.addResponse(expectedResponse);
242 
243     ExecutionName parent = ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]");
244 
245     ListTasksPagedResponse pagedListResponse = client.listTasks(parent);
246 
247     List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll());
248 
249     Assert.assertEquals(1, resources.size());
250     Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0));
251 
252     List<String> actualRequests = mockService.getRequestPaths();
253     Assert.assertEquals(1, actualRequests.size());
254 
255     String apiClientHeaderKey =
256         mockService
257             .getRequestHeaders()
258             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
259             .iterator()
260             .next();
261     Assert.assertTrue(
262         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
263             .matcher(apiClientHeaderKey)
264             .matches());
265   }
266 
267   @Test
listTasksExceptionTest()268   public void listTasksExceptionTest() throws Exception {
269     ApiException exception =
270         ApiExceptionFactory.createException(
271             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
272     mockService.addException(exception);
273 
274     try {
275       ExecutionName parent = ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]");
276       client.listTasks(parent);
277       Assert.fail("No exception raised");
278     } catch (InvalidArgumentException e) {
279       // Expected exception.
280     }
281   }
282 
283   @Test
listTasksTest2()284   public void listTasksTest2() throws Exception {
285     Task responsesElement = Task.newBuilder().build();
286     ListTasksResponse expectedResponse =
287         ListTasksResponse.newBuilder()
288             .setNextPageToken("")
289             .addAllTasks(Arrays.asList(responsesElement))
290             .build();
291     mockService.addResponse(expectedResponse);
292 
293     String parent =
294         "projects/project-1154/locations/location-1154/jobs/job-1154/executions/execution-1154";
295 
296     ListTasksPagedResponse pagedListResponse = client.listTasks(parent);
297 
298     List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll());
299 
300     Assert.assertEquals(1, resources.size());
301     Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0));
302 
303     List<String> actualRequests = mockService.getRequestPaths();
304     Assert.assertEquals(1, actualRequests.size());
305 
306     String apiClientHeaderKey =
307         mockService
308             .getRequestHeaders()
309             .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey())
310             .iterator()
311             .next();
312     Assert.assertTrue(
313         GaxHttpJsonProperties.getDefaultApiClientHeaderPattern()
314             .matcher(apiClientHeaderKey)
315             .matches());
316   }
317 
318   @Test
listTasksExceptionTest2()319   public void listTasksExceptionTest2() throws Exception {
320     ApiException exception =
321         ApiExceptionFactory.createException(
322             new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false);
323     mockService.addException(exception);
324 
325     try {
326       String parent =
327           "projects/project-1154/locations/location-1154/jobs/job-1154/executions/execution-1154";
328       client.listTasks(parent);
329       Assert.fail("No exception raised");
330     } catch (InvalidArgumentException e) {
331       // Expected exception.
332     }
333   }
334 }
335