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.v1beta; 18 19 import static com.google.cloud.gaming.v1beta.GameServerConfigsServiceClient.ListGameServerConfigsPagedResponse; 20 21 import com.google.api.gax.core.NoCredentialsProvider; 22 import com.google.api.gax.grpc.GaxGrpcProperties; 23 import com.google.api.gax.grpc.testing.LocalChannelProvider; 24 import com.google.api.gax.grpc.testing.MockGrpcService; 25 import com.google.api.gax.grpc.testing.MockServiceHelper; 26 import com.google.api.gax.rpc.ApiClientHeaderProvider; 27 import com.google.api.gax.rpc.InvalidArgumentException; 28 import com.google.api.gax.rpc.StatusCode; 29 import com.google.common.collect.Lists; 30 import com.google.longrunning.Operation; 31 import com.google.protobuf.AbstractMessage; 32 import com.google.protobuf.Any; 33 import com.google.protobuf.Empty; 34 import com.google.protobuf.Timestamp; 35 import io.grpc.StatusRuntimeException; 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.UUID; 42 import java.util.concurrent.ExecutionException; 43 import javax.annotation.Generated; 44 import org.junit.After; 45 import org.junit.AfterClass; 46 import org.junit.Assert; 47 import org.junit.Before; 48 import org.junit.BeforeClass; 49 import org.junit.Test; 50 51 @Generated("by gapic-generator-java") 52 public class GameServerConfigsServiceClientTest { 53 private static MockGameServerConfigsService mockGameServerConfigsService; 54 private static MockServiceHelper mockServiceHelper; 55 private LocalChannelProvider channelProvider; 56 private GameServerConfigsServiceClient client; 57 58 @BeforeClass startStaticServer()59 public static void startStaticServer() { 60 mockGameServerConfigsService = new MockGameServerConfigsService(); 61 mockServiceHelper = 62 new MockServiceHelper( 63 UUID.randomUUID().toString(), 64 Arrays.<MockGrpcService>asList(mockGameServerConfigsService)); 65 mockServiceHelper.start(); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 mockServiceHelper.stop(); 71 } 72 73 @Before setUp()74 public void setUp() throws IOException { 75 mockServiceHelper.reset(); 76 channelProvider = mockServiceHelper.createChannelProvider(); 77 GameServerConfigsServiceSettings settings = 78 GameServerConfigsServiceSettings.newBuilder() 79 .setTransportChannelProvider(channelProvider) 80 .setCredentialsProvider(NoCredentialsProvider.create()) 81 .build(); 82 client = GameServerConfigsServiceClient.create(settings); 83 } 84 85 @After tearDown()86 public void tearDown() throws Exception { 87 client.close(); 88 } 89 90 @Test listGameServerConfigsTest()91 public void listGameServerConfigsTest() throws Exception { 92 GameServerConfig responsesElement = GameServerConfig.newBuilder().build(); 93 ListGameServerConfigsResponse expectedResponse = 94 ListGameServerConfigsResponse.newBuilder() 95 .setNextPageToken("") 96 .addAllGameServerConfigs(Arrays.asList(responsesElement)) 97 .build(); 98 mockGameServerConfigsService.addResponse(expectedResponse); 99 100 GameServerDeploymentName parent = 101 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 102 103 ListGameServerConfigsPagedResponse pagedListResponse = client.listGameServerConfigs(parent); 104 105 List<GameServerConfig> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 106 107 Assert.assertEquals(1, resources.size()); 108 Assert.assertEquals(expectedResponse.getGameServerConfigsList().get(0), resources.get(0)); 109 110 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 111 Assert.assertEquals(1, actualRequests.size()); 112 ListGameServerConfigsRequest actualRequest = 113 ((ListGameServerConfigsRequest) actualRequests.get(0)); 114 115 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 116 Assert.assertTrue( 117 channelProvider.isHeaderSent( 118 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 119 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 120 } 121 122 @Test listGameServerConfigsExceptionTest()123 public void listGameServerConfigsExceptionTest() throws Exception { 124 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 125 mockGameServerConfigsService.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 mockGameServerConfigsService.addResponse(expectedResponse); 146 147 String parent = "parent-995424086"; 148 149 ListGameServerConfigsPagedResponse pagedListResponse = client.listGameServerConfigs(parent); 150 151 List<GameServerConfig> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 152 153 Assert.assertEquals(1, resources.size()); 154 Assert.assertEquals(expectedResponse.getGameServerConfigsList().get(0), resources.get(0)); 155 156 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 157 Assert.assertEquals(1, actualRequests.size()); 158 ListGameServerConfigsRequest actualRequest = 159 ((ListGameServerConfigsRequest) actualRequests.get(0)); 160 161 Assert.assertEquals(parent, actualRequest.getParent()); 162 Assert.assertTrue( 163 channelProvider.isHeaderSent( 164 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 165 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 166 } 167 168 @Test listGameServerConfigsExceptionTest2()169 public void listGameServerConfigsExceptionTest2() throws Exception { 170 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 171 mockGameServerConfigsService.addException(exception); 172 173 try { 174 String parent = "parent-995424086"; 175 client.listGameServerConfigs(parent); 176 Assert.fail("No exception raised"); 177 } catch (InvalidArgumentException e) { 178 // Expected exception. 179 } 180 } 181 182 @Test getGameServerConfigTest()183 public void getGameServerConfigTest() throws Exception { 184 GameServerConfig expectedResponse = 185 GameServerConfig.newBuilder() 186 .setName( 187 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 188 .toString()) 189 .setCreateTime(Timestamp.newBuilder().build()) 190 .setUpdateTime(Timestamp.newBuilder().build()) 191 .putAllLabels(new HashMap<String, String>()) 192 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 193 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 194 .setDescription("description-1724546052") 195 .build(); 196 mockGameServerConfigsService.addResponse(expectedResponse); 197 198 GameServerConfigName name = 199 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 200 201 GameServerConfig actualResponse = client.getGameServerConfig(name); 202 Assert.assertEquals(expectedResponse, actualResponse); 203 204 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 205 Assert.assertEquals(1, actualRequests.size()); 206 GetGameServerConfigRequest actualRequest = ((GetGameServerConfigRequest) actualRequests.get(0)); 207 208 Assert.assertEquals(name.toString(), actualRequest.getName()); 209 Assert.assertTrue( 210 channelProvider.isHeaderSent( 211 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 212 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 213 } 214 215 @Test getGameServerConfigExceptionTest()216 public void getGameServerConfigExceptionTest() throws Exception { 217 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 218 mockGameServerConfigsService.addException(exception); 219 220 try { 221 GameServerConfigName name = 222 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 223 client.getGameServerConfig(name); 224 Assert.fail("No exception raised"); 225 } catch (InvalidArgumentException e) { 226 // Expected exception. 227 } 228 } 229 230 @Test getGameServerConfigTest2()231 public void getGameServerConfigTest2() throws Exception { 232 GameServerConfig expectedResponse = 233 GameServerConfig.newBuilder() 234 .setName( 235 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 236 .toString()) 237 .setCreateTime(Timestamp.newBuilder().build()) 238 .setUpdateTime(Timestamp.newBuilder().build()) 239 .putAllLabels(new HashMap<String, String>()) 240 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 241 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 242 .setDescription("description-1724546052") 243 .build(); 244 mockGameServerConfigsService.addResponse(expectedResponse); 245 246 String name = "name3373707"; 247 248 GameServerConfig actualResponse = client.getGameServerConfig(name); 249 Assert.assertEquals(expectedResponse, actualResponse); 250 251 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 252 Assert.assertEquals(1, actualRequests.size()); 253 GetGameServerConfigRequest actualRequest = ((GetGameServerConfigRequest) actualRequests.get(0)); 254 255 Assert.assertEquals(name, actualRequest.getName()); 256 Assert.assertTrue( 257 channelProvider.isHeaderSent( 258 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 259 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 260 } 261 262 @Test getGameServerConfigExceptionTest2()263 public void getGameServerConfigExceptionTest2() throws Exception { 264 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 265 mockGameServerConfigsService.addException(exception); 266 267 try { 268 String name = "name3373707"; 269 client.getGameServerConfig(name); 270 Assert.fail("No exception raised"); 271 } catch (InvalidArgumentException e) { 272 // Expected exception. 273 } 274 } 275 276 @Test createGameServerConfigTest()277 public void createGameServerConfigTest() throws Exception { 278 GameServerConfig expectedResponse = 279 GameServerConfig.newBuilder() 280 .setName( 281 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 282 .toString()) 283 .setCreateTime(Timestamp.newBuilder().build()) 284 .setUpdateTime(Timestamp.newBuilder().build()) 285 .putAllLabels(new HashMap<String, String>()) 286 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 287 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 288 .setDescription("description-1724546052") 289 .build(); 290 Operation resultOperation = 291 Operation.newBuilder() 292 .setName("createGameServerConfigTest") 293 .setDone(true) 294 .setResponse(Any.pack(expectedResponse)) 295 .build(); 296 mockGameServerConfigsService.addResponse(resultOperation); 297 298 GameServerDeploymentName parent = 299 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 300 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 301 302 GameServerConfig actualResponse = 303 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 304 Assert.assertEquals(expectedResponse, actualResponse); 305 306 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 307 Assert.assertEquals(1, actualRequests.size()); 308 CreateGameServerConfigRequest actualRequest = 309 ((CreateGameServerConfigRequest) actualRequests.get(0)); 310 311 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 312 Assert.assertEquals(gameServerConfig, actualRequest.getGameServerConfig()); 313 Assert.assertTrue( 314 channelProvider.isHeaderSent( 315 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 316 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 317 } 318 319 @Test createGameServerConfigExceptionTest()320 public void createGameServerConfigExceptionTest() throws Exception { 321 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 322 mockGameServerConfigsService.addException(exception); 323 324 try { 325 GameServerDeploymentName parent = 326 GameServerDeploymentName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]"); 327 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 328 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 329 Assert.fail("No exception raised"); 330 } catch (ExecutionException e) { 331 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 332 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 333 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 334 } 335 } 336 337 @Test createGameServerConfigTest2()338 public void createGameServerConfigTest2() throws Exception { 339 GameServerConfig expectedResponse = 340 GameServerConfig.newBuilder() 341 .setName( 342 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]") 343 .toString()) 344 .setCreateTime(Timestamp.newBuilder().build()) 345 .setUpdateTime(Timestamp.newBuilder().build()) 346 .putAllLabels(new HashMap<String, String>()) 347 .addAllFleetConfigs(new ArrayList<FleetConfig>()) 348 .addAllScalingConfigs(new ArrayList<ScalingConfig>()) 349 .setDescription("description-1724546052") 350 .build(); 351 Operation resultOperation = 352 Operation.newBuilder() 353 .setName("createGameServerConfigTest") 354 .setDone(true) 355 .setResponse(Any.pack(expectedResponse)) 356 .build(); 357 mockGameServerConfigsService.addResponse(resultOperation); 358 359 String parent = "parent-995424086"; 360 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 361 362 GameServerConfig actualResponse = 363 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 364 Assert.assertEquals(expectedResponse, actualResponse); 365 366 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 367 Assert.assertEquals(1, actualRequests.size()); 368 CreateGameServerConfigRequest actualRequest = 369 ((CreateGameServerConfigRequest) actualRequests.get(0)); 370 371 Assert.assertEquals(parent, actualRequest.getParent()); 372 Assert.assertEquals(gameServerConfig, actualRequest.getGameServerConfig()); 373 Assert.assertTrue( 374 channelProvider.isHeaderSent( 375 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 376 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 377 } 378 379 @Test createGameServerConfigExceptionTest2()380 public void createGameServerConfigExceptionTest2() throws Exception { 381 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 382 mockGameServerConfigsService.addException(exception); 383 384 try { 385 String parent = "parent-995424086"; 386 GameServerConfig gameServerConfig = GameServerConfig.newBuilder().build(); 387 client.createGameServerConfigAsync(parent, gameServerConfig).get(); 388 Assert.fail("No exception raised"); 389 } catch (ExecutionException e) { 390 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 391 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 392 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 393 } 394 } 395 396 @Test deleteGameServerConfigTest()397 public void deleteGameServerConfigTest() throws Exception { 398 Empty expectedResponse = Empty.newBuilder().build(); 399 Operation resultOperation = 400 Operation.newBuilder() 401 .setName("deleteGameServerConfigTest") 402 .setDone(true) 403 .setResponse(Any.pack(expectedResponse)) 404 .build(); 405 mockGameServerConfigsService.addResponse(resultOperation); 406 407 GameServerConfigName name = 408 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 409 410 client.deleteGameServerConfigAsync(name).get(); 411 412 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 413 Assert.assertEquals(1, actualRequests.size()); 414 DeleteGameServerConfigRequest actualRequest = 415 ((DeleteGameServerConfigRequest) actualRequests.get(0)); 416 417 Assert.assertEquals(name.toString(), actualRequest.getName()); 418 Assert.assertTrue( 419 channelProvider.isHeaderSent( 420 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 421 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 422 } 423 424 @Test deleteGameServerConfigExceptionTest()425 public void deleteGameServerConfigExceptionTest() throws Exception { 426 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 427 mockGameServerConfigsService.addException(exception); 428 429 try { 430 GameServerConfigName name = 431 GameServerConfigName.of("[PROJECT]", "[LOCATION]", "[DEPLOYMENT]", "[CONFIG]"); 432 client.deleteGameServerConfigAsync(name).get(); 433 Assert.fail("No exception raised"); 434 } catch (ExecutionException e) { 435 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 436 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 437 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 438 } 439 } 440 441 @Test deleteGameServerConfigTest2()442 public void deleteGameServerConfigTest2() throws Exception { 443 Empty expectedResponse = Empty.newBuilder().build(); 444 Operation resultOperation = 445 Operation.newBuilder() 446 .setName("deleteGameServerConfigTest") 447 .setDone(true) 448 .setResponse(Any.pack(expectedResponse)) 449 .build(); 450 mockGameServerConfigsService.addResponse(resultOperation); 451 452 String name = "name3373707"; 453 454 client.deleteGameServerConfigAsync(name).get(); 455 456 List<AbstractMessage> actualRequests = mockGameServerConfigsService.getRequests(); 457 Assert.assertEquals(1, actualRequests.size()); 458 DeleteGameServerConfigRequest actualRequest = 459 ((DeleteGameServerConfigRequest) actualRequests.get(0)); 460 461 Assert.assertEquals(name, actualRequest.getName()); 462 Assert.assertTrue( 463 channelProvider.isHeaderSent( 464 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 465 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 466 } 467 468 @Test deleteGameServerConfigExceptionTest2()469 public void deleteGameServerConfigExceptionTest2() throws Exception { 470 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 471 mockGameServerConfigsService.addException(exception); 472 473 try { 474 String name = "name3373707"; 475 client.deleteGameServerConfigAsync(name).get(); 476 Assert.fail("No exception raised"); 477 } catch (ExecutionException e) { 478 Assert.assertEquals(InvalidArgumentException.class, e.getCause().getClass()); 479 InvalidArgumentException apiException = ((InvalidArgumentException) e.getCause()); 480 Assert.assertEquals(StatusCode.Code.INVALID_ARGUMENT, apiException.getStatusCode().getCode()); 481 } 482 } 483 } 484