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.gaming.v1; 18 19 import static com.google.cloud.gaming.v1.GameServerConfigsServiceClient.ListGameServerConfigsPagedResponse; 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.gaming.v1.stub.HttpJsonGameServerConfigsServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.longrunning.Operation; 33 import com.google.protobuf.Any; 34 import com.google.protobuf.Empty; 35 import com.google.protobuf.Timestamp; 36 import java.io.IOException; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.HashMap; 40 import java.util.List; 41 import java.util.concurrent.ExecutionException; 42 import javax.annotation.Generated; 43 import org.junit.After; 44 import org.junit.AfterClass; 45 import org.junit.Assert; 46 import org.junit.Before; 47 import org.junit.BeforeClass; 48 import org.junit.Test; 49 50 @Generated("by gapic-generator-java") 51 public class GameServerConfigsServiceClientHttpJsonTest { 52 private static MockHttpService mockService; 53 private static GameServerConfigsServiceClient client; 54 55 @BeforeClass startStaticServer()56 public static void startStaticServer() throws IOException { 57 mockService = 58 new MockHttpService( 59 HttpJsonGameServerConfigsServiceStub.getMethodDescriptors(), 60 GameServerConfigsServiceSettings.getDefaultEndpoint()); 61 GameServerConfigsServiceSettings settings = 62 GameServerConfigsServiceSettings.newHttpJsonBuilder() 63 .setTransportChannelProvider( 64 GameServerConfigsServiceSettings.defaultHttpJsonTransportProviderBuilder() 65 .setHttpTransport(mockService) 66 .build()) 67 .setCredentialsProvider(NoCredentialsProvider.create()) 68 .build(); 69 client = GameServerConfigsServiceClient.create(settings); 70 } 71 72 @AfterClass stopServer()73 public static void stopServer() { 74 client.close(); 75 } 76 77 @Before setUp()78 public void setUp() {} 79 80 @After tearDown()81 public void tearDown() throws Exception { 82 mockService.reset(); 83 } 84 85 @Test listGameServerConfigsTest()86 public void listGameServerConfigsTest() throws Exception { 87 GameServerConfig responsesElement = GameServerConfig.newBuilder().build(); 88 ListGameServerConfigsResponse expectedResponse = 89 ListGameServerConfigsResponse.newBuilder() 90 .setNextPageToken("") 91 .addAllGameServerConfigs(Arrays.asList(responsesElement)) 92 .build(); 93 mockService.addResponse(expectedResponse); 94 95 GameServerDeploymentName parent = 96 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 97 98 ListGameServerConfigsPagedResponse pagedListResponse = client.listGameServerConfigs(parent); 99 100 List<GameServerConfig> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 101 102 Assert.assertEquals(1, resources.size()); 103 Assert.assertEquals(expectedResponse.getGameServerConfigsList().get(0), 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 listGameServerConfigsExceptionTest()121 public void listGameServerConfigsExceptionTest() 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 GameServerDeploymentName parent = 129 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 130 client.listGameServerConfigs(parent); 131 Assert.fail("No exception raised"); 132 } catch (InvalidArgumentException e) { 133 // Expected exception. 134 } 135 } 136 137 @Test listGameServerConfigsTest2()138 public void listGameServerConfigsTest2() throws Exception { 139 GameServerConfig responsesElement = GameServerConfig.newBuilder().build(); 140 ListGameServerConfigsResponse expectedResponse = 141 ListGameServerConfigsResponse.newBuilder() 142 .setNextPageToken("") 143 .addAllGameServerConfigs(Arrays.asList(responsesElement)) 144 .build(); 145 mockService.addResponse(expectedResponse); 146 147 String parent = 148 "projects/project-6046/locations/location-6046/gameServerDeployments/gameServerDeployment-6046"; 149 150 ListGameServerConfigsPagedResponse pagedListResponse = client.listGameServerConfigs(parent); 151 152 List<GameServerConfig> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 153 154 Assert.assertEquals(1, resources.size()); 155 Assert.assertEquals(expectedResponse.getGameServerConfigsList().get(0), resources.get(0)); 156 157 List<String> actualRequests = mockService.getRequestPaths(); 158 Assert.assertEquals(1, actualRequests.size()); 159 160 String apiClientHeaderKey = 161 mockService 162 .getRequestHeaders() 163 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 164 .iterator() 165 .next(); 166 Assert.assertTrue( 167 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 168 .matcher(apiClientHeaderKey) 169 .matches()); 170 } 171 172 @Test listGameServerConfigsExceptionTest2()173 public void listGameServerConfigsExceptionTest2() throws Exception { 174 ApiException exception = 175 ApiExceptionFactory.createException( 176 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 177 mockService.addException(exception); 178 179 try { 180 String parent = 181 "projects/project-6046/locations/location-6046/gameServerDeployments/gameServerDeployment-6046"; 182 client.listGameServerConfigs(parent); 183 Assert.fail("No exception raised"); 184 } catch (InvalidArgumentException e) { 185 // Expected exception. 186 } 187 } 188 189 @Test getGameServerConfigTest()190 public void getGameServerConfigTest() throws Exception { 191 GameServerConfig expectedResponse = 192 GameServerConfig.newBuilder() 193 .setName( 194 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 195 .toString()) 196 .setCreateTime(Timestamp.newBuilder().build()) 197 .setUpdateTime(Timestamp.newBuilder().build()) 198 .putAllLabels(new HashMap<String, String>()) 199 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 200 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 201 .setDescription("description-1724546052") 202 .build(); 203 mockService.addResponse(expectedResponse); 204 205 GameServerConfigName name = 206 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 207 208 GameServerConfig actualResponse = client.getGameServerConfig(name); 209 Assert.assertEquals(expectedResponse, actualResponse); 210 211 List<String> actualRequests = mockService.getRequestPaths(); 212 Assert.assertEquals(1, actualRequests.size()); 213 214 String apiClientHeaderKey = 215 mockService 216 .getRequestHeaders() 217 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 218 .iterator() 219 .next(); 220 Assert.assertTrue( 221 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 222 .matcher(apiClientHeaderKey) 223 .matches()); 224 } 225 226 @Test getGameServerConfigExceptionTest()227 public void getGameServerConfigExceptionTest() throws Exception { 228 ApiException exception = 229 ApiExceptionFactory.createException( 230 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 231 mockService.addException(exception); 232 233 try { 234 GameServerConfigName name = 235 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 236 client.getGameServerConfig(name); 237 Assert.fail("No exception raised"); 238 } catch (InvalidArgumentException e) { 239 // Expected exception. 240 } 241 } 242 243 @Test getGameServerConfigTest2()244 public void getGameServerConfigTest2() throws Exception { 245 GameServerConfig expectedResponse = 246 GameServerConfig.newBuilder() 247 .setName( 248 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 249 .toString()) 250 .setCreateTime(Timestamp.newBuilder().build()) 251 .setUpdateTime(Timestamp.newBuilder().build()) 252 .putAllLabels(new HashMap<String, String>()) 253 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 254 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 255 .setDescription("description-1724546052") 256 .build(); 257 mockService.addResponse(expectedResponse); 258 259 String name = 260 "projects/project-2748/locations/location-2748/gameServerDeployments/gameServerDeployment-2748/configs/config-2748"; 261 262 GameServerConfig actualResponse = client.getGameServerConfig(name); 263 Assert.assertEquals(expectedResponse, actualResponse); 264 265 List<String> actualRequests = mockService.getRequestPaths(); 266 Assert.assertEquals(1, actualRequests.size()); 267 268 String apiClientHeaderKey = 269 mockService 270 .getRequestHeaders() 271 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 272 .iterator() 273 .next(); 274 Assert.assertTrue( 275 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 276 .matcher(apiClientHeaderKey) 277 .matches()); 278 } 279 280 @Test getGameServerConfigExceptionTest2()281 public void getGameServerConfigExceptionTest2() throws Exception { 282 ApiException exception = 283 ApiExceptionFactory.createException( 284 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 285 mockService.addException(exception); 286 287 try { 288 String name = 289 "projects/project-2748/locations/location-2748/gameServerDeployments/gameServerDeployment-2748/configs/config-2748"; 290 client.getGameServerConfig(name); 291 Assert.fail("No exception raised"); 292 } catch (InvalidArgumentException e) { 293 // Expected exception. 294 } 295 } 296 297 @Test createGameServerConfigTest()298 public void createGameServerConfigTest() throws Exception { 299 GameServerConfig expectedResponse = 300 GameServerConfig.newBuilder() 301 .setName( 302 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 303 .toString()) 304 .setCreateTime(Timestamp.newBuilder().build()) 305 .setUpdateTime(Timestamp.newBuilder().build()) 306 .putAllLabels(new HashMap<String, String>()) 307 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 308 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 309 .setDescription("description-1724546052") 310 .build(); 311 Operation resultOperation = 312 Operation.newBuilder() 313 .setName("createGameServerConfigTest") 314 .setDone(true) 315 .setResponse(Any.pack(expectedResponse)) 316 .build(); 317 mockService.addResponse(resultOperation); 318 319 GameServerDeploymentName parent = 320 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 321 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 322 323 GameServerConfig actualResponse = 324 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 325 Assert.assertEquals(expectedResponse, actualResponse); 326 327 List<String> actualRequests = mockService.getRequestPaths(); 328 Assert.assertEquals(1, actualRequests.size()); 329 330 String apiClientHeaderKey = 331 mockService 332 .getRequestHeaders() 333 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 334 .iterator() 335 .next(); 336 Assert.assertTrue( 337 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 338 .matcher(apiClientHeaderKey) 339 .matches()); 340 } 341 342 @Test createGameServerConfigExceptionTest()343 public void createGameServerConfigExceptionTest() throws Exception { 344 ApiException exception = 345 ApiExceptionFactory.createException( 346 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 347 mockService.addException(exception); 348 349 try { 350 GameServerDeploymentName parent = 351 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 352 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 353 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 354 Assert.fail("No exception raised"); 355 } catch (ExecutionException e) { 356 } 357 } 358 359 @Test createGameServerConfigTest2()360 public void createGameServerConfigTest2() throws Exception { 361 GameServerConfig expectedResponse = 362 GameServerConfig.newBuilder() 363 .setName( 364 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 365 .toString()) 366 .setCreateTime(Timestamp.newBuilder().build()) 367 .setUpdateTime(Timestamp.newBuilder().build()) 368 .putAllLabels(new HashMap<String, String>()) 369 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 370 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 371 .setDescription("description-1724546052") 372 .build(); 373 Operation resultOperation = 374 Operation.newBuilder() 375 .setName("createGameServerConfigTest") 376 .setDone(true) 377 .setResponse(Any.pack(expectedResponse)) 378 .build(); 379 mockService.addResponse(resultOperation); 380 381 String parent = 382 "projects/project-6046/locations/location-6046/gameServerDeployments/gameServerDeployment-6046"; 383 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 384 385 GameServerConfig actualResponse = 386 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 387 Assert.assertEquals(expectedResponse, actualResponse); 388 389 List<String> actualRequests = mockService.getRequestPaths(); 390 Assert.assertEquals(1, actualRequests.size()); 391 392 String apiClientHeaderKey = 393 mockService 394 .getRequestHeaders() 395 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 396 .iterator() 397 .next(); 398 Assert.assertTrue( 399 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 400 .matcher(apiClientHeaderKey) 401 .matches()); 402 } 403 404 @Test createGameServerConfigExceptionTest2()405 public void createGameServerConfigExceptionTest2() throws Exception { 406 ApiException exception = 407 ApiExceptionFactory.createException( 408 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 409 mockService.addException(exception); 410 411 try { 412 String parent = 413 "projects/project-6046/locations/location-6046/gameServerDeployments/gameServerDeployment-6046"; 414 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 415 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 416 Assert.fail("No exception raised"); 417 } catch (ExecutionException e) { 418 } 419 } 420 421 @Test deleteGameServerConfigTest()422 public void deleteGameServerConfigTest() throws Exception { 423 Empty expectedResponse = Empty.newBuilder().build(); 424 Operation resultOperation = 425 Operation.newBuilder() 426 .setName("deleteGameServerConfigTest") 427 .setDone(true) 428 .setResponse(Any.pack(expectedResponse)) 429 .build(); 430 mockService.addResponse(resultOperation); 431 432 GameServerConfigName name = 433 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 434 435 client.deleteGameServerConfigAsync(name).get(); 436 437 List<String> actualRequests = mockService.getRequestPaths(); 438 Assert.assertEquals(1, actualRequests.size()); 439 440 String apiClientHeaderKey = 441 mockService 442 .getRequestHeaders() 443 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 444 .iterator() 445 .next(); 446 Assert.assertTrue( 447 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 448 .matcher(apiClientHeaderKey) 449 .matches()); 450 } 451 452 @Test deleteGameServerConfigExceptionTest()453 public void deleteGameServerConfigExceptionTest() throws Exception { 454 ApiException exception = 455 ApiExceptionFactory.createException( 456 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 457 mockService.addException(exception); 458 459 try { 460 GameServerConfigName name = 461 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 462 client.deleteGameServerConfigAsync(name).get(); 463 Assert.fail("No exception raised"); 464 } catch (ExecutionException e) { 465 } 466 } 467 468 @Test deleteGameServerConfigTest2()469 public void deleteGameServerConfigTest2() throws Exception { 470 Empty expectedResponse = Empty.newBuilder().build(); 471 Operation resultOperation = 472 Operation.newBuilder() 473 .setName("deleteGameServerConfigTest") 474 .setDone(true) 475 .setResponse(Any.pack(expectedResponse)) 476 .build(); 477 mockService.addResponse(resultOperation); 478 479 String name = 480 "projects/project-2748/locations/location-2748/gameServerDeployments/gameServerDeployment-2748/configs/config-2748"; 481 482 client.deleteGameServerConfigAsync(name).get(); 483 484 List<String> actualRequests = mockService.getRequestPaths(); 485 Assert.assertEquals(1, actualRequests.size()); 486 487 String apiClientHeaderKey = 488 mockService 489 .getRequestHeaders() 490 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 491 .iterator() 492 .next(); 493 Assert.assertTrue( 494 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 495 .matcher(apiClientHeaderKey) 496 .matches()); 497 } 498 499 @Test deleteGameServerConfigExceptionTest2()500 public void deleteGameServerConfigExceptionTest2() throws Exception { 501 ApiException exception = 502 ApiExceptionFactory.createException( 503 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 504 mockService.addException(exception); 505 506 try { 507 String name = 508 "projects/project-2748/locations/location-2748/gameServerDeployments/gameServerDeployment-2748/configs/config-2748"; 509 client.deleteGameServerConfigAsync(name).get(); 510 Assert.fail("No exception raised"); 511 } catch (ExecutionException e) { 512 } 513 } 514 } 515