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.HealthChecksClient.AggregatedListPagedResponse; 20 import static com.google.cloud.compute.v1.HealthChecksClient.ListPagedResponse; 21 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.httpjson.GaxHttpJsonProperties; 24 import com.google.api.gax.httpjson.testing.MockHttpService; 25 import com.google.api.gax.rpc.ApiClientHeaderProvider; 26 import com.google.api.gax.rpc.ApiException; 27 import com.google.api.gax.rpc.ApiExceptionFactory; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.api.gax.rpc.StatusCode; 30 import com.google.api.gax.rpc.testing.FakeStatusCode; 31 import com.google.cloud.compute.v1.Operation.Status; 32 import com.google.cloud.compute.v1.stub.HttpJsonHealthChecksStub; 33 import com.google.common.collect.Lists; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.Collections; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.concurrent.ExecutionException; 41 import javax.annotation.Generated; 42 import org.junit.After; 43 import org.junit.AfterClass; 44 import org.junit.Assert; 45 import org.junit.Before; 46 import org.junit.BeforeClass; 47 import org.junit.Test; 48 49 @Generated("by gapic-generator-java") 50 public class HealthChecksClientTest { 51 private static MockHttpService mockService; 52 private static HealthChecksClient client; 53 54 @BeforeClass startStaticServer()55 public static void startStaticServer() throws IOException { 56 mockService = 57 new MockHttpService( 58 HttpJsonHealthChecksStub.getMethodDescriptors(), 59 HealthChecksSettings.getDefaultEndpoint()); 60 HealthChecksSettings settings = 61 HealthChecksSettings.newBuilder() 62 .setTransportChannelProvider( 63 HealthChecksSettings.defaultHttpJsonTransportProviderBuilder() 64 .setHttpTransport(mockService) 65 .build()) 66 .setCredentialsProvider(NoCredentialsProvider.create()) 67 .build(); 68 client = HealthChecksClient.create(settings); 69 } 70 71 @AfterClass stopServer()72 public static void stopServer() { 73 client.close(); 74 } 75 76 @Before setUp()77 public void setUp() {} 78 79 @After tearDown()80 public void tearDown() throws Exception { 81 mockService.reset(); 82 } 83 84 @Test aggregatedListTest()85 public void aggregatedListTest() throws Exception { 86 HealthChecksScopedList responsesElement = HealthChecksScopedList.newBuilder().build(); 87 HealthChecksAggregatedList expectedResponse = 88 HealthChecksAggregatedList.newBuilder() 89 .setNextPageToken("") 90 .putAllItems(Collections.singletonMap("items", responsesElement)) 91 .build(); 92 mockService.addResponse(expectedResponse); 93 94 String project = "project-6911"; 95 96 AggregatedListPagedResponse pagedListResponse = client.aggregatedList(project); 97 98 List<Map.Entry<String, HealthChecksScopedList>> resources = 99 Lists.newArrayList(pagedListResponse.iterateAll()); 100 101 Assert.assertEquals(1, resources.size()); 102 Assert.assertEquals( 103 expectedResponse.getItemsMap().entrySet().iterator().next(), resources.get(0)); 104 105 List<String> actualRequests = mockService.getRequestPaths(); 106 Assert.assertEquals(1, actualRequests.size()); 107 108 String apiClientHeaderKey = 109 mockService 110 .getRequestHeaders() 111 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 112 .iterator() 113 .next(); 114 Assert.assertTrue( 115 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 116 .matcher(apiClientHeaderKey) 117 .matches()); 118 } 119 120 @Test aggregatedListExceptionTest()121 public void aggregatedListExceptionTest() throws Exception { 122 ApiException exception = 123 ApiExceptionFactory.createException( 124 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 125 mockService.addException(exception); 126 127 try { 128 String project = "project-6911"; 129 client.aggregatedList(project); 130 Assert.fail("No exception raised"); 131 } catch (InvalidArgumentException e) { 132 // Expected exception. 133 } 134 } 135 136 @Test deleteTest()137 public void deleteTest() throws Exception { 138 Operation expectedResponse = 139 Operation.newBuilder() 140 .setClientOperationId("clientOperationId-1230366697") 141 .setCreationTimestamp("creationTimestamp-370203401") 142 .setDescription("description-1724546052") 143 .setEndTime("endTime-1607243192") 144 .setError(Error.newBuilder().build()) 145 .setHttpErrorMessage("httpErrorMessage1577303431") 146 .setHttpErrorStatusCode(0) 147 .setId(3355) 148 .setInsertTime("insertTime966165798") 149 .setKind("kind3292052") 150 .setName("name3373707") 151 .setOperationGroupId("operationGroupId1716161683") 152 .setOperationType("operationType91999553") 153 .setProgress(-1001078227) 154 .setRegion("region-934795532") 155 .setSelfLink("selfLink1191800166") 156 .setStartTime("startTime-2129294769") 157 .setStatus(Status.DONE) 158 .setStatusMessage("statusMessage-958704715") 159 .setTargetId(-815576439) 160 .setTargetLink("targetLink486368555") 161 .setUser("user3599307") 162 .addAllWarnings(new ArrayList<Warnings>()) 163 .setZone("zone3744684") 164 .build(); 165 mockService.addResponse(expectedResponse); 166 167 String project = "project-6911"; 168 String healthCheck = "healthCheck-7822"; 169 170 Operation actualResponse = client.deleteAsync(project, healthCheck).get(); 171 Assert.assertEquals(expectedResponse, actualResponse); 172 173 List<String> actualRequests = mockService.getRequestPaths(); 174 Assert.assertEquals(1, actualRequests.size()); 175 176 String apiClientHeaderKey = 177 mockService 178 .getRequestHeaders() 179 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 180 .iterator() 181 .next(); 182 Assert.assertTrue( 183 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 184 .matcher(apiClientHeaderKey) 185 .matches()); 186 } 187 188 @Test deleteExceptionTest()189 public void deleteExceptionTest() throws Exception { 190 ApiException exception = 191 ApiExceptionFactory.createException( 192 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 193 mockService.addException(exception); 194 195 try { 196 String project = "project-6911"; 197 String healthCheck = "healthCheck-7822"; 198 client.deleteAsync(project, healthCheck).get(); 199 Assert.fail("No exception raised"); 200 } catch (ExecutionException e) { 201 } 202 } 203 204 @Test getTest()205 public void getTest() throws Exception { 206 HealthCheck expectedResponse = 207 HealthCheck.newBuilder() 208 .setCheckIntervalSec(345561006) 209 .setCreationTimestamp("creationTimestamp-370203401") 210 .setDescription("description-1724546052") 211 .setGrpcHealthCheck(GRPCHealthCheck.newBuilder().build()) 212 .setHealthyThreshold(-133658551) 213 .setHttp2HealthCheck(HTTP2HealthCheck.newBuilder().build()) 214 .setHttpHealthCheck(HTTPHealthCheck.newBuilder().build()) 215 .setHttpsHealthCheck(HTTPSHealthCheck.newBuilder().build()) 216 .setId(3355) 217 .setKind("kind3292052") 218 .setLogConfig(HealthCheckLogConfig.newBuilder().build()) 219 .setName("name3373707") 220 .setRegion("region-934795532") 221 .setSelfLink("selfLink1191800166") 222 .setSslHealthCheck(SSLHealthCheck.newBuilder().build()) 223 .setTcpHealthCheck(TCPHealthCheck.newBuilder().build()) 224 .setTimeoutSec(-2067488653) 225 .setType("type3575610") 226 .setUnhealthyThreshold(1838571216) 227 .build(); 228 mockService.addResponse(expectedResponse); 229 230 String project = "project-6911"; 231 String healthCheck = "healthCheck-7822"; 232 233 HealthCheck actualResponse = client.get(project, healthCheck); 234 Assert.assertEquals(expectedResponse, actualResponse); 235 236 List<String> actualRequests = mockService.getRequestPaths(); 237 Assert.assertEquals(1, actualRequests.size()); 238 239 String apiClientHeaderKey = 240 mockService 241 .getRequestHeaders() 242 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 243 .iterator() 244 .next(); 245 Assert.assertTrue( 246 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 247 .matcher(apiClientHeaderKey) 248 .matches()); 249 } 250 251 @Test getExceptionTest()252 public void getExceptionTest() throws Exception { 253 ApiException exception = 254 ApiExceptionFactory.createException( 255 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 256 mockService.addException(exception); 257 258 try { 259 String project = "project-6911"; 260 String healthCheck = "healthCheck-7822"; 261 client.get(project, healthCheck); 262 Assert.fail("No exception raised"); 263 } catch (InvalidArgumentException e) { 264 // Expected exception. 265 } 266 } 267 268 @Test insertTest()269 public void insertTest() throws Exception { 270 Operation expectedResponse = 271 Operation.newBuilder() 272 .setClientOperationId("clientOperationId-1230366697") 273 .setCreationTimestamp("creationTimestamp-370203401") 274 .setDescription("description-1724546052") 275 .setEndTime("endTime-1607243192") 276 .setError(Error.newBuilder().build()) 277 .setHttpErrorMessage("httpErrorMessage1577303431") 278 .setHttpErrorStatusCode(0) 279 .setId(3355) 280 .setInsertTime("insertTime966165798") 281 .setKind("kind3292052") 282 .setName("name3373707") 283 .setOperationGroupId("operationGroupId1716161683") 284 .setOperationType("operationType91999553") 285 .setProgress(-1001078227) 286 .setRegion("region-934795532") 287 .setSelfLink("selfLink1191800166") 288 .setStartTime("startTime-2129294769") 289 .setStatus(Status.DONE) 290 .setStatusMessage("statusMessage-958704715") 291 .setTargetId(-815576439) 292 .setTargetLink("targetLink486368555") 293 .setUser("user3599307") 294 .addAllWarnings(new ArrayList<Warnings>()) 295 .setZone("zone3744684") 296 .build(); 297 mockService.addResponse(expectedResponse); 298 299 String project = "project-6911"; 300 HealthCheck healthCheckResource = HealthCheck.newBuilder().build(); 301 302 Operation actualResponse = client.insertAsync(project, healthCheckResource).get(); 303 Assert.assertEquals(expectedResponse, actualResponse); 304 305 List<String> actualRequests = mockService.getRequestPaths(); 306 Assert.assertEquals(1, actualRequests.size()); 307 308 String apiClientHeaderKey = 309 mockService 310 .getRequestHeaders() 311 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 312 .iterator() 313 .next(); 314 Assert.assertTrue( 315 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 316 .matcher(apiClientHeaderKey) 317 .matches()); 318 } 319 320 @Test insertExceptionTest()321 public void insertExceptionTest() throws Exception { 322 ApiException exception = 323 ApiExceptionFactory.createException( 324 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 325 mockService.addException(exception); 326 327 try { 328 String project = "project-6911"; 329 HealthCheck healthCheckResource = HealthCheck.newBuilder().build(); 330 client.insertAsync(project, healthCheckResource).get(); 331 Assert.fail("No exception raised"); 332 } catch (ExecutionException e) { 333 } 334 } 335 336 @Test listTest()337 public void listTest() throws Exception { 338 HealthCheck responsesElement = HealthCheck.newBuilder().build(); 339 HealthCheckList expectedResponse = 340 HealthCheckList.newBuilder() 341 .setNextPageToken("") 342 .addAllItems(Arrays.asList(responsesElement)) 343 .build(); 344 mockService.addResponse(expectedResponse); 345 346 String project = "project-6911"; 347 348 ListPagedResponse pagedListResponse = client.list(project); 349 350 List<HealthCheck> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 351 352 Assert.assertEquals(1, resources.size()); 353 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 354 355 List<String> actualRequests = mockService.getRequestPaths(); 356 Assert.assertEquals(1, actualRequests.size()); 357 358 String apiClientHeaderKey = 359 mockService 360 .getRequestHeaders() 361 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 362 .iterator() 363 .next(); 364 Assert.assertTrue( 365 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 366 .matcher(apiClientHeaderKey) 367 .matches()); 368 } 369 370 @Test listExceptionTest()371 public void listExceptionTest() throws Exception { 372 ApiException exception = 373 ApiExceptionFactory.createException( 374 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 375 mockService.addException(exception); 376 377 try { 378 String project = "project-6911"; 379 client.list(project); 380 Assert.fail("No exception raised"); 381 } catch (InvalidArgumentException e) { 382 // Expected exception. 383 } 384 } 385 386 @Test patchTest()387 public void patchTest() throws Exception { 388 Operation expectedResponse = 389 Operation.newBuilder() 390 .setClientOperationId("clientOperationId-1230366697") 391 .setCreationTimestamp("creationTimestamp-370203401") 392 .setDescription("description-1724546052") 393 .setEndTime("endTime-1607243192") 394 .setError(Error.newBuilder().build()) 395 .setHttpErrorMessage("httpErrorMessage1577303431") 396 .setHttpErrorStatusCode(0) 397 .setId(3355) 398 .setInsertTime("insertTime966165798") 399 .setKind("kind3292052") 400 .setName("name3373707") 401 .setOperationGroupId("operationGroupId1716161683") 402 .setOperationType("operationType91999553") 403 .setProgress(-1001078227) 404 .setRegion("region-934795532") 405 .setSelfLink("selfLink1191800166") 406 .setStartTime("startTime-2129294769") 407 .setStatus(Status.DONE) 408 .setStatusMessage("statusMessage-958704715") 409 .setTargetId(-815576439) 410 .setTargetLink("targetLink486368555") 411 .setUser("user3599307") 412 .addAllWarnings(new ArrayList<Warnings>()) 413 .setZone("zone3744684") 414 .build(); 415 mockService.addResponse(expectedResponse); 416 417 String project = "project-6911"; 418 String healthCheck = "healthCheck-7822"; 419 HealthCheck healthCheckResource = HealthCheck.newBuilder().build(); 420 421 Operation actualResponse = client.patchAsync(project, healthCheck, healthCheckResource).get(); 422 Assert.assertEquals(expectedResponse, actualResponse); 423 424 List<String> actualRequests = mockService.getRequestPaths(); 425 Assert.assertEquals(1, actualRequests.size()); 426 427 String apiClientHeaderKey = 428 mockService 429 .getRequestHeaders() 430 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 431 .iterator() 432 .next(); 433 Assert.assertTrue( 434 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 435 .matcher(apiClientHeaderKey) 436 .matches()); 437 } 438 439 @Test patchExceptionTest()440 public void patchExceptionTest() throws Exception { 441 ApiException exception = 442 ApiExceptionFactory.createException( 443 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 444 mockService.addException(exception); 445 446 try { 447 String project = "project-6911"; 448 String healthCheck = "healthCheck-7822"; 449 HealthCheck healthCheckResource = HealthCheck.newBuilder().build(); 450 client.patchAsync(project, healthCheck, healthCheckResource).get(); 451 Assert.fail("No exception raised"); 452 } catch (ExecutionException e) { 453 } 454 } 455 456 @Test updateTest()457 public void updateTest() throws Exception { 458 Operation expectedResponse = 459 Operation.newBuilder() 460 .setClientOperationId("clientOperationId-1230366697") 461 .setCreationTimestamp("creationTimestamp-370203401") 462 .setDescription("description-1724546052") 463 .setEndTime("endTime-1607243192") 464 .setError(Error.newBuilder().build()) 465 .setHttpErrorMessage("httpErrorMessage1577303431") 466 .setHttpErrorStatusCode(0) 467 .setId(3355) 468 .setInsertTime("insertTime966165798") 469 .setKind("kind3292052") 470 .setName("name3373707") 471 .setOperationGroupId("operationGroupId1716161683") 472 .setOperationType("operationType91999553") 473 .setProgress(-1001078227) 474 .setRegion("region-934795532") 475 .setSelfLink("selfLink1191800166") 476 .setStartTime("startTime-2129294769") 477 .setStatus(Status.DONE) 478 .setStatusMessage("statusMessage-958704715") 479 .setTargetId(-815576439) 480 .setTargetLink("targetLink486368555") 481 .setUser("user3599307") 482 .addAllWarnings(new ArrayList<Warnings>()) 483 .setZone("zone3744684") 484 .build(); 485 mockService.addResponse(expectedResponse); 486 487 String project = "project-6911"; 488 String healthCheck = "healthCheck-7822"; 489 HealthCheck healthCheckResource = HealthCheck.newBuilder().build(); 490 491 Operation actualResponse = client.updateAsync(project, healthCheck, healthCheckResource).get(); 492 Assert.assertEquals(expectedResponse, actualResponse); 493 494 List<String> actualRequests = mockService.getRequestPaths(); 495 Assert.assertEquals(1, actualRequests.size()); 496 497 String apiClientHeaderKey = 498 mockService 499 .getRequestHeaders() 500 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 501 .iterator() 502 .next(); 503 Assert.assertTrue( 504 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 505 .matcher(apiClientHeaderKey) 506 .matches()); 507 } 508 509 @Test updateExceptionTest()510 public void updateExceptionTest() throws Exception { 511 ApiException exception = 512 ApiExceptionFactory.createException( 513 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 514 mockService.addException(exception); 515 516 try { 517 String project = "project-6911"; 518 String healthCheck = "healthCheck-7822"; 519 HealthCheck healthCheckResource = HealthCheck.newBuilder().build(); 520 client.updateAsync(project, healthCheck, healthCheckResource).get(); 521 Assert.fail("No exception raised"); 522 } catch (ExecutionException e) { 523 } 524 } 525 } 526