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