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.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.common.collect.Lists; 29 import com.google.protobuf.AbstractMessage; 30 import com.google.protobuf.Duration; 31 import com.google.protobuf.Timestamp; 32 import io.grpc.StatusRuntimeException; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.HashMap; 37 import java.util.List; 38 import java.util.UUID; 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 TasksClientTest { 49 private static MockLocations mockLocations; 50 private static MockServiceHelper mockServiceHelper; 51 private static MockTasks mockTasks; 52 private LocalChannelProvider channelProvider; 53 private TasksClient client; 54 55 @BeforeClass startStaticServer()56 public static void startStaticServer() { 57 mockTasks = new MockTasks(); 58 mockLocations = new MockLocations(); 59 mockServiceHelper = 60 new MockServiceHelper( 61 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockTasks, mockLocations)); 62 mockServiceHelper.start(); 63 } 64 65 @AfterClass stopServer()66 public static void stopServer() { 67 mockServiceHelper.stop(); 68 } 69 70 @Before setUp()71 public void setUp() throws IOException { 72 mockServiceHelper.reset(); 73 channelProvider = mockServiceHelper.createChannelProvider(); 74 TasksSettings settings = 75 TasksSettings.newBuilder() 76 .setTransportChannelProvider(channelProvider) 77 .setCredentialsProvider(NoCredentialsProvider.create()) 78 .build(); 79 client = TasksClient.create(settings); 80 } 81 82 @After tearDown()83 public void tearDown() throws Exception { 84 client.close(); 85 } 86 87 @Test getTaskTest()88 public void getTaskTest() throws Exception { 89 Task expectedResponse = 90 Task.newBuilder() 91 .setName( 92 TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]", "[TASK]").toString()) 93 .setUid("uid115792") 94 .setGeneration(305703192) 95 .putAllLabels(new HashMap<String, String>()) 96 .putAllAnnotations(new HashMap<String, String>()) 97 .setCreateTime(Timestamp.newBuilder().build()) 98 .setStartTime(Timestamp.newBuilder().build()) 99 .setCompletionTime(Timestamp.newBuilder().build()) 100 .setUpdateTime(Timestamp.newBuilder().build()) 101 .setDeleteTime(Timestamp.newBuilder().build()) 102 .setExpireTime(Timestamp.newBuilder().build()) 103 .setJob(JobName.of("[PROJECT]", "[LOCATION]", "[JOB]").toString()) 104 .setExecution( 105 ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]").toString()) 106 .addAllContainers(new ArrayList<Container>()) 107 .addAllVolumes(new ArrayList<Volume>()) 108 .setMaxRetries(1129288043) 109 .setTimeout(Duration.newBuilder().build()) 110 .setServiceAccount("serviceAccount1079137720") 111 .setExecutionEnvironment(ExecutionEnvironment.forNumber(0)) 112 .setReconciling(true) 113 .addAllConditions(new ArrayList<Condition>()) 114 .setObservedGeneration(900833007) 115 .setIndex(100346066) 116 .setRetried(1098377527) 117 .setLastAttemptResult(TaskAttemptResult.newBuilder().build()) 118 .setEncryptionKey("encryptionKey-36224036") 119 .setVpcAccess(VpcAccess.newBuilder().build()) 120 .setLogUri("logUri-1097354360") 121 .setSatisfiesPzs(true) 122 .setEtag("etag3123477") 123 .build(); 124 mockTasks.addResponse(expectedResponse); 125 126 TaskName name = TaskName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]", "[TASK]"); 127 128 Task actualResponse = client.getTask(name); 129 Assert.assertEquals(expectedResponse, actualResponse); 130 131 List<AbstractMessage> actualRequests = mockTasks.getRequests(); 132 Assert.assertEquals(1, actualRequests.size()); 133 GetTaskRequest actualRequest = ((GetTaskRequest) actualRequests.get(0)); 134 135 Assert.assertEquals(name.toString(), actualRequest.getName()); 136 Assert.assertTrue( 137 channelProvider.isHeaderSent( 138 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 139 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 140 } 141 142 @Test getTaskExceptionTest()143 public void getTaskExceptionTest() throws Exception { 144 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 145 mockTasks.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 mockTasks.addResponse(expectedResponse); 194 195 String name = "name3373707"; 196 197 Task actualResponse = client.getTask(name); 198 Assert.assertEquals(expectedResponse, actualResponse); 199 200 List<AbstractMessage> actualRequests = mockTasks.getRequests(); 201 Assert.assertEquals(1, actualRequests.size()); 202 GetTaskRequest actualRequest = ((GetTaskRequest) actualRequests.get(0)); 203 204 Assert.assertEquals(name, actualRequest.getName()); 205 Assert.assertTrue( 206 channelProvider.isHeaderSent( 207 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 208 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 209 } 210 211 @Test getTaskExceptionTest2()212 public void getTaskExceptionTest2() throws Exception { 213 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 214 mockTasks.addException(exception); 215 216 try { 217 String name = "name3373707"; 218 client.getTask(name); 219 Assert.fail("No exception raised"); 220 } catch (InvalidArgumentException e) { 221 // Expected exception. 222 } 223 } 224 225 @Test listTasksTest()226 public void listTasksTest() throws Exception { 227 Task responsesElement = Task.newBuilder().build(); 228 ListTasksResponse expectedResponse = 229 ListTasksResponse.newBuilder() 230 .setNextPageToken("") 231 .addAllTasks(Arrays.asList(responsesElement)) 232 .build(); 233 mockTasks.addResponse(expectedResponse); 234 235 ExecutionName parent = ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]"); 236 237 ListTasksPagedResponse pagedListResponse = client.listTasks(parent); 238 239 List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 240 241 Assert.assertEquals(1, resources.size()); 242 Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0)); 243 244 List<AbstractMessage> actualRequests = mockTasks.getRequests(); 245 Assert.assertEquals(1, actualRequests.size()); 246 ListTasksRequest actualRequest = ((ListTasksRequest) actualRequests.get(0)); 247 248 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 249 Assert.assertTrue( 250 channelProvider.isHeaderSent( 251 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 252 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 253 } 254 255 @Test listTasksExceptionTest()256 public void listTasksExceptionTest() throws Exception { 257 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 258 mockTasks.addException(exception); 259 260 try { 261 ExecutionName parent = ExecutionName.of("[PROJECT]", "[LOCATION]", "[JOB]", "[EXECUTION]"); 262 client.listTasks(parent); 263 Assert.fail("No exception raised"); 264 } catch (InvalidArgumentException e) { 265 // Expected exception. 266 } 267 } 268 269 @Test listTasksTest2()270 public void listTasksTest2() throws Exception { 271 Task responsesElement = Task.newBuilder().build(); 272 ListTasksResponse expectedResponse = 273 ListTasksResponse.newBuilder() 274 .setNextPageToken("") 275 .addAllTasks(Arrays.asList(responsesElement)) 276 .build(); 277 mockTasks.addResponse(expectedResponse); 278 279 String parent = "parent-995424086"; 280 281 ListTasksPagedResponse pagedListResponse = client.listTasks(parent); 282 283 List<Task> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 284 285 Assert.assertEquals(1, resources.size()); 286 Assert.assertEquals(expectedResponse.getTasksList().get(0), resources.get(0)); 287 288 List<AbstractMessage> actualRequests = mockTasks.getRequests(); 289 Assert.assertEquals(1, actualRequests.size()); 290 ListTasksRequest actualRequest = ((ListTasksRequest) actualRequests.get(0)); 291 292 Assert.assertEquals(parent, actualRequest.getParent()); 293 Assert.assertTrue( 294 channelProvider.isHeaderSent( 295 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 296 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 297 } 298 299 @Test listTasksExceptionTest2()300 public void listTasksExceptionTest2() throws Exception { 301 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 302 mockTasks.addException(exception); 303 304 try { 305 String parent = "parent-995424086"; 306 client.listTasks(parent); 307 Assert.fail("No exception raised"); 308 } catch (InvalidArgumentException e) { 309 // Expected exception. 310 } 311 } 312 } 313