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.JobControllerClient.ListJobsPagedResponse; 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.HttpJsonJobControllerStub; 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 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 JobControllerClientHttpJsonTest { 52 private static MockHttpService mockService; 53 private static JobControllerClient client; 54 55 @BeforeClass startStaticServer()56 public static void startStaticServer() throws IOException { 57 mockService = 58 new MockHttpService( 59 HttpJsonJobControllerStub.getMethodDescriptors(), 60 JobControllerSettings.getDefaultEndpoint()); 61 JobControllerSettings settings = 62 JobControllerSettings.newHttpJsonBuilder() 63 .setTransportChannelProvider( 64 JobControllerSettings.defaultHttpJsonTransportProviderBuilder() 65 .setHttpTransport(mockService) 66 .build()) 67 .setCredentialsProvider(NoCredentialsProvider.create()) 68 .build(); 69 client = JobControllerClient.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 submitJobTest()86 public void submitJobTest() throws Exception { 87 Job expectedResponse = 88 Job.newBuilder() 89 .setReference(JobReference.newBuilder().build()) 90 .setPlacement(JobPlacement.newBuilder().build()) 91 .setStatus(JobStatus.newBuilder().build()) 92 .addAllStatusHistory(new ArrayList<JobStatus>()) 93 .addAllYarnApplications(new ArrayList<YarnApplication>()) 94 .setDriverOutputResourceUri("driverOutputResourceUri794556277") 95 .setDriverControlFilesUri("driverControlFilesUri1491948202") 96 .putAllLabels(new HashMap<String, String>()) 97 .setScheduling(JobScheduling.newBuilder().build()) 98 .setJobUuid("jobUuid-1437868776") 99 .setDone(true) 100 .setDriverSchedulingConfig(DriverSchedulingConfig.newBuilder().build()) 101 .build(); 102 mockService.addResponse(expectedResponse); 103 104 String projectId = "projectId-1530"; 105 String region = "region-9622"; 106 Job job = Job.newBuilder().build(); 107 108 Job actualResponse = client.submitJob(projectId, region, job); 109 Assert.assertEquals(expectedResponse, actualResponse); 110 111 List<String> actualRequests = mockService.getRequestPaths(); 112 Assert.assertEquals(1, actualRequests.size()); 113 114 String apiClientHeaderKey = 115 mockService 116 .getRequestHeaders() 117 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 118 .iterator() 119 .next(); 120 Assert.assertTrue( 121 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 122 .matcher(apiClientHeaderKey) 123 .matches()); 124 } 125 126 @Test submitJobExceptionTest()127 public void submitJobExceptionTest() throws Exception { 128 ApiException exception = 129 ApiExceptionFactory.createException( 130 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 131 mockService.addException(exception); 132 133 try { 134 String projectId = "projectId-1530"; 135 String region = "region-9622"; 136 Job job = Job.newBuilder().build(); 137 client.submitJob(projectId, region, job); 138 Assert.fail("No exception raised"); 139 } catch (InvalidArgumentException e) { 140 // Expected exception. 141 } 142 } 143 144 @Test submitJobAsOperationTest()145 public void submitJobAsOperationTest() throws Exception { 146 Job expectedResponse = 147 Job.newBuilder() 148 .setReference(JobReference.newBuilder().build()) 149 .setPlacement(JobPlacement.newBuilder().build()) 150 .setStatus(JobStatus.newBuilder().build()) 151 .addAllStatusHistory(new ArrayList<JobStatus>()) 152 .addAllYarnApplications(new ArrayList<YarnApplication>()) 153 .setDriverOutputResourceUri("driverOutputResourceUri794556277") 154 .setDriverControlFilesUri("driverControlFilesUri1491948202") 155 .putAllLabels(new HashMap<String, String>()) 156 .setScheduling(JobScheduling.newBuilder().build()) 157 .setJobUuid("jobUuid-1437868776") 158 .setDone(true) 159 .setDriverSchedulingConfig(DriverSchedulingConfig.newBuilder().build()) 160 .build(); 161 Operation resultOperation = 162 Operation.newBuilder() 163 .setName("submitJobAsOperationTest") 164 .setDone(true) 165 .setResponse(Any.pack(expectedResponse)) 166 .build(); 167 mockService.addResponse(resultOperation); 168 169 String projectId = "projectId-1530"; 170 String region = "region-9622"; 171 Job job = Job.newBuilder().build(); 172 173 Job actualResponse = client.submitJobAsOperationAsync(projectId, region, job).get(); 174 Assert.assertEquals(expectedResponse, actualResponse); 175 176 List<String> actualRequests = mockService.getRequestPaths(); 177 Assert.assertEquals(1, actualRequests.size()); 178 179 String apiClientHeaderKey = 180 mockService 181 .getRequestHeaders() 182 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 183 .iterator() 184 .next(); 185 Assert.assertTrue( 186 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 187 .matcher(apiClientHeaderKey) 188 .matches()); 189 } 190 191 @Test submitJobAsOperationExceptionTest()192 public void submitJobAsOperationExceptionTest() throws Exception { 193 ApiException exception = 194 ApiExceptionFactory.createException( 195 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 196 mockService.addException(exception); 197 198 try { 199 String projectId = "projectId-1530"; 200 String region = "region-9622"; 201 Job job = Job.newBuilder().build(); 202 client.submitJobAsOperationAsync(projectId, region, job).get(); 203 Assert.fail("No exception raised"); 204 } catch (ExecutionException e) { 205 } 206 } 207 208 @Test getJobTest()209 public void getJobTest() throws Exception { 210 Job expectedResponse = 211 Job.newBuilder() 212 .setReference(JobReference.newBuilder().build()) 213 .setPlacement(JobPlacement.newBuilder().build()) 214 .setStatus(JobStatus.newBuilder().build()) 215 .addAllStatusHistory(new ArrayList<JobStatus>()) 216 .addAllYarnApplications(new ArrayList<YarnApplication>()) 217 .setDriverOutputResourceUri("driverOutputResourceUri794556277") 218 .setDriverControlFilesUri("driverControlFilesUri1491948202") 219 .putAllLabels(new HashMap<String, String>()) 220 .setScheduling(JobScheduling.newBuilder().build()) 221 .setJobUuid("jobUuid-1437868776") 222 .setDone(true) 223 .setDriverSchedulingConfig(DriverSchedulingConfig.newBuilder().build()) 224 .build(); 225 mockService.addResponse(expectedResponse); 226 227 String projectId = "projectId-1530"; 228 String region = "region-9622"; 229 String jobId = "jobId-3646"; 230 231 Job actualResponse = client.getJob(projectId, region, jobId); 232 Assert.assertEquals(expectedResponse, actualResponse); 233 234 List<String> actualRequests = mockService.getRequestPaths(); 235 Assert.assertEquals(1, actualRequests.size()); 236 237 String apiClientHeaderKey = 238 mockService 239 .getRequestHeaders() 240 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 241 .iterator() 242 .next(); 243 Assert.assertTrue( 244 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 245 .matcher(apiClientHeaderKey) 246 .matches()); 247 } 248 249 @Test getJobExceptionTest()250 public void getJobExceptionTest() throws Exception { 251 ApiException exception = 252 ApiExceptionFactory.createException( 253 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 254 mockService.addException(exception); 255 256 try { 257 String projectId = "projectId-1530"; 258 String region = "region-9622"; 259 String jobId = "jobId-3646"; 260 client.getJob(projectId, region, jobId); 261 Assert.fail("No exception raised"); 262 } catch (InvalidArgumentException e) { 263 // Expected exception. 264 } 265 } 266 267 @Test listJobsTest()268 public void listJobsTest() throws Exception { 269 Job responsesElement = Job.newBuilder().build(); 270 ListJobsResponse expectedResponse = 271 ListJobsResponse.newBuilder() 272 .setNextPageToken("") 273 .addAllJobs(Arrays.asList(responsesElement)) 274 .build(); 275 mockService.addResponse(expectedResponse); 276 277 String projectId = "projectId-1530"; 278 String region = "region-9622"; 279 280 ListJobsPagedResponse pagedListResponse = client.listJobs(projectId, region); 281 282 List<Job> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 283 284 Assert.assertEquals(1, resources.size()); 285 Assert.assertEquals(expectedResponse.getJobsList().get(0), resources.get(0)); 286 287 List<String> actualRequests = mockService.getRequestPaths(); 288 Assert.assertEquals(1, actualRequests.size()); 289 290 String apiClientHeaderKey = 291 mockService 292 .getRequestHeaders() 293 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 294 .iterator() 295 .next(); 296 Assert.assertTrue( 297 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 298 .matcher(apiClientHeaderKey) 299 .matches()); 300 } 301 302 @Test listJobsExceptionTest()303 public void listJobsExceptionTest() throws Exception { 304 ApiException exception = 305 ApiExceptionFactory.createException( 306 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 307 mockService.addException(exception); 308 309 try { 310 String projectId = "projectId-1530"; 311 String region = "region-9622"; 312 client.listJobs(projectId, region); 313 Assert.fail("No exception raised"); 314 } catch (InvalidArgumentException e) { 315 // Expected exception. 316 } 317 } 318 319 @Test listJobsTest2()320 public void listJobsTest2() throws Exception { 321 Job responsesElement = Job.newBuilder().build(); 322 ListJobsResponse expectedResponse = 323 ListJobsResponse.newBuilder() 324 .setNextPageToken("") 325 .addAllJobs(Arrays.asList(responsesElement)) 326 .build(); 327 mockService.addResponse(expectedResponse); 328 329 String projectId = "projectId-1530"; 330 String region = "region-9622"; 331 String filter = "filter-1274492040"; 332 333 ListJobsPagedResponse pagedListResponse = client.listJobs(projectId, region, filter); 334 335 List<Job> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 336 337 Assert.assertEquals(1, resources.size()); 338 Assert.assertEquals(expectedResponse.getJobsList().get(0), resources.get(0)); 339 340 List<String> actualRequests = mockService.getRequestPaths(); 341 Assert.assertEquals(1, actualRequests.size()); 342 343 String apiClientHeaderKey = 344 mockService 345 .getRequestHeaders() 346 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 347 .iterator() 348 .next(); 349 Assert.assertTrue( 350 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 351 .matcher(apiClientHeaderKey) 352 .matches()); 353 } 354 355 @Test listJobsExceptionTest2()356 public void listJobsExceptionTest2() throws Exception { 357 ApiException exception = 358 ApiExceptionFactory.createException( 359 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 360 mockService.addException(exception); 361 362 try { 363 String projectId = "projectId-1530"; 364 String region = "region-9622"; 365 String filter = "filter-1274492040"; 366 client.listJobs(projectId, region, filter); 367 Assert.fail("No exception raised"); 368 } catch (InvalidArgumentException e) { 369 // Expected exception. 370 } 371 } 372 373 @Test updateJobTest()374 public void updateJobTest() throws Exception { 375 Job expectedResponse = 376 Job.newBuilder() 377 .setReference(JobReference.newBuilder().build()) 378 .setPlacement(JobPlacement.newBuilder().build()) 379 .setStatus(JobStatus.newBuilder().build()) 380 .addAllStatusHistory(new ArrayList<JobStatus>()) 381 .addAllYarnApplications(new ArrayList<YarnApplication>()) 382 .setDriverOutputResourceUri("driverOutputResourceUri794556277") 383 .setDriverControlFilesUri("driverControlFilesUri1491948202") 384 .putAllLabels(new HashMap<String, String>()) 385 .setScheduling(JobScheduling.newBuilder().build()) 386 .setJobUuid("jobUuid-1437868776") 387 .setDone(true) 388 .setDriverSchedulingConfig(DriverSchedulingConfig.newBuilder().build()) 389 .build(); 390 mockService.addResponse(expectedResponse); 391 392 UpdateJobRequest request = 393 UpdateJobRequest.newBuilder() 394 .setProjectId("projectId-1530") 395 .setRegion("region-9622") 396 .setJobId("jobId-3646") 397 .setJob(Job.newBuilder().build()) 398 .setUpdateMask(FieldMask.newBuilder().build()) 399 .build(); 400 401 Job actualResponse = client.updateJob(request); 402 Assert.assertEquals(expectedResponse, actualResponse); 403 404 List<String> actualRequests = mockService.getRequestPaths(); 405 Assert.assertEquals(1, actualRequests.size()); 406 407 String apiClientHeaderKey = 408 mockService 409 .getRequestHeaders() 410 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 411 .iterator() 412 .next(); 413 Assert.assertTrue( 414 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 415 .matcher(apiClientHeaderKey) 416 .matches()); 417 } 418 419 @Test updateJobExceptionTest()420 public void updateJobExceptionTest() throws Exception { 421 ApiException exception = 422 ApiExceptionFactory.createException( 423 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 424 mockService.addException(exception); 425 426 try { 427 UpdateJobRequest request = 428 UpdateJobRequest.newBuilder() 429 .setProjectId("projectId-1530") 430 .setRegion("region-9622") 431 .setJobId("jobId-3646") 432 .setJob(Job.newBuilder().build()) 433 .setUpdateMask(FieldMask.newBuilder().build()) 434 .build(); 435 client.updateJob(request); 436 Assert.fail("No exception raised"); 437 } catch (InvalidArgumentException e) { 438 // Expected exception. 439 } 440 } 441 442 @Test cancelJobTest()443 public void cancelJobTest() throws Exception { 444 Job expectedResponse = 445 Job.newBuilder() 446 .setReference(JobReference.newBuilder().build()) 447 .setPlacement(JobPlacement.newBuilder().build()) 448 .setStatus(JobStatus.newBuilder().build()) 449 .addAllStatusHistory(new ArrayList<JobStatus>()) 450 .addAllYarnApplications(new ArrayList<YarnApplication>()) 451 .setDriverOutputResourceUri("driverOutputResourceUri794556277") 452 .setDriverControlFilesUri("driverControlFilesUri1491948202") 453 .putAllLabels(new HashMap<String, String>()) 454 .setScheduling(JobScheduling.newBuilder().build()) 455 .setJobUuid("jobUuid-1437868776") 456 .setDone(true) 457 .setDriverSchedulingConfig(DriverSchedulingConfig.newBuilder().build()) 458 .build(); 459 mockService.addResponse(expectedResponse); 460 461 String projectId = "projectId-1530"; 462 String region = "region-9622"; 463 String jobId = "jobId-3646"; 464 465 Job actualResponse = client.cancelJob(projectId, region, jobId); 466 Assert.assertEquals(expectedResponse, actualResponse); 467 468 List<String> actualRequests = mockService.getRequestPaths(); 469 Assert.assertEquals(1, actualRequests.size()); 470 471 String apiClientHeaderKey = 472 mockService 473 .getRequestHeaders() 474 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 475 .iterator() 476 .next(); 477 Assert.assertTrue( 478 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 479 .matcher(apiClientHeaderKey) 480 .matches()); 481 } 482 483 @Test cancelJobExceptionTest()484 public void cancelJobExceptionTest() throws Exception { 485 ApiException exception = 486 ApiExceptionFactory.createException( 487 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 488 mockService.addException(exception); 489 490 try { 491 String projectId = "projectId-1530"; 492 String region = "region-9622"; 493 String jobId = "jobId-3646"; 494 client.cancelJob(projectId, region, jobId); 495 Assert.fail("No exception raised"); 496 } catch (InvalidArgumentException e) { 497 // Expected exception. 498 } 499 } 500 501 @Test deleteJobTest()502 public void deleteJobTest() throws Exception { 503 Empty expectedResponse = Empty.newBuilder().build(); 504 mockService.addResponse(expectedResponse); 505 506 String projectId = "projectId-1530"; 507 String region = "region-9622"; 508 String jobId = "jobId-3646"; 509 510 client.deleteJob(projectId, region, jobId); 511 512 List<String> actualRequests = mockService.getRequestPaths(); 513 Assert.assertEquals(1, actualRequests.size()); 514 515 String apiClientHeaderKey = 516 mockService 517 .getRequestHeaders() 518 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 519 .iterator() 520 .next(); 521 Assert.assertTrue( 522 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 523 .matcher(apiClientHeaderKey) 524 .matches()); 525 } 526 527 @Test deleteJobExceptionTest()528 public void deleteJobExceptionTest() throws Exception { 529 ApiException exception = 530 ApiExceptionFactory.createException( 531 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 532 mockService.addException(exception); 533 534 try { 535 String projectId = "projectId-1530"; 536 String region = "region-9622"; 537 String jobId = "jobId-3646"; 538 client.deleteJob(projectId, region, jobId); 539 Assert.fail("No exception raised"); 540 } catch (InvalidArgumentException e) { 541 // Expected exception. 542 } 543 } 544 } 545