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.talent.v4; 18 19 import static com.google.cloud.talent.v4.TenantServiceClient.ListTenantsPagedResponse; 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.common.collect.Lists; 29 import com.google.protobuf.AbstractMessage; 30 import com.google.protobuf.Empty; 31 import com.google.protobuf.FieldMask; 32 import io.grpc.StatusRuntimeException; 33 import java.io.IOException; 34 import java.util.Arrays; 35 import java.util.List; 36 import java.util.UUID; 37 import javax.annotation.Generated; 38 import org.junit.After; 39 import org.junit.AfterClass; 40 import org.junit.Assert; 41 import org.junit.Before; 42 import org.junit.BeforeClass; 43 import org.junit.Test; 44 45 @Generated("by gapic-generator-java") 46 public class TenantServiceClientTest { 47 private static MockServiceHelper mockServiceHelper; 48 private static MockTenantService mockTenantService; 49 private LocalChannelProvider channelProvider; 50 private TenantServiceClient client; 51 52 @BeforeClass startStaticServer()53 public static void startStaticServer() { 54 mockTenantService = new MockTenantService(); 55 mockServiceHelper = 56 new MockServiceHelper( 57 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockTenantService)); 58 mockServiceHelper.start(); 59 } 60 61 @AfterClass stopServer()62 public static void stopServer() { 63 mockServiceHelper.stop(); 64 } 65 66 @Before setUp()67 public void setUp() throws IOException { 68 mockServiceHelper.reset(); 69 channelProvider = mockServiceHelper.createChannelProvider(); 70 TenantServiceSettings settings = 71 TenantServiceSettings.newBuilder() 72 .setTransportChannelProvider(channelProvider) 73 .setCredentialsProvider(NoCredentialsProvider.create()) 74 .build(); 75 client = TenantServiceClient.create(settings); 76 } 77 78 @After tearDown()79 public void tearDown() throws Exception { 80 client.close(); 81 } 82 83 @Test createTenantTest()84 public void createTenantTest() throws Exception { 85 Tenant expectedResponse = 86 Tenant.newBuilder() 87 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 88 .setExternalId("externalId-1699764666") 89 .build(); 90 mockTenantService.addResponse(expectedResponse); 91 92 ProjectName parent = ProjectName.of("[PROJECT]"); 93 Tenant tenant = Tenant.newBuilder().build(); 94 95 Tenant actualResponse = client.createTenant(parent, tenant); 96 Assert.assertEquals(expectedResponse, actualResponse); 97 98 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 99 Assert.assertEquals(1, actualRequests.size()); 100 CreateTenantRequest actualRequest = ((CreateTenantRequest) actualRequests.get(0)); 101 102 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 103 Assert.assertEquals(tenant, actualRequest.getTenant()); 104 Assert.assertTrue( 105 channelProvider.isHeaderSent( 106 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 107 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 108 } 109 110 @Test createTenantExceptionTest()111 public void createTenantExceptionTest() throws Exception { 112 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 113 mockTenantService.addException(exception); 114 115 try { 116 ProjectName parent = ProjectName.of("[PROJECT]"); 117 Tenant tenant = Tenant.newBuilder().build(); 118 client.createTenant(parent, tenant); 119 Assert.fail("No exception raised"); 120 } catch (InvalidArgumentException e) { 121 // Expected exception. 122 } 123 } 124 125 @Test createTenantTest2()126 public void createTenantTest2() throws Exception { 127 Tenant expectedResponse = 128 Tenant.newBuilder() 129 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 130 .setExternalId("externalId-1699764666") 131 .build(); 132 mockTenantService.addResponse(expectedResponse); 133 134 String parent = "parent-995424086"; 135 Tenant tenant = Tenant.newBuilder().build(); 136 137 Tenant actualResponse = client.createTenant(parent, tenant); 138 Assert.assertEquals(expectedResponse, actualResponse); 139 140 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 141 Assert.assertEquals(1, actualRequests.size()); 142 CreateTenantRequest actualRequest = ((CreateTenantRequest) actualRequests.get(0)); 143 144 Assert.assertEquals(parent, actualRequest.getParent()); 145 Assert.assertEquals(tenant, actualRequest.getTenant()); 146 Assert.assertTrue( 147 channelProvider.isHeaderSent( 148 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 149 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 150 } 151 152 @Test createTenantExceptionTest2()153 public void createTenantExceptionTest2() throws Exception { 154 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 155 mockTenantService.addException(exception); 156 157 try { 158 String parent = "parent-995424086"; 159 Tenant tenant = Tenant.newBuilder().build(); 160 client.createTenant(parent, tenant); 161 Assert.fail("No exception raised"); 162 } catch (InvalidArgumentException e) { 163 // Expected exception. 164 } 165 } 166 167 @Test getTenantTest()168 public void getTenantTest() throws Exception { 169 Tenant expectedResponse = 170 Tenant.newBuilder() 171 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 172 .setExternalId("externalId-1699764666") 173 .build(); 174 mockTenantService.addResponse(expectedResponse); 175 176 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 177 178 Tenant actualResponse = client.getTenant(name); 179 Assert.assertEquals(expectedResponse, actualResponse); 180 181 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 182 Assert.assertEquals(1, actualRequests.size()); 183 GetTenantRequest actualRequest = ((GetTenantRequest) actualRequests.get(0)); 184 185 Assert.assertEquals(name.toString(), actualRequest.getName()); 186 Assert.assertTrue( 187 channelProvider.isHeaderSent( 188 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 189 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 190 } 191 192 @Test getTenantExceptionTest()193 public void getTenantExceptionTest() throws Exception { 194 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 195 mockTenantService.addException(exception); 196 197 try { 198 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 199 client.getTenant(name); 200 Assert.fail("No exception raised"); 201 } catch (InvalidArgumentException e) { 202 // Expected exception. 203 } 204 } 205 206 @Test getTenantTest2()207 public void getTenantTest2() throws Exception { 208 Tenant expectedResponse = 209 Tenant.newBuilder() 210 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 211 .setExternalId("externalId-1699764666") 212 .build(); 213 mockTenantService.addResponse(expectedResponse); 214 215 String name = "name3373707"; 216 217 Tenant actualResponse = client.getTenant(name); 218 Assert.assertEquals(expectedResponse, actualResponse); 219 220 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 221 Assert.assertEquals(1, actualRequests.size()); 222 GetTenantRequest actualRequest = ((GetTenantRequest) actualRequests.get(0)); 223 224 Assert.assertEquals(name, actualRequest.getName()); 225 Assert.assertTrue( 226 channelProvider.isHeaderSent( 227 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 228 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 229 } 230 231 @Test getTenantExceptionTest2()232 public void getTenantExceptionTest2() throws Exception { 233 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 234 mockTenantService.addException(exception); 235 236 try { 237 String name = "name3373707"; 238 client.getTenant(name); 239 Assert.fail("No exception raised"); 240 } catch (InvalidArgumentException e) { 241 // Expected exception. 242 } 243 } 244 245 @Test updateTenantTest()246 public void updateTenantTest() throws Exception { 247 Tenant expectedResponse = 248 Tenant.newBuilder() 249 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 250 .setExternalId("externalId-1699764666") 251 .build(); 252 mockTenantService.addResponse(expectedResponse); 253 254 Tenant tenant = Tenant.newBuilder().build(); 255 FieldMask updateMask = FieldMask.newBuilder().build(); 256 257 Tenant actualResponse = client.updateTenant(tenant, updateMask); 258 Assert.assertEquals(expectedResponse, actualResponse); 259 260 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 261 Assert.assertEquals(1, actualRequests.size()); 262 UpdateTenantRequest actualRequest = ((UpdateTenantRequest) actualRequests.get(0)); 263 264 Assert.assertEquals(tenant, actualRequest.getTenant()); 265 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 266 Assert.assertTrue( 267 channelProvider.isHeaderSent( 268 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 269 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 270 } 271 272 @Test updateTenantExceptionTest()273 public void updateTenantExceptionTest() throws Exception { 274 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 275 mockTenantService.addException(exception); 276 277 try { 278 Tenant tenant = Tenant.newBuilder().build(); 279 FieldMask updateMask = FieldMask.newBuilder().build(); 280 client.updateTenant(tenant, updateMask); 281 Assert.fail("No exception raised"); 282 } catch (InvalidArgumentException e) { 283 // Expected exception. 284 } 285 } 286 287 @Test deleteTenantTest()288 public void deleteTenantTest() throws Exception { 289 Empty expectedResponse = Empty.newBuilder().build(); 290 mockTenantService.addResponse(expectedResponse); 291 292 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 293 294 client.deleteTenant(name); 295 296 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 297 Assert.assertEquals(1, actualRequests.size()); 298 DeleteTenantRequest actualRequest = ((DeleteTenantRequest) actualRequests.get(0)); 299 300 Assert.assertEquals(name.toString(), actualRequest.getName()); 301 Assert.assertTrue( 302 channelProvider.isHeaderSent( 303 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 304 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 305 } 306 307 @Test deleteTenantExceptionTest()308 public void deleteTenantExceptionTest() throws Exception { 309 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 310 mockTenantService.addException(exception); 311 312 try { 313 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 314 client.deleteTenant(name); 315 Assert.fail("No exception raised"); 316 } catch (InvalidArgumentException e) { 317 // Expected exception. 318 } 319 } 320 321 @Test deleteTenantTest2()322 public void deleteTenantTest2() throws Exception { 323 Empty expectedResponse = Empty.newBuilder().build(); 324 mockTenantService.addResponse(expectedResponse); 325 326 String name = "name3373707"; 327 328 client.deleteTenant(name); 329 330 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 331 Assert.assertEquals(1, actualRequests.size()); 332 DeleteTenantRequest actualRequest = ((DeleteTenantRequest) actualRequests.get(0)); 333 334 Assert.assertEquals(name, actualRequest.getName()); 335 Assert.assertTrue( 336 channelProvider.isHeaderSent( 337 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 338 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 339 } 340 341 @Test deleteTenantExceptionTest2()342 public void deleteTenantExceptionTest2() throws Exception { 343 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 344 mockTenantService.addException(exception); 345 346 try { 347 String name = "name3373707"; 348 client.deleteTenant(name); 349 Assert.fail("No exception raised"); 350 } catch (InvalidArgumentException e) { 351 // Expected exception. 352 } 353 } 354 355 @Test listTenantsTest()356 public void listTenantsTest() throws Exception { 357 Tenant responsesElement = Tenant.newBuilder().build(); 358 ListTenantsResponse expectedResponse = 359 ListTenantsResponse.newBuilder() 360 .setNextPageToken("") 361 .addAllTenants(Arrays.asList(responsesElement)) 362 .build(); 363 mockTenantService.addResponse(expectedResponse); 364 365 ProjectName parent = ProjectName.of("[PROJECT]"); 366 367 ListTenantsPagedResponse pagedListResponse = client.listTenants(parent); 368 369 List<Tenant> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 370 371 Assert.assertEquals(1, resources.size()); 372 Assert.assertEquals(expectedResponse.getTenantsList().get(0), resources.get(0)); 373 374 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 375 Assert.assertEquals(1, actualRequests.size()); 376 ListTenantsRequest actualRequest = ((ListTenantsRequest) actualRequests.get(0)); 377 378 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 379 Assert.assertTrue( 380 channelProvider.isHeaderSent( 381 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 382 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 383 } 384 385 @Test listTenantsExceptionTest()386 public void listTenantsExceptionTest() throws Exception { 387 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 388 mockTenantService.addException(exception); 389 390 try { 391 ProjectName parent = ProjectName.of("[PROJECT]"); 392 client.listTenants(parent); 393 Assert.fail("No exception raised"); 394 } catch (InvalidArgumentException e) { 395 // Expected exception. 396 } 397 } 398 399 @Test listTenantsTest2()400 public void listTenantsTest2() throws Exception { 401 Tenant responsesElement = Tenant.newBuilder().build(); 402 ListTenantsResponse expectedResponse = 403 ListTenantsResponse.newBuilder() 404 .setNextPageToken("") 405 .addAllTenants(Arrays.asList(responsesElement)) 406 .build(); 407 mockTenantService.addResponse(expectedResponse); 408 409 String parent = "parent-995424086"; 410 411 ListTenantsPagedResponse pagedListResponse = client.listTenants(parent); 412 413 List<Tenant> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 414 415 Assert.assertEquals(1, resources.size()); 416 Assert.assertEquals(expectedResponse.getTenantsList().get(0), resources.get(0)); 417 418 List<AbstractMessage> actualRequests = mockTenantService.getRequests(); 419 Assert.assertEquals(1, actualRequests.size()); 420 ListTenantsRequest actualRequest = ((ListTenantsRequest) actualRequests.get(0)); 421 422 Assert.assertEquals(parent, actualRequest.getParent()); 423 Assert.assertTrue( 424 channelProvider.isHeaderSent( 425 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 426 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 427 } 428 429 @Test listTenantsExceptionTest2()430 public void listTenantsExceptionTest2() throws Exception { 431 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 432 mockTenantService.addException(exception); 433 434 try { 435 String parent = "parent-995424086"; 436 client.listTenants(parent); 437 Assert.fail("No exception raised"); 438 } catch (InvalidArgumentException e) { 439 // Expected exception. 440 } 441 } 442 } 443