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.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.talent.v4beta1.stub.HttpJsonTenantServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Empty; 33 import java.io.IOException; 34 import java.util.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 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 TenantServiceClientHttpJsonTest { 47 private static MockHttpService mockService; 48 private static TenantServiceClient client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() throws IOException { 52 mockService = 53 new MockHttpService( 54 HttpJsonTenantServiceStub.getMethodDescriptors(), 55 TenantServiceSettings.getDefaultEndpoint()); 56 TenantServiceSettings settings = 57 TenantServiceSettings.newHttpJsonBuilder() 58 .setTransportChannelProvider( 59 TenantServiceSettings.defaultHttpJsonTransportProviderBuilder() 60 .setHttpTransport(mockService) 61 .build()) 62 .setCredentialsProvider(NoCredentialsProvider.create()) 63 .build(); 64 client = TenantServiceClient.create(settings); 65 } 66 67 @AfterClass stopServer()68 public static void stopServer() { 69 client.close(); 70 } 71 72 @Before setUp()73 public void setUp() {} 74 75 @After tearDown()76 public void tearDown() throws Exception { 77 mockService.reset(); 78 } 79 80 @Test createTenantTest()81 public void createTenantTest() throws Exception { 82 Tenant expectedResponse = 83 Tenant.newBuilder() 84 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 85 .setExternalId("externalId-1699764666") 86 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 87 .build(); 88 mockService.addResponse(expectedResponse); 89 90 ProjectName parent = ProjectName.of("[PROJECT]"); 91 Tenant tenant = Tenant.newBuilder().build(); 92 93 Tenant actualResponse = client.createTenant(parent, tenant); 94 Assert.assertEquals(expectedResponse, actualResponse); 95 96 List<String> actualRequests = mockService.getRequestPaths(); 97 Assert.assertEquals(1, actualRequests.size()); 98 99 String apiClientHeaderKey = 100 mockService 101 .getRequestHeaders() 102 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 103 .iterator() 104 .next(); 105 Assert.assertTrue( 106 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 107 .matcher(apiClientHeaderKey) 108 .matches()); 109 } 110 111 @Test createTenantExceptionTest()112 public void createTenantExceptionTest() throws Exception { 113 ApiException exception = 114 ApiExceptionFactory.createException( 115 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 116 mockService.addException(exception); 117 118 try { 119 ProjectName parent = ProjectName.of("[PROJECT]"); 120 Tenant tenant = Tenant.newBuilder().build(); 121 client.createTenant(parent, tenant); 122 Assert.fail("No exception raised"); 123 } catch (InvalidArgumentException e) { 124 // Expected exception. 125 } 126 } 127 128 @Test createTenantTest2()129 public void createTenantTest2() throws Exception { 130 Tenant expectedResponse = 131 Tenant.newBuilder() 132 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 133 .setExternalId("externalId-1699764666") 134 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 135 .build(); 136 mockService.addResponse(expectedResponse); 137 138 String parent = "projects/project-2353"; 139 Tenant tenant = Tenant.newBuilder().build(); 140 141 Tenant actualResponse = client.createTenant(parent, tenant); 142 Assert.assertEquals(expectedResponse, actualResponse); 143 144 List<String> actualRequests = mockService.getRequestPaths(); 145 Assert.assertEquals(1, actualRequests.size()); 146 147 String apiClientHeaderKey = 148 mockService 149 .getRequestHeaders() 150 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 151 .iterator() 152 .next(); 153 Assert.assertTrue( 154 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 155 .matcher(apiClientHeaderKey) 156 .matches()); 157 } 158 159 @Test createTenantExceptionTest2()160 public void createTenantExceptionTest2() throws Exception { 161 ApiException exception = 162 ApiExceptionFactory.createException( 163 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 164 mockService.addException(exception); 165 166 try { 167 String parent = "projects/project-2353"; 168 Tenant tenant = Tenant.newBuilder().build(); 169 client.createTenant(parent, tenant); 170 Assert.fail("No exception raised"); 171 } catch (InvalidArgumentException e) { 172 // Expected exception. 173 } 174 } 175 176 @Test getTenantTest()177 public void getTenantTest() throws Exception { 178 Tenant expectedResponse = 179 Tenant.newBuilder() 180 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 181 .setExternalId("externalId-1699764666") 182 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 183 .build(); 184 mockService.addResponse(expectedResponse); 185 186 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 187 188 Tenant actualResponse = client.getTenant(name); 189 Assert.assertEquals(expectedResponse, actualResponse); 190 191 List<String> actualRequests = mockService.getRequestPaths(); 192 Assert.assertEquals(1, actualRequests.size()); 193 194 String apiClientHeaderKey = 195 mockService 196 .getRequestHeaders() 197 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 198 .iterator() 199 .next(); 200 Assert.assertTrue( 201 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 202 .matcher(apiClientHeaderKey) 203 .matches()); 204 } 205 206 @Test getTenantExceptionTest()207 public void getTenantExceptionTest() throws Exception { 208 ApiException exception = 209 ApiExceptionFactory.createException( 210 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 211 mockService.addException(exception); 212 213 try { 214 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 215 client.getTenant(name); 216 Assert.fail("No exception raised"); 217 } catch (InvalidArgumentException e) { 218 // Expected exception. 219 } 220 } 221 222 @Test getTenantTest2()223 public void getTenantTest2() throws Exception { 224 Tenant expectedResponse = 225 Tenant.newBuilder() 226 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 227 .setExternalId("externalId-1699764666") 228 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 229 .build(); 230 mockService.addResponse(expectedResponse); 231 232 String name = "projects/project-7691/tenants/tenant-7691"; 233 234 Tenant actualResponse = client.getTenant(name); 235 Assert.assertEquals(expectedResponse, actualResponse); 236 237 List<String> actualRequests = mockService.getRequestPaths(); 238 Assert.assertEquals(1, actualRequests.size()); 239 240 String apiClientHeaderKey = 241 mockService 242 .getRequestHeaders() 243 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 244 .iterator() 245 .next(); 246 Assert.assertTrue( 247 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 248 .matcher(apiClientHeaderKey) 249 .matches()); 250 } 251 252 @Test getTenantExceptionTest2()253 public void getTenantExceptionTest2() throws Exception { 254 ApiException exception = 255 ApiExceptionFactory.createException( 256 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 257 mockService.addException(exception); 258 259 try { 260 String name = "projects/project-7691/tenants/tenant-7691"; 261 client.getTenant(name); 262 Assert.fail("No exception raised"); 263 } catch (InvalidArgumentException e) { 264 // Expected exception. 265 } 266 } 267 268 @Test updateTenantTest()269 public void updateTenantTest() throws Exception { 270 Tenant expectedResponse = 271 Tenant.newBuilder() 272 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 273 .setExternalId("externalId-1699764666") 274 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 275 .build(); 276 mockService.addResponse(expectedResponse); 277 278 Tenant tenant = 279 Tenant.newBuilder() 280 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 281 .setExternalId("externalId-1699764666") 282 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 283 .build(); 284 285 Tenant actualResponse = client.updateTenant(tenant); 286 Assert.assertEquals(expectedResponse, actualResponse); 287 288 List<String> actualRequests = mockService.getRequestPaths(); 289 Assert.assertEquals(1, actualRequests.size()); 290 291 String apiClientHeaderKey = 292 mockService 293 .getRequestHeaders() 294 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 295 .iterator() 296 .next(); 297 Assert.assertTrue( 298 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 299 .matcher(apiClientHeaderKey) 300 .matches()); 301 } 302 303 @Test updateTenantExceptionTest()304 public void updateTenantExceptionTest() throws Exception { 305 ApiException exception = 306 ApiExceptionFactory.createException( 307 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 308 mockService.addException(exception); 309 310 try { 311 Tenant tenant = 312 Tenant.newBuilder() 313 .setName(TenantName.of("[PROJECT]", "[TENANT]").toString()) 314 .setExternalId("externalId-1699764666") 315 .addAllKeywordSearchableProfileCustomAttributes(new ArrayList<String>()) 316 .build(); 317 client.updateTenant(tenant); 318 Assert.fail("No exception raised"); 319 } catch (InvalidArgumentException e) { 320 // Expected exception. 321 } 322 } 323 324 @Test deleteTenantTest()325 public void deleteTenantTest() throws Exception { 326 Empty expectedResponse = Empty.newBuilder().build(); 327 mockService.addResponse(expectedResponse); 328 329 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 330 331 client.deleteTenant(name); 332 333 List<String> actualRequests = mockService.getRequestPaths(); 334 Assert.assertEquals(1, actualRequests.size()); 335 336 String apiClientHeaderKey = 337 mockService 338 .getRequestHeaders() 339 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 340 .iterator() 341 .next(); 342 Assert.assertTrue( 343 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 344 .matcher(apiClientHeaderKey) 345 .matches()); 346 } 347 348 @Test deleteTenantExceptionTest()349 public void deleteTenantExceptionTest() throws Exception { 350 ApiException exception = 351 ApiExceptionFactory.createException( 352 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 353 mockService.addException(exception); 354 355 try { 356 TenantName name = TenantName.of("[PROJECT]", "[TENANT]"); 357 client.deleteTenant(name); 358 Assert.fail("No exception raised"); 359 } catch (InvalidArgumentException e) { 360 // Expected exception. 361 } 362 } 363 364 @Test deleteTenantTest2()365 public void deleteTenantTest2() throws Exception { 366 Empty expectedResponse = Empty.newBuilder().build(); 367 mockService.addResponse(expectedResponse); 368 369 String name = "projects/project-7691/tenants/tenant-7691"; 370 371 client.deleteTenant(name); 372 373 List<String> actualRequests = mockService.getRequestPaths(); 374 Assert.assertEquals(1, actualRequests.size()); 375 376 String apiClientHeaderKey = 377 mockService 378 .getRequestHeaders() 379 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 380 .iterator() 381 .next(); 382 Assert.assertTrue( 383 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 384 .matcher(apiClientHeaderKey) 385 .matches()); 386 } 387 388 @Test deleteTenantExceptionTest2()389 public void deleteTenantExceptionTest2() throws Exception { 390 ApiException exception = 391 ApiExceptionFactory.createException( 392 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 393 mockService.addException(exception); 394 395 try { 396 String name = "projects/project-7691/tenants/tenant-7691"; 397 client.deleteTenant(name); 398 Assert.fail("No exception raised"); 399 } catch (InvalidArgumentException e) { 400 // Expected exception. 401 } 402 } 403 404 @Test listTenantsTest()405 public void listTenantsTest() throws Exception { 406 Tenant responsesElement = Tenant.newBuilder().build(); 407 ListTenantsResponse expectedResponse = 408 ListTenantsResponse.newBuilder() 409 .setNextPageToken("") 410 .addAllTenants(Arrays.asList(responsesElement)) 411 .build(); 412 mockService.addResponse(expectedResponse); 413 414 ProjectName parent = ProjectName.of("[PROJECT]"); 415 416 ListTenantsPagedResponse pagedListResponse = client.listTenants(parent); 417 418 List<Tenant> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 419 420 Assert.assertEquals(1, resources.size()); 421 Assert.assertEquals(expectedResponse.getTenantsList().get(0), resources.get(0)); 422 423 List<String> actualRequests = mockService.getRequestPaths(); 424 Assert.assertEquals(1, actualRequests.size()); 425 426 String apiClientHeaderKey = 427 mockService 428 .getRequestHeaders() 429 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 430 .iterator() 431 .next(); 432 Assert.assertTrue( 433 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 434 .matcher(apiClientHeaderKey) 435 .matches()); 436 } 437 438 @Test listTenantsExceptionTest()439 public void listTenantsExceptionTest() throws Exception { 440 ApiException exception = 441 ApiExceptionFactory.createException( 442 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 443 mockService.addException(exception); 444 445 try { 446 ProjectName parent = ProjectName.of("[PROJECT]"); 447 client.listTenants(parent); 448 Assert.fail("No exception raised"); 449 } catch (InvalidArgumentException e) { 450 // Expected exception. 451 } 452 } 453 454 @Test listTenantsTest2()455 public void listTenantsTest2() throws Exception { 456 Tenant responsesElement = Tenant.newBuilder().build(); 457 ListTenantsResponse expectedResponse = 458 ListTenantsResponse.newBuilder() 459 .setNextPageToken("") 460 .addAllTenants(Arrays.asList(responsesElement)) 461 .build(); 462 mockService.addResponse(expectedResponse); 463 464 String parent = "projects/project-2353"; 465 466 ListTenantsPagedResponse pagedListResponse = client.listTenants(parent); 467 468 List<Tenant> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 469 470 Assert.assertEquals(1, resources.size()); 471 Assert.assertEquals(expectedResponse.getTenantsList().get(0), resources.get(0)); 472 473 List<String> actualRequests = mockService.getRequestPaths(); 474 Assert.assertEquals(1, actualRequests.size()); 475 476 String apiClientHeaderKey = 477 mockService 478 .getRequestHeaders() 479 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 480 .iterator() 481 .next(); 482 Assert.assertTrue( 483 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 484 .matcher(apiClientHeaderKey) 485 .matches()); 486 } 487 488 @Test listTenantsExceptionTest2()489 public void listTenantsExceptionTest2() throws Exception { 490 ApiException exception = 491 ApiExceptionFactory.createException( 492 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 493 mockService.addException(exception); 494 495 try { 496 String parent = "projects/project-2353"; 497 client.listTenants(parent); 498 Assert.fail("No exception raised"); 499 } catch (InvalidArgumentException e) { 500 // Expected exception. 501 } 502 } 503 } 504