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.contentwarehouse.v1; 18 19 import static com.google.cloud.contentwarehouse.v1.RuleSetServiceClient.ListRuleSetsPagedResponse; 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.contentwarehouse.v1.stub.HttpJsonRuleSetServiceStub; 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 RuleSetServiceClientHttpJsonTest { 47 private static MockHttpService mockService; 48 private static RuleSetServiceClient client; 49 50 @BeforeClass startStaticServer()51 public static void startStaticServer() throws IOException { 52 mockService = 53 new MockHttpService( 54 HttpJsonRuleSetServiceStub.getMethodDescriptors(), 55 RuleSetServiceSettings.getDefaultEndpoint()); 56 RuleSetServiceSettings settings = 57 RuleSetServiceSettings.newHttpJsonBuilder() 58 .setTransportChannelProvider( 59 RuleSetServiceSettings.defaultHttpJsonTransportProviderBuilder() 60 .setHttpTransport(mockService) 61 .build()) 62 .setCredentialsProvider(NoCredentialsProvider.create()) 63 .build(); 64 client = RuleSetServiceClient.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 createRuleSetTest()81 public void createRuleSetTest() throws Exception { 82 RuleSet expectedResponse = 83 RuleSet.newBuilder() 84 .setName(RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]").toString()) 85 .setDescription("description-1724546052") 86 .setSource("source-896505829") 87 .addAllRules(new ArrayList<Rule>()) 88 .build(); 89 mockService.addResponse(expectedResponse); 90 91 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 92 RuleSet ruleSet = RuleSet.newBuilder().build(); 93 94 RuleSet actualResponse = client.createRuleSet(parent, ruleSet); 95 Assert.assertEquals(expectedResponse, actualResponse); 96 97 List<String> actualRequests = mockService.getRequestPaths(); 98 Assert.assertEquals(1, actualRequests.size()); 99 100 String apiClientHeaderKey = 101 mockService 102 .getRequestHeaders() 103 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 104 .iterator() 105 .next(); 106 Assert.assertTrue( 107 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 108 .matcher(apiClientHeaderKey) 109 .matches()); 110 } 111 112 @Test createRuleSetExceptionTest()113 public void createRuleSetExceptionTest() throws Exception { 114 ApiException exception = 115 ApiExceptionFactory.createException( 116 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 117 mockService.addException(exception); 118 119 try { 120 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 121 RuleSet ruleSet = RuleSet.newBuilder().build(); 122 client.createRuleSet(parent, ruleSet); 123 Assert.fail("No exception raised"); 124 } catch (InvalidArgumentException e) { 125 // Expected exception. 126 } 127 } 128 129 @Test createRuleSetTest2()130 public void createRuleSetTest2() throws Exception { 131 RuleSet expectedResponse = 132 RuleSet.newBuilder() 133 .setName(RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]").toString()) 134 .setDescription("description-1724546052") 135 .setSource("source-896505829") 136 .addAllRules(new ArrayList<Rule>()) 137 .build(); 138 mockService.addResponse(expectedResponse); 139 140 String parent = "projects/project-5833/locations/location-5833"; 141 RuleSet ruleSet = RuleSet.newBuilder().build(); 142 143 RuleSet actualResponse = client.createRuleSet(parent, ruleSet); 144 Assert.assertEquals(expectedResponse, actualResponse); 145 146 List<String> actualRequests = mockService.getRequestPaths(); 147 Assert.assertEquals(1, actualRequests.size()); 148 149 String apiClientHeaderKey = 150 mockService 151 .getRequestHeaders() 152 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 153 .iterator() 154 .next(); 155 Assert.assertTrue( 156 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 157 .matcher(apiClientHeaderKey) 158 .matches()); 159 } 160 161 @Test createRuleSetExceptionTest2()162 public void createRuleSetExceptionTest2() throws Exception { 163 ApiException exception = 164 ApiExceptionFactory.createException( 165 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 166 mockService.addException(exception); 167 168 try { 169 String parent = "projects/project-5833/locations/location-5833"; 170 RuleSet ruleSet = RuleSet.newBuilder().build(); 171 client.createRuleSet(parent, ruleSet); 172 Assert.fail("No exception raised"); 173 } catch (InvalidArgumentException e) { 174 // Expected exception. 175 } 176 } 177 178 @Test getRuleSetTest()179 public void getRuleSetTest() throws Exception { 180 RuleSet expectedResponse = 181 RuleSet.newBuilder() 182 .setName(RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]").toString()) 183 .setDescription("description-1724546052") 184 .setSource("source-896505829") 185 .addAllRules(new ArrayList<Rule>()) 186 .build(); 187 mockService.addResponse(expectedResponse); 188 189 RuleSetName name = RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]"); 190 191 RuleSet actualResponse = client.getRuleSet(name); 192 Assert.assertEquals(expectedResponse, actualResponse); 193 194 List<String> actualRequests = mockService.getRequestPaths(); 195 Assert.assertEquals(1, actualRequests.size()); 196 197 String apiClientHeaderKey = 198 mockService 199 .getRequestHeaders() 200 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 201 .iterator() 202 .next(); 203 Assert.assertTrue( 204 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 205 .matcher(apiClientHeaderKey) 206 .matches()); 207 } 208 209 @Test getRuleSetExceptionTest()210 public void getRuleSetExceptionTest() throws Exception { 211 ApiException exception = 212 ApiExceptionFactory.createException( 213 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 214 mockService.addException(exception); 215 216 try { 217 RuleSetName name = RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]"); 218 client.getRuleSet(name); 219 Assert.fail("No exception raised"); 220 } catch (InvalidArgumentException e) { 221 // Expected exception. 222 } 223 } 224 225 @Test getRuleSetTest2()226 public void getRuleSetTest2() throws Exception { 227 RuleSet expectedResponse = 228 RuleSet.newBuilder() 229 .setName(RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]").toString()) 230 .setDescription("description-1724546052") 231 .setSource("source-896505829") 232 .addAllRules(new ArrayList<Rule>()) 233 .build(); 234 mockService.addResponse(expectedResponse); 235 236 String name = "projects/project-2899/locations/location-2899/ruleSets/ruleSet-2899"; 237 238 RuleSet actualResponse = client.getRuleSet(name); 239 Assert.assertEquals(expectedResponse, actualResponse); 240 241 List<String> actualRequests = mockService.getRequestPaths(); 242 Assert.assertEquals(1, actualRequests.size()); 243 244 String apiClientHeaderKey = 245 mockService 246 .getRequestHeaders() 247 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 248 .iterator() 249 .next(); 250 Assert.assertTrue( 251 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 252 .matcher(apiClientHeaderKey) 253 .matches()); 254 } 255 256 @Test getRuleSetExceptionTest2()257 public void getRuleSetExceptionTest2() throws Exception { 258 ApiException exception = 259 ApiExceptionFactory.createException( 260 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 261 mockService.addException(exception); 262 263 try { 264 String name = "projects/project-2899/locations/location-2899/ruleSets/ruleSet-2899"; 265 client.getRuleSet(name); 266 Assert.fail("No exception raised"); 267 } catch (InvalidArgumentException e) { 268 // Expected exception. 269 } 270 } 271 272 @Test updateRuleSetTest()273 public void updateRuleSetTest() throws Exception { 274 RuleSet expectedResponse = 275 RuleSet.newBuilder() 276 .setName(RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]").toString()) 277 .setDescription("description-1724546052") 278 .setSource("source-896505829") 279 .addAllRules(new ArrayList<Rule>()) 280 .build(); 281 mockService.addResponse(expectedResponse); 282 283 RuleSetName name = RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]"); 284 RuleSet ruleSet = RuleSet.newBuilder().build(); 285 286 RuleSet actualResponse = client.updateRuleSet(name, ruleSet); 287 Assert.assertEquals(expectedResponse, actualResponse); 288 289 List<String> actualRequests = mockService.getRequestPaths(); 290 Assert.assertEquals(1, actualRequests.size()); 291 292 String apiClientHeaderKey = 293 mockService 294 .getRequestHeaders() 295 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 296 .iterator() 297 .next(); 298 Assert.assertTrue( 299 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 300 .matcher(apiClientHeaderKey) 301 .matches()); 302 } 303 304 @Test updateRuleSetExceptionTest()305 public void updateRuleSetExceptionTest() throws Exception { 306 ApiException exception = 307 ApiExceptionFactory.createException( 308 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 309 mockService.addException(exception); 310 311 try { 312 RuleSetName name = RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]"); 313 RuleSet ruleSet = RuleSet.newBuilder().build(); 314 client.updateRuleSet(name, ruleSet); 315 Assert.fail("No exception raised"); 316 } catch (InvalidArgumentException e) { 317 // Expected exception. 318 } 319 } 320 321 @Test updateRuleSetTest2()322 public void updateRuleSetTest2() throws Exception { 323 RuleSet expectedResponse = 324 RuleSet.newBuilder() 325 .setName(RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]").toString()) 326 .setDescription("description-1724546052") 327 .setSource("source-896505829") 328 .addAllRules(new ArrayList<Rule>()) 329 .build(); 330 mockService.addResponse(expectedResponse); 331 332 String name = "projects/project-2899/locations/location-2899/ruleSets/ruleSet-2899"; 333 RuleSet ruleSet = RuleSet.newBuilder().build(); 334 335 RuleSet actualResponse = client.updateRuleSet(name, ruleSet); 336 Assert.assertEquals(expectedResponse, actualResponse); 337 338 List<String> actualRequests = mockService.getRequestPaths(); 339 Assert.assertEquals(1, actualRequests.size()); 340 341 String apiClientHeaderKey = 342 mockService 343 .getRequestHeaders() 344 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 345 .iterator() 346 .next(); 347 Assert.assertTrue( 348 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 349 .matcher(apiClientHeaderKey) 350 .matches()); 351 } 352 353 @Test updateRuleSetExceptionTest2()354 public void updateRuleSetExceptionTest2() throws Exception { 355 ApiException exception = 356 ApiExceptionFactory.createException( 357 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 358 mockService.addException(exception); 359 360 try { 361 String name = "projects/project-2899/locations/location-2899/ruleSets/ruleSet-2899"; 362 RuleSet ruleSet = RuleSet.newBuilder().build(); 363 client.updateRuleSet(name, ruleSet); 364 Assert.fail("No exception raised"); 365 } catch (InvalidArgumentException e) { 366 // Expected exception. 367 } 368 } 369 370 @Test deleteRuleSetTest()371 public void deleteRuleSetTest() throws Exception { 372 Empty expectedResponse = Empty.newBuilder().build(); 373 mockService.addResponse(expectedResponse); 374 375 RuleSetName name = RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]"); 376 377 client.deleteRuleSet(name); 378 379 List<String> actualRequests = mockService.getRequestPaths(); 380 Assert.assertEquals(1, actualRequests.size()); 381 382 String apiClientHeaderKey = 383 mockService 384 .getRequestHeaders() 385 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 386 .iterator() 387 .next(); 388 Assert.assertTrue( 389 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 390 .matcher(apiClientHeaderKey) 391 .matches()); 392 } 393 394 @Test deleteRuleSetExceptionTest()395 public void deleteRuleSetExceptionTest() throws Exception { 396 ApiException exception = 397 ApiExceptionFactory.createException( 398 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 399 mockService.addException(exception); 400 401 try { 402 RuleSetName name = RuleSetName.of("[PROJECT]", "[LOCATION]", "[RULE_SET]"); 403 client.deleteRuleSet(name); 404 Assert.fail("No exception raised"); 405 } catch (InvalidArgumentException e) { 406 // Expected exception. 407 } 408 } 409 410 @Test deleteRuleSetTest2()411 public void deleteRuleSetTest2() throws Exception { 412 Empty expectedResponse = Empty.newBuilder().build(); 413 mockService.addResponse(expectedResponse); 414 415 String name = "projects/project-2899/locations/location-2899/ruleSets/ruleSet-2899"; 416 417 client.deleteRuleSet(name); 418 419 List<String> actualRequests = mockService.getRequestPaths(); 420 Assert.assertEquals(1, actualRequests.size()); 421 422 String apiClientHeaderKey = 423 mockService 424 .getRequestHeaders() 425 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 426 .iterator() 427 .next(); 428 Assert.assertTrue( 429 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 430 .matcher(apiClientHeaderKey) 431 .matches()); 432 } 433 434 @Test deleteRuleSetExceptionTest2()435 public void deleteRuleSetExceptionTest2() throws Exception { 436 ApiException exception = 437 ApiExceptionFactory.createException( 438 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 439 mockService.addException(exception); 440 441 try { 442 String name = "projects/project-2899/locations/location-2899/ruleSets/ruleSet-2899"; 443 client.deleteRuleSet(name); 444 Assert.fail("No exception raised"); 445 } catch (InvalidArgumentException e) { 446 // Expected exception. 447 } 448 } 449 450 @Test listRuleSetsTest()451 public void listRuleSetsTest() throws Exception { 452 RuleSet responsesElement = RuleSet.newBuilder().build(); 453 ListRuleSetsResponse expectedResponse = 454 ListRuleSetsResponse.newBuilder() 455 .setNextPageToken("") 456 .addAllRuleSets(Arrays.asList(responsesElement)) 457 .build(); 458 mockService.addResponse(expectedResponse); 459 460 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 461 462 ListRuleSetsPagedResponse pagedListResponse = client.listRuleSets(parent); 463 464 List<RuleSet> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 465 466 Assert.assertEquals(1, resources.size()); 467 Assert.assertEquals(expectedResponse.getRuleSetsList().get(0), resources.get(0)); 468 469 List<String> actualRequests = mockService.getRequestPaths(); 470 Assert.assertEquals(1, actualRequests.size()); 471 472 String apiClientHeaderKey = 473 mockService 474 .getRequestHeaders() 475 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 476 .iterator() 477 .next(); 478 Assert.assertTrue( 479 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 480 .matcher(apiClientHeaderKey) 481 .matches()); 482 } 483 484 @Test listRuleSetsExceptionTest()485 public void listRuleSetsExceptionTest() throws Exception { 486 ApiException exception = 487 ApiExceptionFactory.createException( 488 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 489 mockService.addException(exception); 490 491 try { 492 LocationName parent = LocationName.of("[PROJECT]", "[LOCATION]"); 493 client.listRuleSets(parent); 494 Assert.fail("No exception raised"); 495 } catch (InvalidArgumentException e) { 496 // Expected exception. 497 } 498 } 499 500 @Test listRuleSetsTest2()501 public void listRuleSetsTest2() throws Exception { 502 RuleSet responsesElement = RuleSet.newBuilder().build(); 503 ListRuleSetsResponse expectedResponse = 504 ListRuleSetsResponse.newBuilder() 505 .setNextPageToken("") 506 .addAllRuleSets(Arrays.asList(responsesElement)) 507 .build(); 508 mockService.addResponse(expectedResponse); 509 510 String parent = "projects/project-5833/locations/location-5833"; 511 512 ListRuleSetsPagedResponse pagedListResponse = client.listRuleSets(parent); 513 514 List<RuleSet> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 515 516 Assert.assertEquals(1, resources.size()); 517 Assert.assertEquals(expectedResponse.getRuleSetsList().get(0), resources.get(0)); 518 519 List<String> actualRequests = mockService.getRequestPaths(); 520 Assert.assertEquals(1, actualRequests.size()); 521 522 String apiClientHeaderKey = 523 mockService 524 .getRequestHeaders() 525 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 526 .iterator() 527 .next(); 528 Assert.assertTrue( 529 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 530 .matcher(apiClientHeaderKey) 531 .matches()); 532 } 533 534 @Test listRuleSetsExceptionTest2()535 public void listRuleSetsExceptionTest2() throws Exception { 536 ApiException exception = 537 ApiExceptionFactory.createException( 538 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 539 mockService.addException(exception); 540 541 try { 542 String parent = "projects/project-5833/locations/location-5833"; 543 client.listRuleSets(parent); 544 Assert.fail("No exception raised"); 545 } catch (InvalidArgumentException e) { 546 // Expected exception. 547 } 548 } 549 } 550