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.ClusterControllerClient.ListClustersPagedResponse; 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.HttpJsonClusterControllerStub; 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 ClusterControllerClientHttpJsonTest { 52 private static MockHttpService mockService; 53 private static ClusterControllerClient client; 54 55 @BeforeClass startStaticServer()56 public static void startStaticServer() throws IOException { 57 mockService = 58 new MockHttpService( 59 HttpJsonClusterControllerStub.getMethodDescriptors(), 60 ClusterControllerSettings.getDefaultEndpoint()); 61 ClusterControllerSettings settings = 62 ClusterControllerSettings.newHttpJsonBuilder() 63 .setTransportChannelProvider( 64 ClusterControllerSettings.defaultHttpJsonTransportProviderBuilder() 65 .setHttpTransport(mockService) 66 .build()) 67 .setCredentialsProvider(NoCredentialsProvider.create()) 68 .build(); 69 client = ClusterControllerClient.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 createClusterTest()86 public void createClusterTest() throws Exception { 87 Cluster expectedResponse = 88 Cluster.newBuilder() 89 .setProjectId("projectId-894832108") 90 .setClusterName("clusterName-1141738587") 91 .setConfig(ClusterConfig.newBuilder().build()) 92 .setVirtualClusterConfig(VirtualClusterConfig.newBuilder().build()) 93 .putAllLabels(new HashMap<String, String>()) 94 .setStatus(ClusterStatus.newBuilder().build()) 95 .addAllStatusHistory(new ArrayList<ClusterStatus>()) 96 .setClusterUuid("clusterUuid-1141510955") 97 .setMetrics(ClusterMetrics.newBuilder().build()) 98 .build(); 99 Operation resultOperation = 100 Operation.newBuilder() 101 .setName("createClusterTest") 102 .setDone(true) 103 .setResponse(Any.pack(expectedResponse)) 104 .build(); 105 mockService.addResponse(resultOperation); 106 107 String projectId = "projectId-1530"; 108 String region = "region-9622"; 109 Cluster cluster = Cluster.newBuilder().build(); 110 111 Cluster actualResponse = client.createClusterAsync(projectId, region, cluster).get(); 112 Assert.assertEquals(expectedResponse, actualResponse); 113 114 List<String> actualRequests = mockService.getRequestPaths(); 115 Assert.assertEquals(1, actualRequests.size()); 116 117 String apiClientHeaderKey = 118 mockService 119 .getRequestHeaders() 120 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 121 .iterator() 122 .next(); 123 Assert.assertTrue( 124 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 125 .matcher(apiClientHeaderKey) 126 .matches()); 127 } 128 129 @Test createClusterExceptionTest()130 public void createClusterExceptionTest() throws Exception { 131 ApiException exception = 132 ApiExceptionFactory.createException( 133 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 134 mockService.addException(exception); 135 136 try { 137 String projectId = "projectId-1530"; 138 String region = "region-9622"; 139 Cluster cluster = Cluster.newBuilder().build(); 140 client.createClusterAsync(projectId, region, cluster).get(); 141 Assert.fail("No exception raised"); 142 } catch (ExecutionException e) { 143 } 144 } 145 146 @Test updateClusterTest()147 public void updateClusterTest() throws Exception { 148 Cluster expectedResponse = 149 Cluster.newBuilder() 150 .setProjectId("projectId-894832108") 151 .setClusterName("clusterName-1141738587") 152 .setConfig(ClusterConfig.newBuilder().build()) 153 .setVirtualClusterConfig(VirtualClusterConfig.newBuilder().build()) 154 .putAllLabels(new HashMap<String, String>()) 155 .setStatus(ClusterStatus.newBuilder().build()) 156 .addAllStatusHistory(new ArrayList<ClusterStatus>()) 157 .setClusterUuid("clusterUuid-1141510955") 158 .setMetrics(ClusterMetrics.newBuilder().build()) 159 .build(); 160 Operation resultOperation = 161 Operation.newBuilder() 162 .setName("updateClusterTest") 163 .setDone(true) 164 .setResponse(Any.pack(expectedResponse)) 165 .build(); 166 mockService.addResponse(resultOperation); 167 168 String projectId = "projectId-1530"; 169 String region = "region-9622"; 170 String clusterName = "clusterName-7787"; 171 Cluster cluster = Cluster.newBuilder().build(); 172 FieldMask updateMask = FieldMask.newBuilder().build(); 173 174 Cluster actualResponse = 175 client.updateClusterAsync(projectId, region, clusterName, cluster, updateMask).get(); 176 Assert.assertEquals(expectedResponse, actualResponse); 177 178 List<String> actualRequests = mockService.getRequestPaths(); 179 Assert.assertEquals(1, actualRequests.size()); 180 181 String apiClientHeaderKey = 182 mockService 183 .getRequestHeaders() 184 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 185 .iterator() 186 .next(); 187 Assert.assertTrue( 188 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 189 .matcher(apiClientHeaderKey) 190 .matches()); 191 } 192 193 @Test updateClusterExceptionTest()194 public void updateClusterExceptionTest() throws Exception { 195 ApiException exception = 196 ApiExceptionFactory.createException( 197 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 198 mockService.addException(exception); 199 200 try { 201 String projectId = "projectId-1530"; 202 String region = "region-9622"; 203 String clusterName = "clusterName-7787"; 204 Cluster cluster = Cluster.newBuilder().build(); 205 FieldMask updateMask = FieldMask.newBuilder().build(); 206 client.updateClusterAsync(projectId, region, clusterName, cluster, updateMask).get(); 207 Assert.fail("No exception raised"); 208 } catch (ExecutionException e) { 209 } 210 } 211 212 @Test stopClusterTest()213 public void stopClusterTest() throws Exception { 214 Cluster expectedResponse = 215 Cluster.newBuilder() 216 .setProjectId("projectId-894832108") 217 .setClusterName("clusterName-1141738587") 218 .setConfig(ClusterConfig.newBuilder().build()) 219 .setVirtualClusterConfig(VirtualClusterConfig.newBuilder().build()) 220 .putAllLabels(new HashMap<String, String>()) 221 .setStatus(ClusterStatus.newBuilder().build()) 222 .addAllStatusHistory(new ArrayList<ClusterStatus>()) 223 .setClusterUuid("clusterUuid-1141510955") 224 .setMetrics(ClusterMetrics.newBuilder().build()) 225 .build(); 226 Operation resultOperation = 227 Operation.newBuilder() 228 .setName("stopClusterTest") 229 .setDone(true) 230 .setResponse(Any.pack(expectedResponse)) 231 .build(); 232 mockService.addResponse(resultOperation); 233 234 StopClusterRequest request = 235 StopClusterRequest.newBuilder() 236 .setProjectId("projectId-1530") 237 .setRegion("region-9622") 238 .setClusterName("clusterName-7787") 239 .setClusterUuid("clusterUuid-1141510955") 240 .setRequestId("requestId693933066") 241 .build(); 242 243 Cluster actualResponse = client.stopClusterAsync(request).get(); 244 Assert.assertEquals(expectedResponse, actualResponse); 245 246 List<String> actualRequests = mockService.getRequestPaths(); 247 Assert.assertEquals(1, actualRequests.size()); 248 249 String apiClientHeaderKey = 250 mockService 251 .getRequestHeaders() 252 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 253 .iterator() 254 .next(); 255 Assert.assertTrue( 256 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 257 .matcher(apiClientHeaderKey) 258 .matches()); 259 } 260 261 @Test stopClusterExceptionTest()262 public void stopClusterExceptionTest() throws Exception { 263 ApiException exception = 264 ApiExceptionFactory.createException( 265 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 266 mockService.addException(exception); 267 268 try { 269 StopClusterRequest request = 270 StopClusterRequest.newBuilder() 271 .setProjectId("projectId-1530") 272 .setRegion("region-9622") 273 .setClusterName("clusterName-7787") 274 .setClusterUuid("clusterUuid-1141510955") 275 .setRequestId("requestId693933066") 276 .build(); 277 client.stopClusterAsync(request).get(); 278 Assert.fail("No exception raised"); 279 } catch (ExecutionException e) { 280 } 281 } 282 283 @Test startClusterTest()284 public void startClusterTest() throws Exception { 285 Cluster expectedResponse = 286 Cluster.newBuilder() 287 .setProjectId("projectId-894832108") 288 .setClusterName("clusterName-1141738587") 289 .setConfig(ClusterConfig.newBuilder().build()) 290 .setVirtualClusterConfig(VirtualClusterConfig.newBuilder().build()) 291 .putAllLabels(new HashMap<String, String>()) 292 .setStatus(ClusterStatus.newBuilder().build()) 293 .addAllStatusHistory(new ArrayList<ClusterStatus>()) 294 .setClusterUuid("clusterUuid-1141510955") 295 .setMetrics(ClusterMetrics.newBuilder().build()) 296 .build(); 297 Operation resultOperation = 298 Operation.newBuilder() 299 .setName("startClusterTest") 300 .setDone(true) 301 .setResponse(Any.pack(expectedResponse)) 302 .build(); 303 mockService.addResponse(resultOperation); 304 305 StartClusterRequest request = 306 StartClusterRequest.newBuilder() 307 .setProjectId("projectId-1530") 308 .setRegion("region-9622") 309 .setClusterName("clusterName-7787") 310 .setClusterUuid("clusterUuid-1141510955") 311 .setRequestId("requestId693933066") 312 .build(); 313 314 Cluster actualResponse = client.startClusterAsync(request).get(); 315 Assert.assertEquals(expectedResponse, actualResponse); 316 317 List<String> actualRequests = mockService.getRequestPaths(); 318 Assert.assertEquals(1, actualRequests.size()); 319 320 String apiClientHeaderKey = 321 mockService 322 .getRequestHeaders() 323 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 324 .iterator() 325 .next(); 326 Assert.assertTrue( 327 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 328 .matcher(apiClientHeaderKey) 329 .matches()); 330 } 331 332 @Test startClusterExceptionTest()333 public void startClusterExceptionTest() throws Exception { 334 ApiException exception = 335 ApiExceptionFactory.createException( 336 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 337 mockService.addException(exception); 338 339 try { 340 StartClusterRequest request = 341 StartClusterRequest.newBuilder() 342 .setProjectId("projectId-1530") 343 .setRegion("region-9622") 344 .setClusterName("clusterName-7787") 345 .setClusterUuid("clusterUuid-1141510955") 346 .setRequestId("requestId693933066") 347 .build(); 348 client.startClusterAsync(request).get(); 349 Assert.fail("No exception raised"); 350 } catch (ExecutionException e) { 351 } 352 } 353 354 @Test deleteClusterTest()355 public void deleteClusterTest() throws Exception { 356 Empty expectedResponse = Empty.newBuilder().build(); 357 Operation resultOperation = 358 Operation.newBuilder() 359 .setName("deleteClusterTest") 360 .setDone(true) 361 .setResponse(Any.pack(expectedResponse)) 362 .build(); 363 mockService.addResponse(resultOperation); 364 365 String projectId = "projectId-1530"; 366 String region = "region-9622"; 367 String clusterName = "clusterName-7787"; 368 369 client.deleteClusterAsync(projectId, region, clusterName).get(); 370 371 List<String> actualRequests = mockService.getRequestPaths(); 372 Assert.assertEquals(1, actualRequests.size()); 373 374 String apiClientHeaderKey = 375 mockService 376 .getRequestHeaders() 377 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 378 .iterator() 379 .next(); 380 Assert.assertTrue( 381 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 382 .matcher(apiClientHeaderKey) 383 .matches()); 384 } 385 386 @Test deleteClusterExceptionTest()387 public void deleteClusterExceptionTest() throws Exception { 388 ApiException exception = 389 ApiExceptionFactory.createException( 390 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 391 mockService.addException(exception); 392 393 try { 394 String projectId = "projectId-1530"; 395 String region = "region-9622"; 396 String clusterName = "clusterName-7787"; 397 client.deleteClusterAsync(projectId, region, clusterName).get(); 398 Assert.fail("No exception raised"); 399 } catch (ExecutionException e) { 400 } 401 } 402 403 @Test getClusterTest()404 public void getClusterTest() throws Exception { 405 Cluster expectedResponse = 406 Cluster.newBuilder() 407 .setProjectId("projectId-894832108") 408 .setClusterName("clusterName-1141738587") 409 .setConfig(ClusterConfig.newBuilder().build()) 410 .setVirtualClusterConfig(VirtualClusterConfig.newBuilder().build()) 411 .putAllLabels(new HashMap<String, String>()) 412 .setStatus(ClusterStatus.newBuilder().build()) 413 .addAllStatusHistory(new ArrayList<ClusterStatus>()) 414 .setClusterUuid("clusterUuid-1141510955") 415 .setMetrics(ClusterMetrics.newBuilder().build()) 416 .build(); 417 mockService.addResponse(expectedResponse); 418 419 String projectId = "projectId-1530"; 420 String region = "region-9622"; 421 String clusterName = "clusterName-7787"; 422 423 Cluster actualResponse = client.getCluster(projectId, region, clusterName); 424 Assert.assertEquals(expectedResponse, actualResponse); 425 426 List<String> actualRequests = mockService.getRequestPaths(); 427 Assert.assertEquals(1, actualRequests.size()); 428 429 String apiClientHeaderKey = 430 mockService 431 .getRequestHeaders() 432 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 433 .iterator() 434 .next(); 435 Assert.assertTrue( 436 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 437 .matcher(apiClientHeaderKey) 438 .matches()); 439 } 440 441 @Test getClusterExceptionTest()442 public void getClusterExceptionTest() throws Exception { 443 ApiException exception = 444 ApiExceptionFactory.createException( 445 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 446 mockService.addException(exception); 447 448 try { 449 String projectId = "projectId-1530"; 450 String region = "region-9622"; 451 String clusterName = "clusterName-7787"; 452 client.getCluster(projectId, region, clusterName); 453 Assert.fail("No exception raised"); 454 } catch (InvalidArgumentException e) { 455 // Expected exception. 456 } 457 } 458 459 @Test listClustersTest()460 public void listClustersTest() throws Exception { 461 Cluster responsesElement = Cluster.newBuilder().build(); 462 ListClustersResponse expectedResponse = 463 ListClustersResponse.newBuilder() 464 .setNextPageToken("") 465 .addAllClusters(Arrays.asList(responsesElement)) 466 .build(); 467 mockService.addResponse(expectedResponse); 468 469 String projectId = "projectId-1530"; 470 String region = "region-9622"; 471 472 ListClustersPagedResponse pagedListResponse = client.listClusters(projectId, region); 473 474 List<Cluster> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 475 476 Assert.assertEquals(1, resources.size()); 477 Assert.assertEquals(expectedResponse.getClustersList().get(0), resources.get(0)); 478 479 List<String> actualRequests = mockService.getRequestPaths(); 480 Assert.assertEquals(1, actualRequests.size()); 481 482 String apiClientHeaderKey = 483 mockService 484 .getRequestHeaders() 485 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 486 .iterator() 487 .next(); 488 Assert.assertTrue( 489 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 490 .matcher(apiClientHeaderKey) 491 .matches()); 492 } 493 494 @Test listClustersExceptionTest()495 public void listClustersExceptionTest() throws Exception { 496 ApiException exception = 497 ApiExceptionFactory.createException( 498 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 499 mockService.addException(exception); 500 501 try { 502 String projectId = "projectId-1530"; 503 String region = "region-9622"; 504 client.listClusters(projectId, region); 505 Assert.fail("No exception raised"); 506 } catch (InvalidArgumentException e) { 507 // Expected exception. 508 } 509 } 510 511 @Test listClustersTest2()512 public void listClustersTest2() throws Exception { 513 Cluster responsesElement = Cluster.newBuilder().build(); 514 ListClustersResponse expectedResponse = 515 ListClustersResponse.newBuilder() 516 .setNextPageToken("") 517 .addAllClusters(Arrays.asList(responsesElement)) 518 .build(); 519 mockService.addResponse(expectedResponse); 520 521 String projectId = "projectId-1530"; 522 String region = "region-9622"; 523 String filter = "filter-1274492040"; 524 525 ListClustersPagedResponse pagedListResponse = client.listClusters(projectId, region, filter); 526 527 List<Cluster> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 528 529 Assert.assertEquals(1, resources.size()); 530 Assert.assertEquals(expectedResponse.getClustersList().get(0), resources.get(0)); 531 532 List<String> actualRequests = mockService.getRequestPaths(); 533 Assert.assertEquals(1, actualRequests.size()); 534 535 String apiClientHeaderKey = 536 mockService 537 .getRequestHeaders() 538 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 539 .iterator() 540 .next(); 541 Assert.assertTrue( 542 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 543 .matcher(apiClientHeaderKey) 544 .matches()); 545 } 546 547 @Test listClustersExceptionTest2()548 public void listClustersExceptionTest2() throws Exception { 549 ApiException exception = 550 ApiExceptionFactory.createException( 551 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 552 mockService.addException(exception); 553 554 try { 555 String projectId = "projectId-1530"; 556 String region = "region-9622"; 557 String filter = "filter-1274492040"; 558 client.listClusters(projectId, region, filter); 559 Assert.fail("No exception raised"); 560 } catch (InvalidArgumentException e) { 561 // Expected exception. 562 } 563 } 564 565 @Test diagnoseClusterTest()566 public void diagnoseClusterTest() throws Exception { 567 DiagnoseClusterResults expectedResponse = 568 DiagnoseClusterResults.newBuilder().setOutputUri("outputUri-2119300949").build(); 569 Operation resultOperation = 570 Operation.newBuilder() 571 .setName("diagnoseClusterTest") 572 .setDone(true) 573 .setResponse(Any.pack(expectedResponse)) 574 .build(); 575 mockService.addResponse(resultOperation); 576 577 String projectId = "projectId-1530"; 578 String region = "region-9622"; 579 String clusterName = "clusterName-7787"; 580 581 DiagnoseClusterResults actualResponse = 582 client.diagnoseClusterAsync(projectId, region, clusterName).get(); 583 Assert.assertEquals(expectedResponse, actualResponse); 584 585 List<String> actualRequests = mockService.getRequestPaths(); 586 Assert.assertEquals(1, actualRequests.size()); 587 588 String apiClientHeaderKey = 589 mockService 590 .getRequestHeaders() 591 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 592 .iterator() 593 .next(); 594 Assert.assertTrue( 595 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 596 .matcher(apiClientHeaderKey) 597 .matches()); 598 } 599 600 @Test diagnoseClusterExceptionTest()601 public void diagnoseClusterExceptionTest() throws Exception { 602 ApiException exception = 603 ApiExceptionFactory.createException( 604 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 605 mockService.addException(exception); 606 607 try { 608 String projectId = "projectId-1530"; 609 String region = "region-9622"; 610 String clusterName = "clusterName-7787"; 611 client.diagnoseClusterAsync(projectId, region, clusterName).get(); 612 Assert.fail("No exception raised"); 613 } catch (ExecutionException e) { 614 } 615 } 616 } 617