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.TargetTcpProxiesClient.AggregatedListPagedResponse; 20 import static com.google.cloud.compute.v1.TargetTcpProxiesClient.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.HttpJsonTargetTcpProxiesStub; 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 TargetTcpProxiesClientTest { 51 private static MockHttpService mockService; 52 private static TargetTcpProxiesClient client; 53 54 @BeforeClass startStaticServer()55 public static void startStaticServer() throws IOException { 56 mockService = 57 new MockHttpService( 58 HttpJsonTargetTcpProxiesStub.getMethodDescriptors(), 59 TargetTcpProxiesSettings.getDefaultEndpoint()); 60 TargetTcpProxiesSettings settings = 61 TargetTcpProxiesSettings.newBuilder() 62 .setTransportChannelProvider( 63 TargetTcpProxiesSettings.defaultHttpJsonTransportProviderBuilder() 64 .setHttpTransport(mockService) 65 .build()) 66 .setCredentialsProvider(NoCredentialsProvider.create()) 67 .build(); 68 client = TargetTcpProxiesClient.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 TargetTcpProxiesScopedList responsesElement = TargetTcpProxiesScopedList.newBuilder().build(); 87 TargetTcpProxyAggregatedList expectedResponse = 88 TargetTcpProxyAggregatedList.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, TargetTcpProxiesScopedList>> 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 targetTcpProxy = "targetTcpProxy-7204"; 169 170 Operation actualResponse = client.deleteAsync(project, targetTcpProxy).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 targetTcpProxy = "targetTcpProxy-7204"; 198 client.deleteAsync(project, targetTcpProxy).get(); 199 Assert.fail("No exception raised"); 200 } catch (ExecutionException e) { 201 } 202 } 203 204 @Test getTest()205 public void getTest() throws Exception { 206 TargetTcpProxy expectedResponse = 207 TargetTcpProxy.newBuilder() 208 .setCreationTimestamp("creationTimestamp-370203401") 209 .setDescription("description-1724546052") 210 .setId(3355) 211 .setKind("kind3292052") 212 .setName("name3373707") 213 .setProxyBind(true) 214 .setProxyHeader("proxyHeader1768394395") 215 .setRegion("region-934795532") 216 .setSelfLink("selfLink1191800166") 217 .setService("service1984153269") 218 .build(); 219 mockService.addResponse(expectedResponse); 220 221 String project = "project-6911"; 222 String targetTcpProxy = "targetTcpProxy-7204"; 223 224 TargetTcpProxy actualResponse = client.get(project, targetTcpProxy); 225 Assert.assertEquals(expectedResponse, actualResponse); 226 227 List<String> actualRequests = mockService.getRequestPaths(); 228 Assert.assertEquals(1, actualRequests.size()); 229 230 String apiClientHeaderKey = 231 mockService 232 .getRequestHeaders() 233 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 234 .iterator() 235 .next(); 236 Assert.assertTrue( 237 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 238 .matcher(apiClientHeaderKey) 239 .matches()); 240 } 241 242 @Test getExceptionTest()243 public void getExceptionTest() throws Exception { 244 ApiException exception = 245 ApiExceptionFactory.createException( 246 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 247 mockService.addException(exception); 248 249 try { 250 String project = "project-6911"; 251 String targetTcpProxy = "targetTcpProxy-7204"; 252 client.get(project, targetTcpProxy); 253 Assert.fail("No exception raised"); 254 } catch (InvalidArgumentException e) { 255 // Expected exception. 256 } 257 } 258 259 @Test insertTest()260 public void insertTest() throws Exception { 261 Operation expectedResponse = 262 Operation.newBuilder() 263 .setClientOperationId("clientOperationId-1230366697") 264 .setCreationTimestamp("creationTimestamp-370203401") 265 .setDescription("description-1724546052") 266 .setEndTime("endTime-1607243192") 267 .setError(Error.newBuilder().build()) 268 .setHttpErrorMessage("httpErrorMessage1577303431") 269 .setHttpErrorStatusCode(0) 270 .setId(3355) 271 .setInsertTime("insertTime966165798") 272 .setKind("kind3292052") 273 .setName("name3373707") 274 .setOperationGroupId("operationGroupId1716161683") 275 .setOperationType("operationType91999553") 276 .setProgress(-1001078227) 277 .setRegion("region-934795532") 278 .setSelfLink("selfLink1191800166") 279 .setStartTime("startTime-2129294769") 280 .setStatus(Status.DONE) 281 .setStatusMessage("statusMessage-958704715") 282 .setTargetId(-815576439) 283 .setTargetLink("targetLink486368555") 284 .setUser("user3599307") 285 .addAllWarnings(new ArrayList<Warnings>()) 286 .setZone("zone3744684") 287 .build(); 288 mockService.addResponse(expectedResponse); 289 290 String project = "project-6911"; 291 TargetTcpProxy targetTcpProxyResource = TargetTcpProxy.newBuilder().build(); 292 293 Operation actualResponse = client.insertAsync(project, targetTcpProxyResource).get(); 294 Assert.assertEquals(expectedResponse, actualResponse); 295 296 List<String> actualRequests = mockService.getRequestPaths(); 297 Assert.assertEquals(1, actualRequests.size()); 298 299 String apiClientHeaderKey = 300 mockService 301 .getRequestHeaders() 302 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 303 .iterator() 304 .next(); 305 Assert.assertTrue( 306 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 307 .matcher(apiClientHeaderKey) 308 .matches()); 309 } 310 311 @Test insertExceptionTest()312 public void insertExceptionTest() throws Exception { 313 ApiException exception = 314 ApiExceptionFactory.createException( 315 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 316 mockService.addException(exception); 317 318 try { 319 String project = "project-6911"; 320 TargetTcpProxy targetTcpProxyResource = TargetTcpProxy.newBuilder().build(); 321 client.insertAsync(project, targetTcpProxyResource).get(); 322 Assert.fail("No exception raised"); 323 } catch (ExecutionException e) { 324 } 325 } 326 327 @Test listTest()328 public void listTest() throws Exception { 329 TargetTcpProxy responsesElement = TargetTcpProxy.newBuilder().build(); 330 TargetTcpProxyList expectedResponse = 331 TargetTcpProxyList.newBuilder() 332 .setNextPageToken("") 333 .addAllItems(Arrays.asList(responsesElement)) 334 .build(); 335 mockService.addResponse(expectedResponse); 336 337 String project = "project-6911"; 338 339 ListPagedResponse pagedListResponse = client.list(project); 340 341 List<TargetTcpProxy> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 342 343 Assert.assertEquals(1, resources.size()); 344 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 345 346 List<String> actualRequests = mockService.getRequestPaths(); 347 Assert.assertEquals(1, actualRequests.size()); 348 349 String apiClientHeaderKey = 350 mockService 351 .getRequestHeaders() 352 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 353 .iterator() 354 .next(); 355 Assert.assertTrue( 356 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 357 .matcher(apiClientHeaderKey) 358 .matches()); 359 } 360 361 @Test listExceptionTest()362 public void listExceptionTest() throws Exception { 363 ApiException exception = 364 ApiExceptionFactory.createException( 365 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 366 mockService.addException(exception); 367 368 try { 369 String project = "project-6911"; 370 client.list(project); 371 Assert.fail("No exception raised"); 372 } catch (InvalidArgumentException e) { 373 // Expected exception. 374 } 375 } 376 377 @Test setBackendServiceTest()378 public void setBackendServiceTest() throws Exception { 379 Operation expectedResponse = 380 Operation.newBuilder() 381 .setClientOperationId("clientOperationId-1230366697") 382 .setCreationTimestamp("creationTimestamp-370203401") 383 .setDescription("description-1724546052") 384 .setEndTime("endTime-1607243192") 385 .setError(Error.newBuilder().build()) 386 .setHttpErrorMessage("httpErrorMessage1577303431") 387 .setHttpErrorStatusCode(0) 388 .setId(3355) 389 .setInsertTime("insertTime966165798") 390 .setKind("kind3292052") 391 .setName("name3373707") 392 .setOperationGroupId("operationGroupId1716161683") 393 .setOperationType("operationType91999553") 394 .setProgress(-1001078227) 395 .setRegion("region-934795532") 396 .setSelfLink("selfLink1191800166") 397 .setStartTime("startTime-2129294769") 398 .setStatus(Status.DONE) 399 .setStatusMessage("statusMessage-958704715") 400 .setTargetId(-815576439) 401 .setTargetLink("targetLink486368555") 402 .setUser("user3599307") 403 .addAllWarnings(new ArrayList<Warnings>()) 404 .setZone("zone3744684") 405 .build(); 406 mockService.addResponse(expectedResponse); 407 408 String project = "project-6911"; 409 String targetTcpProxy = "targetTcpProxy-7204"; 410 TargetTcpProxiesSetBackendServiceRequest targetTcpProxiesSetBackendServiceRequestResource = 411 TargetTcpProxiesSetBackendServiceRequest.newBuilder().build(); 412 413 Operation actualResponse = 414 client 415 .setBackendServiceAsync( 416 project, targetTcpProxy, targetTcpProxiesSetBackendServiceRequestResource) 417 .get(); 418 Assert.assertEquals(expectedResponse, actualResponse); 419 420 List<String> actualRequests = mockService.getRequestPaths(); 421 Assert.assertEquals(1, actualRequests.size()); 422 423 String apiClientHeaderKey = 424 mockService 425 .getRequestHeaders() 426 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 427 .iterator() 428 .next(); 429 Assert.assertTrue( 430 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 431 .matcher(apiClientHeaderKey) 432 .matches()); 433 } 434 435 @Test setBackendServiceExceptionTest()436 public void setBackendServiceExceptionTest() throws Exception { 437 ApiException exception = 438 ApiExceptionFactory.createException( 439 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 440 mockService.addException(exception); 441 442 try { 443 String project = "project-6911"; 444 String targetTcpProxy = "targetTcpProxy-7204"; 445 TargetTcpProxiesSetBackendServiceRequest targetTcpProxiesSetBackendServiceRequestResource = 446 TargetTcpProxiesSetBackendServiceRequest.newBuilder().build(); 447 client 448 .setBackendServiceAsync( 449 project, targetTcpProxy, targetTcpProxiesSetBackendServiceRequestResource) 450 .get(); 451 Assert.fail("No exception raised"); 452 } catch (ExecutionException e) { 453 } 454 } 455 456 @Test setProxyHeaderTest()457 public void setProxyHeaderTest() 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 targetTcpProxy = "targetTcpProxy-7204"; 489 TargetTcpProxiesSetProxyHeaderRequest targetTcpProxiesSetProxyHeaderRequestResource = 490 TargetTcpProxiesSetProxyHeaderRequest.newBuilder().build(); 491 492 Operation actualResponse = 493 client 494 .setProxyHeaderAsync( 495 project, targetTcpProxy, targetTcpProxiesSetProxyHeaderRequestResource) 496 .get(); 497 Assert.assertEquals(expectedResponse, actualResponse); 498 499 List<String> actualRequests = mockService.getRequestPaths(); 500 Assert.assertEquals(1, actualRequests.size()); 501 502 String apiClientHeaderKey = 503 mockService 504 .getRequestHeaders() 505 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 506 .iterator() 507 .next(); 508 Assert.assertTrue( 509 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 510 .matcher(apiClientHeaderKey) 511 .matches()); 512 } 513 514 @Test setProxyHeaderExceptionTest()515 public void setProxyHeaderExceptionTest() throws Exception { 516 ApiException exception = 517 ApiExceptionFactory.createException( 518 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 519 mockService.addException(exception); 520 521 try { 522 String project = "project-6911"; 523 String targetTcpProxy = "targetTcpProxy-7204"; 524 TargetTcpProxiesSetProxyHeaderRequest targetTcpProxiesSetProxyHeaderRequestResource = 525 TargetTcpProxiesSetProxyHeaderRequest.newBuilder().build(); 526 client 527 .setProxyHeaderAsync( 528 project, targetTcpProxy, targetTcpProxiesSetProxyHeaderRequestResource) 529 .get(); 530 Assert.fail("No exception raised"); 531 } catch (ExecutionException e) { 532 } 533 } 534 } 535