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.GlobalAddressesClient.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.HttpJsonGlobalAddressesStub; 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.List; 37 import java.util.concurrent.ExecutionException; 38 import javax.annotation.Generated; 39 import org.junit.After; 40 import org.junit.AfterClass; 41 import org.junit.Assert; 42 import org.junit.Before; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 @Generated("by gapic-generator-java") 47 public class GlobalAddressesClientTest { 48 private static MockHttpService mockService; 49 private static GlobalAddressesClient client; 50 51 @BeforeClass startStaticServer()52 public static void startStaticServer() throws IOException { 53 mockService = 54 new MockHttpService( 55 HttpJsonGlobalAddressesStub.getMethodDescriptors(), 56 GlobalAddressesSettings.getDefaultEndpoint()); 57 GlobalAddressesSettings settings = 58 GlobalAddressesSettings.newBuilder() 59 .setTransportChannelProvider( 60 GlobalAddressesSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = GlobalAddressesClient.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 address = "address-4954"; 114 115 Operation actualResponse = client.deleteAsync(project, address).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 address = "address-4954"; 143 client.deleteAsync(project, address).get(); 144 Assert.fail("No exception raised"); 145 } catch (ExecutionException e) { 146 } 147 } 148 149 @Test getTest()150 public void getTest() throws Exception { 151 Address expectedResponse = 152 Address.newBuilder() 153 .setAddress("address-1147692044") 154 .setAddressType("addressType-1377270450") 155 .setCreationTimestamp("creationTimestamp-370203401") 156 .setDescription("description-1724546052") 157 .setId(3355) 158 .setIpVersion("ipVersion-1161634383") 159 .setIpv6EndpointType("ipv6EndpointType399011318") 160 .setKind("kind3292052") 161 .setName("name3373707") 162 .setNetwork("network1843485230") 163 .setNetworkTier("networkTier-1940629200") 164 .setPrefixLength(-1157046989) 165 .setPurpose("purpose-220463842") 166 .setRegion("region-934795532") 167 .setSelfLink("selfLink1191800166") 168 .setStatus("status-892481550") 169 .setSubnetwork("subnetwork-1302785042") 170 .addAllUsers(new ArrayList<String>()) 171 .build(); 172 mockService.addResponse(expectedResponse); 173 174 String project = "project-6911"; 175 String address = "address-4954"; 176 177 Address actualResponse = client.get(project, address); 178 Assert.assertEquals(expectedResponse, actualResponse); 179 180 List<String> actualRequests = mockService.getRequestPaths(); 181 Assert.assertEquals(1, actualRequests.size()); 182 183 String apiClientHeaderKey = 184 mockService 185 .getRequestHeaders() 186 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 187 .iterator() 188 .next(); 189 Assert.assertTrue( 190 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 191 .matcher(apiClientHeaderKey) 192 .matches()); 193 } 194 195 @Test getExceptionTest()196 public void getExceptionTest() throws Exception { 197 ApiException exception = 198 ApiExceptionFactory.createException( 199 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 200 mockService.addException(exception); 201 202 try { 203 String project = "project-6911"; 204 String address = "address-4954"; 205 client.get(project, address); 206 Assert.fail("No exception raised"); 207 } catch (InvalidArgumentException e) { 208 // Expected exception. 209 } 210 } 211 212 @Test insertTest()213 public void insertTest() throws Exception { 214 Operation expectedResponse = 215 Operation.newBuilder() 216 .setClientOperationId("clientOperationId-1230366697") 217 .setCreationTimestamp("creationTimestamp-370203401") 218 .setDescription("description-1724546052") 219 .setEndTime("endTime-1607243192") 220 .setError(Error.newBuilder().build()) 221 .setHttpErrorMessage("httpErrorMessage1577303431") 222 .setHttpErrorStatusCode(0) 223 .setId(3355) 224 .setInsertTime("insertTime966165798") 225 .setKind("kind3292052") 226 .setName("name3373707") 227 .setOperationGroupId("operationGroupId1716161683") 228 .setOperationType("operationType91999553") 229 .setProgress(-1001078227) 230 .setRegion("region-934795532") 231 .setSelfLink("selfLink1191800166") 232 .setStartTime("startTime-2129294769") 233 .setStatus(Status.DONE) 234 .setStatusMessage("statusMessage-958704715") 235 .setTargetId(-815576439) 236 .setTargetLink("targetLink486368555") 237 .setUser("user3599307") 238 .addAllWarnings(new ArrayList<Warnings>()) 239 .setZone("zone3744684") 240 .build(); 241 mockService.addResponse(expectedResponse); 242 243 String project = "project-6911"; 244 Address addressResource = Address.newBuilder().build(); 245 246 Operation actualResponse = client.insertAsync(project, addressResource).get(); 247 Assert.assertEquals(expectedResponse, actualResponse); 248 249 List<String> actualRequests = mockService.getRequestPaths(); 250 Assert.assertEquals(1, actualRequests.size()); 251 252 String apiClientHeaderKey = 253 mockService 254 .getRequestHeaders() 255 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 256 .iterator() 257 .next(); 258 Assert.assertTrue( 259 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 260 .matcher(apiClientHeaderKey) 261 .matches()); 262 } 263 264 @Test insertExceptionTest()265 public void insertExceptionTest() throws Exception { 266 ApiException exception = 267 ApiExceptionFactory.createException( 268 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 269 mockService.addException(exception); 270 271 try { 272 String project = "project-6911"; 273 Address addressResource = Address.newBuilder().build(); 274 client.insertAsync(project, addressResource).get(); 275 Assert.fail("No exception raised"); 276 } catch (ExecutionException e) { 277 } 278 } 279 280 @Test listTest()281 public void listTest() throws Exception { 282 Address responsesElement = Address.newBuilder().build(); 283 AddressList expectedResponse = 284 AddressList.newBuilder() 285 .setNextPageToken("") 286 .addAllItems(Arrays.asList(responsesElement)) 287 .build(); 288 mockService.addResponse(expectedResponse); 289 290 String project = "project-6911"; 291 292 ListPagedResponse pagedListResponse = client.list(project); 293 294 List<Address> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 295 296 Assert.assertEquals(1, resources.size()); 297 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 298 299 List<String> actualRequests = mockService.getRequestPaths(); 300 Assert.assertEquals(1, actualRequests.size()); 301 302 String apiClientHeaderKey = 303 mockService 304 .getRequestHeaders() 305 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 306 .iterator() 307 .next(); 308 Assert.assertTrue( 309 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 310 .matcher(apiClientHeaderKey) 311 .matches()); 312 } 313 314 @Test listExceptionTest()315 public void listExceptionTest() throws Exception { 316 ApiException exception = 317 ApiExceptionFactory.createException( 318 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 319 mockService.addException(exception); 320 321 try { 322 String project = "project-6911"; 323 client.list(project); 324 Assert.fail("No exception raised"); 325 } catch (InvalidArgumentException e) { 326 // Expected exception. 327 } 328 } 329 330 @Test setLabelsTest()331 public void setLabelsTest() throws Exception { 332 Operation expectedResponse = 333 Operation.newBuilder() 334 .setClientOperationId("clientOperationId-1230366697") 335 .setCreationTimestamp("creationTimestamp-370203401") 336 .setDescription("description-1724546052") 337 .setEndTime("endTime-1607243192") 338 .setError(Error.newBuilder().build()) 339 .setHttpErrorMessage("httpErrorMessage1577303431") 340 .setHttpErrorStatusCode(0) 341 .setId(3355) 342 .setInsertTime("insertTime966165798") 343 .setKind("kind3292052") 344 .setName("name3373707") 345 .setOperationGroupId("operationGroupId1716161683") 346 .setOperationType("operationType91999553") 347 .setProgress(-1001078227) 348 .setRegion("region-934795532") 349 .setSelfLink("selfLink1191800166") 350 .setStartTime("startTime-2129294769") 351 .setStatus(Status.DONE) 352 .setStatusMessage("statusMessage-958704715") 353 .setTargetId(-815576439) 354 .setTargetLink("targetLink486368555") 355 .setUser("user3599307") 356 .addAllWarnings(new ArrayList<Warnings>()) 357 .setZone("zone3744684") 358 .build(); 359 mockService.addResponse(expectedResponse); 360 361 String project = "project-6911"; 362 String resource = "resource-756"; 363 GlobalSetLabelsRequest globalSetLabelsRequestResource = 364 GlobalSetLabelsRequest.newBuilder().build(); 365 366 Operation actualResponse = 367 client.setLabelsAsync(project, resource, globalSetLabelsRequestResource).get(); 368 Assert.assertEquals(expectedResponse, actualResponse); 369 370 List<String> actualRequests = mockService.getRequestPaths(); 371 Assert.assertEquals(1, actualRequests.size()); 372 373 String apiClientHeaderKey = 374 mockService 375 .getRequestHeaders() 376 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 377 .iterator() 378 .next(); 379 Assert.assertTrue( 380 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 381 .matcher(apiClientHeaderKey) 382 .matches()); 383 } 384 385 @Test setLabelsExceptionTest()386 public void setLabelsExceptionTest() throws Exception { 387 ApiException exception = 388 ApiExceptionFactory.createException( 389 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 390 mockService.addException(exception); 391 392 try { 393 String project = "project-6911"; 394 String resource = "resource-756"; 395 GlobalSetLabelsRequest globalSetLabelsRequestResource = 396 GlobalSetLabelsRequest.newBuilder().build(); 397 client.setLabelsAsync(project, resource, globalSetLabelsRequestResource).get(); 398 Assert.fail("No exception raised"); 399 } catch (ExecutionException e) { 400 } 401 } 402 } 403