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.GlobalOperationsClient.AggregatedListPagedResponse; 20 import static com.google.cloud.compute.v1.GlobalOperationsClient.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.HttpJsonGlobalOperationsStub; 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 javax.annotation.Generated; 41 import org.junit.After; 42 import org.junit.AfterClass; 43 import org.junit.Assert; 44 import org.junit.Before; 45 import org.junit.BeforeClass; 46 import org.junit.Test; 47 48 @Generated("by gapic-generator-java") 49 public class GlobalOperationsClientTest { 50 private static MockHttpService mockService; 51 private static GlobalOperationsClient client; 52 53 @BeforeClass startStaticServer()54 public static void startStaticServer() throws IOException { 55 mockService = 56 new MockHttpService( 57 HttpJsonGlobalOperationsStub.getMethodDescriptors(), 58 GlobalOperationsSettings.getDefaultEndpoint()); 59 GlobalOperationsSettings settings = 60 GlobalOperationsSettings.newBuilder() 61 .setTransportChannelProvider( 62 GlobalOperationsSettings.defaultHttpJsonTransportProviderBuilder() 63 .setHttpTransport(mockService) 64 .build()) 65 .setCredentialsProvider(NoCredentialsProvider.create()) 66 .build(); 67 client = GlobalOperationsClient.create(settings); 68 } 69 70 @AfterClass stopServer()71 public static void stopServer() { 72 client.close(); 73 } 74 75 @Before setUp()76 public void setUp() {} 77 78 @After tearDown()79 public void tearDown() throws Exception { 80 mockService.reset(); 81 } 82 83 @Test aggregatedListTest()84 public void aggregatedListTest() throws Exception { 85 OperationsScopedList responsesElement = OperationsScopedList.newBuilder().build(); 86 OperationAggregatedList expectedResponse = 87 OperationAggregatedList.newBuilder() 88 .setNextPageToken("") 89 .putAllItems(Collections.singletonMap("items", responsesElement)) 90 .build(); 91 mockService.addResponse(expectedResponse); 92 93 String project = "project-6911"; 94 95 AggregatedListPagedResponse pagedListResponse = client.aggregatedList(project); 96 97 List<Map.Entry<String, OperationsScopedList>> resources = 98 Lists.newArrayList(pagedListResponse.iterateAll()); 99 100 Assert.assertEquals(1, resources.size()); 101 Assert.assertEquals( 102 expectedResponse.getItemsMap().entrySet().iterator().next(), resources.get(0)); 103 104 List<String> actualRequests = mockService.getRequestPaths(); 105 Assert.assertEquals(1, actualRequests.size()); 106 107 String apiClientHeaderKey = 108 mockService 109 .getRequestHeaders() 110 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 111 .iterator() 112 .next(); 113 Assert.assertTrue( 114 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 115 .matcher(apiClientHeaderKey) 116 .matches()); 117 } 118 119 @Test aggregatedListExceptionTest()120 public void aggregatedListExceptionTest() throws Exception { 121 ApiException exception = 122 ApiExceptionFactory.createException( 123 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 124 mockService.addException(exception); 125 126 try { 127 String project = "project-6911"; 128 client.aggregatedList(project); 129 Assert.fail("No exception raised"); 130 } catch (InvalidArgumentException e) { 131 // Expected exception. 132 } 133 } 134 135 @Test deleteTest()136 public void deleteTest() throws Exception { 137 DeleteGlobalOperationResponse expectedResponse = 138 DeleteGlobalOperationResponse.newBuilder().build(); 139 mockService.addResponse(expectedResponse); 140 141 String project = "project-6911"; 142 String operation = "operation-3971"; 143 144 DeleteGlobalOperationResponse actualResponse = client.delete(project, operation); 145 Assert.assertEquals(expectedResponse, actualResponse); 146 147 List<String> actualRequests = mockService.getRequestPaths(); 148 Assert.assertEquals(1, actualRequests.size()); 149 150 String apiClientHeaderKey = 151 mockService 152 .getRequestHeaders() 153 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 154 .iterator() 155 .next(); 156 Assert.assertTrue( 157 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 158 .matcher(apiClientHeaderKey) 159 .matches()); 160 } 161 162 @Test deleteExceptionTest()163 public void deleteExceptionTest() throws Exception { 164 ApiException exception = 165 ApiExceptionFactory.createException( 166 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 167 mockService.addException(exception); 168 169 try { 170 String project = "project-6911"; 171 String operation = "operation-3971"; 172 client.delete(project, operation); 173 Assert.fail("No exception raised"); 174 } catch (InvalidArgumentException e) { 175 // Expected exception. 176 } 177 } 178 179 @Test getTest()180 public void getTest() throws Exception { 181 Operation expectedResponse = 182 Operation.newBuilder() 183 .setClientOperationId("clientOperationId-1230366697") 184 .setCreationTimestamp("creationTimestamp-370203401") 185 .setDescription("description-1724546052") 186 .setEndTime("endTime-1607243192") 187 .setError(Error.newBuilder().build()) 188 .setHttpErrorMessage("httpErrorMessage1577303431") 189 .setHttpErrorStatusCode(0) 190 .setId(3355) 191 .setInsertTime("insertTime966165798") 192 .setKind("kind3292052") 193 .setName("name3373707") 194 .setOperationGroupId("operationGroupId1716161683") 195 .setOperationType("operationType91999553") 196 .setProgress(-1001078227) 197 .setRegion("region-934795532") 198 .setSelfLink("selfLink1191800166") 199 .setStartTime("startTime-2129294769") 200 .setStatus(Status.DONE) 201 .setStatusMessage("statusMessage-958704715") 202 .setTargetId(-815576439) 203 .setTargetLink("targetLink486368555") 204 .setUser("user3599307") 205 .addAllWarnings(new ArrayList<Warnings>()) 206 .setZone("zone3744684") 207 .build(); 208 mockService.addResponse(expectedResponse); 209 210 String project = "project-6911"; 211 String operation = "operation-3971"; 212 213 Operation actualResponse = client.get(project, operation); 214 Assert.assertEquals(expectedResponse, actualResponse); 215 216 List<String> actualRequests = mockService.getRequestPaths(); 217 Assert.assertEquals(1, actualRequests.size()); 218 219 String apiClientHeaderKey = 220 mockService 221 .getRequestHeaders() 222 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 223 .iterator() 224 .next(); 225 Assert.assertTrue( 226 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 227 .matcher(apiClientHeaderKey) 228 .matches()); 229 } 230 231 @Test getExceptionTest()232 public void getExceptionTest() throws Exception { 233 ApiException exception = 234 ApiExceptionFactory.createException( 235 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 236 mockService.addException(exception); 237 238 try { 239 String project = "project-6911"; 240 String operation = "operation-3971"; 241 client.get(project, operation); 242 Assert.fail("No exception raised"); 243 } catch (InvalidArgumentException e) { 244 // Expected exception. 245 } 246 } 247 248 @Test listTest()249 public void listTest() throws Exception { 250 Operation responsesElement = Operation.newBuilder().build(); 251 OperationList expectedResponse = 252 OperationList.newBuilder() 253 .setNextPageToken("") 254 .addAllItems(Arrays.asList(responsesElement)) 255 .build(); 256 mockService.addResponse(expectedResponse); 257 258 String project = "project-6911"; 259 260 ListPagedResponse pagedListResponse = client.list(project); 261 262 List<Operation> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 263 264 Assert.assertEquals(1, resources.size()); 265 Assert.assertEquals(expectedResponse.getItemsList().get(0), resources.get(0)); 266 267 List<String> actualRequests = mockService.getRequestPaths(); 268 Assert.assertEquals(1, actualRequests.size()); 269 270 String apiClientHeaderKey = 271 mockService 272 .getRequestHeaders() 273 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 274 .iterator() 275 .next(); 276 Assert.assertTrue( 277 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 278 .matcher(apiClientHeaderKey) 279 .matches()); 280 } 281 282 @Test listExceptionTest()283 public void listExceptionTest() throws Exception { 284 ApiException exception = 285 ApiExceptionFactory.createException( 286 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 287 mockService.addException(exception); 288 289 try { 290 String project = "project-6911"; 291 client.list(project); 292 Assert.fail("No exception raised"); 293 } catch (InvalidArgumentException e) { 294 // Expected exception. 295 } 296 } 297 298 @Test waitTest()299 public void waitTest() throws Exception { 300 Operation expectedResponse = 301 Operation.newBuilder() 302 .setClientOperationId("clientOperationId-1230366697") 303 .setCreationTimestamp("creationTimestamp-370203401") 304 .setDescription("description-1724546052") 305 .setEndTime("endTime-1607243192") 306 .setError(Error.newBuilder().build()) 307 .setHttpErrorMessage("httpErrorMessage1577303431") 308 .setHttpErrorStatusCode(0) 309 .setId(3355) 310 .setInsertTime("insertTime966165798") 311 .setKind("kind3292052") 312 .setName("name3373707") 313 .setOperationGroupId("operationGroupId1716161683") 314 .setOperationType("operationType91999553") 315 .setProgress(-1001078227) 316 .setRegion("region-934795532") 317 .setSelfLink("selfLink1191800166") 318 .setStartTime("startTime-2129294769") 319 .setStatus(Status.DONE) 320 .setStatusMessage("statusMessage-958704715") 321 .setTargetId(-815576439) 322 .setTargetLink("targetLink486368555") 323 .setUser("user3599307") 324 .addAllWarnings(new ArrayList<Warnings>()) 325 .setZone("zone3744684") 326 .build(); 327 mockService.addResponse(expectedResponse); 328 329 String project = "project-6911"; 330 String operation = "operation-3971"; 331 332 Operation actualResponse = client.wait(project, operation); 333 Assert.assertEquals(expectedResponse, actualResponse); 334 335 List<String> actualRequests = mockService.getRequestPaths(); 336 Assert.assertEquals(1, actualRequests.size()); 337 338 String apiClientHeaderKey = 339 mockService 340 .getRequestHeaders() 341 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 342 .iterator() 343 .next(); 344 Assert.assertTrue( 345 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 346 .matcher(apiClientHeaderKey) 347 .matches()); 348 } 349 350 @Test waitExceptionTest()351 public void waitExceptionTest() throws Exception { 352 ApiException exception = 353 ApiExceptionFactory.createException( 354 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 355 mockService.addException(exception); 356 357 try { 358 String project = "project-6911"; 359 String operation = "operation-3971"; 360 client.wait(project, operation); 361 Assert.fail("No exception raised"); 362 } catch (InvalidArgumentException e) { 363 // Expected exception. 364 } 365 } 366 } 367