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.compute.v1; 18 19 import static com.google.cloud.compute.v1.SnapshotsClient.ListPagedResponse; 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.compute.v1.Operation.Status; 31 import com.google.cloud.compute.v1.stub.HttpJsonSnapshotsStub; 32 import com.google.common.collect.Lists; 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.concurrent.ExecutionException; 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 SnapshotsClientTest { 49 private static MockHttpService mockService; 50 private static SnapshotsClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() throws IOException { 54 mockService = 55 new MockHttpService( 56 HttpJsonSnapshotsStub.getMethodDescriptors(), SnapshotsSettings.getDefaultEndpoint()); 57 SnapshotsSettings settings = 58 SnapshotsSettings.newBuilder() 59 .setTransportChannelProvider( 60 SnapshotsSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = SnapshotsClient.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 deleteTest()82 public void deleteTest() throws Exception { 83 Operation expectedResponse = 84 Operation.newBuilder() 85 .setClientOperationId("clientOperationId-1230366697") 86 .setCreationTimestamp("creationTimestamp-370203401") 87 .setDescription("description-1724546052") 88 .setEndTime("endTime-1607243192") 89 .setError(Error.newBuilder().build()) 90 .setHttpErrorMessage("httpErrorMessage1577303431") 91 .setHttpErrorStatusCode(0) 92 .setId(3355) 93 .setInsertTime("insertTime966165798") 94 .setKind("kind3292052") 95 .setName("name3373707") 96 .setOperationGroupId("operationGroupId1716161683") 97 .setOperationType("operationType91999553") 98 .setProgress(-1001078227) 99 .setRegion("region-934795532") 100 .setSelfLink("selfLink1191800166") 101 .setStartTime("startTime-2129294769") 102 .setStatus(Status.DONE) 103 .setStatusMessage("statusMessage-958704715") 104 .setTargetId(-815576439) 105 .setTargetLink("targetLink486368555") 106 .setUser("user3599307") 107 .addAllWarnings(new ArrayList<Warnings>()) 108 .setZone("zone3744684") 109 .build(); 110 mockService.addResponse(expectedResponse); 111 112 String project = "project-6911"; 113 String snapshot = "snapshot-5030"; 114 115 Operation actualResponse = client.deleteAsync(project, snapshot).get(); 116 Assert.assertEquals(expectedResponse, actualResponse); 117 118 List<String> actualRequests = mockService.getRequestPaths(); 119 Assert.assertEquals(1, actualRequests.size()); 120 121 String apiClientHeaderKey = 122 mockService 123 .getRequestHeaders() 124 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 125 .iterator() 126 .next(); 127 Assert.assertTrue( 128 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 129 .matcher(apiClientHeaderKey) 130 .matches()); 131 } 132 133 @Test deleteExceptionTest()134 public void deleteExceptionTest() throws Exception { 135 ApiException exception = 136 ApiExceptionFactory.createException( 137 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 138 mockService.addException(exception); 139 140 try { 141 String project = "project-6911"; 142 String snapshot = "snapshot-5030"; 143 client.deleteAsync(project, snapshot).get(); 144 Assert.fail("No exception raised"); 145 } catch (ExecutionException e) { 146 } 147 } 148 149 @Test getTest()150 public void getTest() throws Exception { 151 Snapshot expectedResponse = 152 Snapshot.newBuilder() 153 .setArchitecture("architecture839674195") 154 .setAutoCreated(true) 155 .setChainName("chainName1317895404") 156 .setCreationSizeBytes(1199141901) 157 .setCreationTimestamp("creationTimestamp-370203401") 158 .setDescription("description-1724546052") 159 .setDiskSizeGb(-757478089) 160 .setDownloadBytes(971924980) 161 .setId(3355) 162 .setKind("kind3292052") 163 .setLabelFingerprint("labelFingerprint379449680") 164 .putAllLabels(new HashMap<String, String>()) 165 .addAllLicenseCodes(new ArrayList<Long>()) 166 .addAllLicenses(new ArrayList<String>()) 167 .setLocationHint("locationHint-58650660") 168 .setName("name3373707") 169 .setSatisfiesPzs(true) 170 .setSelfLink("selfLink1191800166") 171 .setSnapshotEncryptionKey(CustomerEncryptionKey.newBuilder().build()) 172 .setSnapshotType("snapshotType-931506402") 173 .setSourceDisk("sourceDisk-1111923624") 174 .setSourceDiskEncryptionKey(CustomerEncryptionKey.newBuilder().build()) 175 .setSourceDiskId("sourceDiskId888256403") 176 .setSourceSnapshotSchedulePolicy("sourceSnapshotSchedulePolicy-1664282744") 177 .setSourceSnapshotSchedulePolicyId("sourceSnapshotSchedulePolicyId-1647880509") 178 .setStatus("status-892481550") 179 .setStorageBytes(2035244455) 180 .setStorageBytesStatus("storageBytesStatus-1653482814") 181 .addAllStorageLocations(new ArrayList<String>()) 182 .build(); 183 mockService.addResponse(expectedResponse); 184 185 String project = "project-6911"; 186 String snapshot = "snapshot-5030"; 187 188 Snapshot actualResponse = client.get(project, snapshot); 189 Assert.assertEquals(expectedResponse, actualResponse); 190 191 List<String> actualRequests = mockService.getRequestPaths(); 192 Assert.assertEquals(1, actualRequests.size()); 193 194 String apiClientHeaderKey = 195 mockService 196 .getRequestHeaders() 197 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 198 .iterator() 199 .next(); 200 Assert.assertTrue( 201 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 202 .matcher(apiClientHeaderKey) 203 .matches()); 204 } 205 206 @Test getExceptionTest()207 public void getExceptionTest() throws Exception { 208 ApiException exception = 209 ApiExceptionFactory.createException( 210 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 211 mockService.addException(exception); 212 213 try { 214 String project = "project-6911"; 215 String snapshot = "snapshot-5030"; 216 client.get(project, snapshot); 217 Assert.fail("No exception raised"); 218 } catch (InvalidArgumentException e) { 219 // Expected exception. 220 } 221 } 222 223 @Test getIamPolicyTest()224 public void getIamPolicyTest() throws Exception { 225 Policy expectedResponse = 226 Policy.newBuilder() 227 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 228 .addAllBindings(new ArrayList<Binding>()) 229 .setEtag("etag3123477") 230 .setIamOwned(true) 231 .addAllRules(new ArrayList<Rule>()) 232 .setVersion(351608024) 233 .build(); 234 mockService.addResponse(expectedResponse); 235 236 String project = "project-6911"; 237 String resource = "resource-756"; 238 239 Policy actualResponse = client.getIamPolicy(project, resource); 240 Assert.assertEquals(expectedResponse, actualResponse); 241 242 List<String> actualRequests = mockService.getRequestPaths(); 243 Assert.assertEquals(1, actualRequests.size()); 244 245 String apiClientHeaderKey = 246 mockService 247 .getRequestHeaders() 248 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 249 .iterator() 250 .next(); 251 Assert.assertTrue( 252 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 253 .matcher(apiClientHeaderKey) 254 .matches()); 255 } 256 257 @Test getIamPolicyExceptionTest()258 public void getIamPolicyExceptionTest() throws Exception { 259 ApiException exception = 260 ApiExceptionFactory.createException( 261 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 262 mockService.addException(exception); 263 264 try { 265 String project = "project-6911"; 266 String resource = "resource-756"; 267 client.getIamPolicy(project, resource); 268 Assert.fail("No exception raised"); 269 } catch (InvalidArgumentException e) { 270 // Expected exception. 271 } 272 } 273 274 @Test insertTest()275 public void insertTest() throws Exception { 276 Operation expectedResponse = 277 Operation.newBuilder() 278 .setClientOperationId("clientOperationId-1230366697") 279 .setCreationTimestamp("creationTimestamp-370203401") 280 .setDescription("description-1724546052") 281 .setEndTime("endTime-1607243192") 282 .setError(Error.newBuilder().build()) 283 .setHttpErrorMessage("httpErrorMessage1577303431") 284 .setHttpErrorStatusCode(0) 285 .setId(3355) 286 .setInsertTime("insertTime966165798") 287 .setKind("kind3292052") 288 .setName("name3373707") 289 .setOperationGroupId("operationGroupId1716161683") 290 .setOperationType("operationType91999553") 291 .setProgress(-1001078227) 292 .setRegion("region-934795532") 293 .setSelfLink("selfLink1191800166") 294 .setStartTime("startTime-2129294769") 295 .setStatus(Status.DONE) 296 .setStatusMessage("statusMessage-958704715") 297 .setTargetId(-815576439) 298 .setTargetLink("targetLink486368555") 299 .setUser("user3599307") 300 .addAllWarnings(new ArrayList<Warnings>()) 301 .setZone("zone3744684") 302 .build(); 303 mockService.addResponse(expectedResponse); 304 305 String project = "project-6911"; 306 Snapshot snapshotResource = Snapshot.newBuilder().build(); 307 308 Operation actualResponse = client.insertAsync(project, snapshotResource).get(); 309 Assert.assertEquals(expectedResponse, actualResponse); 310 311 List<String> actualRequests = mockService.getRequestPaths(); 312 Assert.assertEquals(1, actualRequests.size()); 313 314 String apiClientHeaderKey = 315 mockService 316 .getRequestHeaders() 317 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 318 .iterator() 319 .next(); 320 Assert.assertTrue( 321 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 322 .matcher(apiClientHeaderKey) 323 .matches()); 324 } 325 326 @Test insertExceptionTest()327 public void insertExceptionTest() throws Exception { 328 ApiException exception = 329 ApiExceptionFactory.createException( 330 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 331 mockService.addException(exception); 332 333 try { 334 String project = "project-6911"; 335 Snapshot snapshotResource = Snapshot.newBuilder().build(); 336 client.insertAsync(project, snapshotResource).get(); 337 Assert.fail("No exception raised"); 338 } catch (ExecutionException e) { 339 } 340 } 341 342 @Test listTest()343 public void listTest() throws Exception { 344 Snapshot responsesElement = Snapshot.newBuilder().build(); 345 SnapshotList expectedResponse = 346 SnapshotList.newBuilder() 347 .setNextPageToken("") 348 .addAllItems(Arrays.asList(responsesElement)) 349 .build(); 350 mockService.addResponse(expectedResponse); 351 352 String project = "project-6911"; 353 354 ListPagedResponse pagedListResponse = client.list(project); 355 356 List<Snapshot> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 357 358 Assert.assertEquals(1, resources.size()); 359 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 360 361 List<String> actualRequests = mockService.getRequestPaths(); 362 Assert.assertEquals(1, actualRequests.size()); 363 364 String apiClientHeaderKey = 365 mockService 366 .getRequestHeaders() 367 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 368 .iterator() 369 .next(); 370 Assert.assertTrue( 371 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 372 .matcher(apiClientHeaderKey) 373 .matches()); 374 } 375 376 @Test listExceptionTest()377 public void listExceptionTest() throws Exception { 378 ApiException exception = 379 ApiExceptionFactory.createException( 380 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 381 mockService.addException(exception); 382 383 try { 384 String project = "project-6911"; 385 client.list(project); 386 Assert.fail("No exception raised"); 387 } catch (InvalidArgumentException e) { 388 // Expected exception. 389 } 390 } 391 392 @Test setIamPolicyTest()393 public void setIamPolicyTest() throws Exception { 394 Policy expectedResponse = 395 Policy.newBuilder() 396 .addAllAuditConfigs(new ArrayList<AuditConfig>()) 397 .addAllBindings(new ArrayList<Binding>()) 398 .setEtag("etag3123477") 399 .setIamOwned(true) 400 .addAllRules(new ArrayList<Rule>()) 401 .setVersion(351608024) 402 .build(); 403 mockService.addResponse(expectedResponse); 404 405 String project = "project-6911"; 406 String resource = "resource-756"; 407 GlobalSetPolicyRequest globalSetPolicyRequestResource = 408 GlobalSetPolicyRequest.newBuilder().build(); 409 410 Policy actualResponse = client.setIamPolicy(project, resource, globalSetPolicyRequestResource); 411 Assert.assertEquals(expectedResponse, actualResponse); 412 413 List<String> actualRequests = mockService.getRequestPaths(); 414 Assert.assertEquals(1, actualRequests.size()); 415 416 String apiClientHeaderKey = 417 mockService 418 .getRequestHeaders() 419 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 420 .iterator() 421 .next(); 422 Assert.assertTrue( 423 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 424 .matcher(apiClientHeaderKey) 425 .matches()); 426 } 427 428 @Test setIamPolicyExceptionTest()429 public void setIamPolicyExceptionTest() throws Exception { 430 ApiException exception = 431 ApiExceptionFactory.createException( 432 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 433 mockService.addException(exception); 434 435 try { 436 String project = "project-6911"; 437 String resource = "resource-756"; 438 GlobalSetPolicyRequest globalSetPolicyRequestResource = 439 GlobalSetPolicyRequest.newBuilder().build(); 440 client.setIamPolicy(project, resource, globalSetPolicyRequestResource); 441 Assert.fail("No exception raised"); 442 } catch (InvalidArgumentException e) { 443 // Expected exception. 444 } 445 } 446 447 @Test setLabelsTest()448 public void setLabelsTest() throws Exception { 449 Operation expectedResponse = 450 Operation.newBuilder() 451 .setClientOperationId("clientOperationId-1230366697") 452 .setCreationTimestamp("creationTimestamp-370203401") 453 .setDescription("description-1724546052") 454 .setEndTime("endTime-1607243192") 455 .setError(Error.newBuilder().build()) 456 .setHttpErrorMessage("httpErrorMessage1577303431") 457 .setHttpErrorStatusCode(0) 458 .setId(3355) 459 .setInsertTime("insertTime966165798") 460 .setKind("kind3292052") 461 .setName("name3373707") 462 .setOperationGroupId("operationGroupId1716161683") 463 .setOperationType("operationType91999553") 464 .setProgress(-1001078227) 465 .setRegion("region-934795532") 466 .setSelfLink("selfLink1191800166") 467 .setStartTime("startTime-2129294769") 468 .setStatus(Status.DONE) 469 .setStatusMessage("statusMessage-958704715") 470 .setTargetId(-815576439) 471 .setTargetLink("targetLink486368555") 472 .setUser("user3599307") 473 .addAllWarnings(new ArrayList<Warnings>()) 474 .setZone("zone3744684") 475 .build(); 476 mockService.addResponse(expectedResponse); 477 478 String project = "project-6911"; 479 String resource = "resource-756"; 480 GlobalSetLabelsRequest globalSetLabelsRequestResource = 481 GlobalSetLabelsRequest.newBuilder().build(); 482 483 Operation actualResponse = 484 client.setLabelsAsync(project, resource, globalSetLabelsRequestResource).get(); 485 Assert.assertEquals(expectedResponse, actualResponse); 486 487 List<String> actualRequests = mockService.getRequestPaths(); 488 Assert.assertEquals(1, actualRequests.size()); 489 490 String apiClientHeaderKey = 491 mockService 492 .getRequestHeaders() 493 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 494 .iterator() 495 .next(); 496 Assert.assertTrue( 497 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 498 .matcher(apiClientHeaderKey) 499 .matches()); 500 } 501 502 @Test setLabelsExceptionTest()503 public void setLabelsExceptionTest() throws Exception { 504 ApiException exception = 505 ApiExceptionFactory.createException( 506 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 507 mockService.addException(exception); 508 509 try { 510 String project = "project-6911"; 511 String resource = "resource-756"; 512 GlobalSetLabelsRequest globalSetLabelsRequestResource = 513 GlobalSetLabelsRequest.newBuilder().build(); 514 client.setLabelsAsync(project, resource, globalSetLabelsRequestResource).get(); 515 Assert.fail("No exception raised"); 516 } catch (ExecutionException e) { 517 } 518 } 519 520 @Test testIamPermissionsTest()521 public void testIamPermissionsTest() throws Exception { 522 TestPermissionsResponse expectedResponse = 523 TestPermissionsResponse.newBuilder().addAllPermissions(new ArrayList<String>()).build(); 524 mockService.addResponse(expectedResponse); 525 526 String project = "project-6911"; 527 String resource = "resource-756"; 528 TestPermissionsRequest testPermissionsRequestResource = 529 TestPermissionsRequest.newBuilder().build(); 530 531 TestPermissionsResponse actualResponse = 532 client.testIamPermissions(project, resource, testPermissionsRequestResource); 533 Assert.assertEquals(expectedResponse, actualResponse); 534 535 List<String> actualRequests = mockService.getRequestPaths(); 536 Assert.assertEquals(1, actualRequests.size()); 537 538 String apiClientHeaderKey = 539 mockService 540 .getRequestHeaders() 541 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 542 .iterator() 543 .next(); 544 Assert.assertTrue( 545 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 546 .matcher(apiClientHeaderKey) 547 .matches()); 548 } 549 550 @Test testIamPermissionsExceptionTest()551 public void testIamPermissionsExceptionTest() throws Exception { 552 ApiException exception = 553 ApiExceptionFactory.createException( 554 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 555 mockService.addException(exception); 556 557 try { 558 String project = "project-6911"; 559 String resource = "resource-756"; 560 TestPermissionsRequest testPermissionsRequestResource = 561 TestPermissionsRequest.newBuilder().build(); 562 client.testIamPermissions(project, resource, testPermissionsRequestResource); 563 Assert.fail("No exception raised"); 564 } catch (InvalidArgumentException e) { 565 // Expected exception. 566 } 567 } 568 } 569