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.billing.budgets.v1; 18 19 import static com.google.cloud.billing.budgets.v1.BudgetServiceClient.ListBudgetsPagedResponse; 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.billing.budgets.v1.stub.HttpJsonBudgetServiceStub; 31 import com.google.common.collect.Lists; 32 import com.google.protobuf.Empty; 33 import com.google.protobuf.FieldMask; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.List; 38 import javax.annotation.Generated; 39 import org.junit.After; 40 import org.junit.AfterClass; 41 import org.junit.Assert; 42 import org.junit.Before; 43 import org.junit.BeforeClass; 44 import org.junit.Test; 45 46 @Generated("by gapic-generator-java") 47 public class BudgetServiceClientHttpJsonTest { 48 private static MockHttpService mockService; 49 private static BudgetServiceClient client; 50 51 @BeforeClass startStaticServer()52 public static void startStaticServer() throws IOException { 53 mockService = 54 new MockHttpService( 55 HttpJsonBudgetServiceStub.getMethodDescriptors(), 56 BudgetServiceSettings.getDefaultEndpoint()); 57 BudgetServiceSettings settings = 58 BudgetServiceSettings.newHttpJsonBuilder() 59 .setTransportChannelProvider( 60 BudgetServiceSettings.defaultHttpJsonTransportProviderBuilder() 61 .setHttpTransport(mockService) 62 .build()) 63 .setCredentialsProvider(NoCredentialsProvider.create()) 64 .build(); 65 client = BudgetServiceClient.create(settings); 66 } 67 68 @AfterClass stopServer()69 public static void stopServer() { 70 client.close(); 71 } 72 73 @Before setUp()74 public void setUp() {} 75 76 @After tearDown()77 public void tearDown() throws Exception { 78 mockService.reset(); 79 } 80 81 @Test createBudgetTest()82 public void createBudgetTest() throws Exception { 83 Budget expectedResponse = 84 Budget.newBuilder() 85 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 86 .setDisplayName("displayName1714148973") 87 .setBudgetFilter(Filter.newBuilder().build()) 88 .setAmount(BudgetAmount.newBuilder().build()) 89 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 90 .setNotificationsRule(NotificationsRule.newBuilder().build()) 91 .setEtag("etag3123477") 92 .build(); 93 mockService.addResponse(expectedResponse); 94 95 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 96 Budget budget = Budget.newBuilder().build(); 97 98 Budget actualResponse = client.createBudget(parent, budget); 99 Assert.assertEquals(expectedResponse, actualResponse); 100 101 List<String> actualRequests = mockService.getRequestPaths(); 102 Assert.assertEquals(1, actualRequests.size()); 103 104 String apiClientHeaderKey = 105 mockService 106 .getRequestHeaders() 107 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 108 .iterator() 109 .next(); 110 Assert.assertTrue( 111 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 112 .matcher(apiClientHeaderKey) 113 .matches()); 114 } 115 116 @Test createBudgetExceptionTest()117 public void createBudgetExceptionTest() throws Exception { 118 ApiException exception = 119 ApiExceptionFactory.createException( 120 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 121 mockService.addException(exception); 122 123 try { 124 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 125 Budget budget = Budget.newBuilder().build(); 126 client.createBudget(parent, budget); 127 Assert.fail("No exception raised"); 128 } catch (InvalidArgumentException e) { 129 // Expected exception. 130 } 131 } 132 133 @Test createBudgetTest2()134 public void createBudgetTest2() throws Exception { 135 Budget expectedResponse = 136 Budget.newBuilder() 137 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 138 .setDisplayName("displayName1714148973") 139 .setBudgetFilter(Filter.newBuilder().build()) 140 .setAmount(BudgetAmount.newBuilder().build()) 141 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 142 .setNotificationsRule(NotificationsRule.newBuilder().build()) 143 .setEtag("etag3123477") 144 .build(); 145 mockService.addResponse(expectedResponse); 146 147 String parent = "billingAccounts/billingAccount-7950"; 148 Budget budget = Budget.newBuilder().build(); 149 150 Budget actualResponse = client.createBudget(parent, budget); 151 Assert.assertEquals(expectedResponse, actualResponse); 152 153 List<String> actualRequests = mockService.getRequestPaths(); 154 Assert.assertEquals(1, actualRequests.size()); 155 156 String apiClientHeaderKey = 157 mockService 158 .getRequestHeaders() 159 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 160 .iterator() 161 .next(); 162 Assert.assertTrue( 163 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 164 .matcher(apiClientHeaderKey) 165 .matches()); 166 } 167 168 @Test createBudgetExceptionTest2()169 public void createBudgetExceptionTest2() throws Exception { 170 ApiException exception = 171 ApiExceptionFactory.createException( 172 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 173 mockService.addException(exception); 174 175 try { 176 String parent = "billingAccounts/billingAccount-7950"; 177 Budget budget = Budget.newBuilder().build(); 178 client.createBudget(parent, budget); 179 Assert.fail("No exception raised"); 180 } catch (InvalidArgumentException e) { 181 // Expected exception. 182 } 183 } 184 185 @Test updateBudgetTest()186 public void updateBudgetTest() throws Exception { 187 Budget expectedResponse = 188 Budget.newBuilder() 189 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 190 .setDisplayName("displayName1714148973") 191 .setBudgetFilter(Filter.newBuilder().build()) 192 .setAmount(BudgetAmount.newBuilder().build()) 193 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 194 .setNotificationsRule(NotificationsRule.newBuilder().build()) 195 .setEtag("etag3123477") 196 .build(); 197 mockService.addResponse(expectedResponse); 198 199 Budget budget = 200 Budget.newBuilder() 201 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 202 .setDisplayName("displayName1714148973") 203 .setBudgetFilter(Filter.newBuilder().build()) 204 .setAmount(BudgetAmount.newBuilder().build()) 205 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 206 .setNotificationsRule(NotificationsRule.newBuilder().build()) 207 .setEtag("etag3123477") 208 .build(); 209 FieldMask updateMask = FieldMask.newBuilder().build(); 210 211 Budget actualResponse = client.updateBudget(budget, updateMask); 212 Assert.assertEquals(expectedResponse, actualResponse); 213 214 List<String> actualRequests = mockService.getRequestPaths(); 215 Assert.assertEquals(1, actualRequests.size()); 216 217 String apiClientHeaderKey = 218 mockService 219 .getRequestHeaders() 220 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 221 .iterator() 222 .next(); 223 Assert.assertTrue( 224 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 225 .matcher(apiClientHeaderKey) 226 .matches()); 227 } 228 229 @Test updateBudgetExceptionTest()230 public void updateBudgetExceptionTest() throws Exception { 231 ApiException exception = 232 ApiExceptionFactory.createException( 233 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 234 mockService.addException(exception); 235 236 try { 237 Budget budget = 238 Budget.newBuilder() 239 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 240 .setDisplayName("displayName1714148973") 241 .setBudgetFilter(Filter.newBuilder().build()) 242 .setAmount(BudgetAmount.newBuilder().build()) 243 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 244 .setNotificationsRule(NotificationsRule.newBuilder().build()) 245 .setEtag("etag3123477") 246 .build(); 247 FieldMask updateMask = FieldMask.newBuilder().build(); 248 client.updateBudget(budget, updateMask); 249 Assert.fail("No exception raised"); 250 } catch (InvalidArgumentException e) { 251 // Expected exception. 252 } 253 } 254 255 @Test getBudgetTest()256 public void getBudgetTest() throws Exception { 257 Budget expectedResponse = 258 Budget.newBuilder() 259 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 260 .setDisplayName("displayName1714148973") 261 .setBudgetFilter(Filter.newBuilder().build()) 262 .setAmount(BudgetAmount.newBuilder().build()) 263 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 264 .setNotificationsRule(NotificationsRule.newBuilder().build()) 265 .setEtag("etag3123477") 266 .build(); 267 mockService.addResponse(expectedResponse); 268 269 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 270 271 Budget actualResponse = client.getBudget(name); 272 Assert.assertEquals(expectedResponse, actualResponse); 273 274 List<String> actualRequests = mockService.getRequestPaths(); 275 Assert.assertEquals(1, actualRequests.size()); 276 277 String apiClientHeaderKey = 278 mockService 279 .getRequestHeaders() 280 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 281 .iterator() 282 .next(); 283 Assert.assertTrue( 284 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 285 .matcher(apiClientHeaderKey) 286 .matches()); 287 } 288 289 @Test getBudgetExceptionTest()290 public void getBudgetExceptionTest() throws Exception { 291 ApiException exception = 292 ApiExceptionFactory.createException( 293 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 294 mockService.addException(exception); 295 296 try { 297 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 298 client.getBudget(name); 299 Assert.fail("No exception raised"); 300 } catch (InvalidArgumentException e) { 301 // Expected exception. 302 } 303 } 304 305 @Test getBudgetTest2()306 public void getBudgetTest2() throws Exception { 307 Budget expectedResponse = 308 Budget.newBuilder() 309 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 310 .setDisplayName("displayName1714148973") 311 .setBudgetFilter(Filter.newBuilder().build()) 312 .setAmount(BudgetAmount.newBuilder().build()) 313 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 314 .setNotificationsRule(NotificationsRule.newBuilder().build()) 315 .setEtag("etag3123477") 316 .build(); 317 mockService.addResponse(expectedResponse); 318 319 String name = "billingAccounts/billingAccount-7221/budgets/budget-7221"; 320 321 Budget actualResponse = client.getBudget(name); 322 Assert.assertEquals(expectedResponse, actualResponse); 323 324 List<String> actualRequests = mockService.getRequestPaths(); 325 Assert.assertEquals(1, actualRequests.size()); 326 327 String apiClientHeaderKey = 328 mockService 329 .getRequestHeaders() 330 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 331 .iterator() 332 .next(); 333 Assert.assertTrue( 334 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 335 .matcher(apiClientHeaderKey) 336 .matches()); 337 } 338 339 @Test getBudgetExceptionTest2()340 public void getBudgetExceptionTest2() throws Exception { 341 ApiException exception = 342 ApiExceptionFactory.createException( 343 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 344 mockService.addException(exception); 345 346 try { 347 String name = "billingAccounts/billingAccount-7221/budgets/budget-7221"; 348 client.getBudget(name); 349 Assert.fail("No exception raised"); 350 } catch (InvalidArgumentException e) { 351 // Expected exception. 352 } 353 } 354 355 @Test listBudgetsTest()356 public void listBudgetsTest() throws Exception { 357 Budget responsesElement = Budget.newBuilder().build(); 358 ListBudgetsResponse expectedResponse = 359 ListBudgetsResponse.newBuilder() 360 .setNextPageToken("") 361 .addAllBudgets(Arrays.asList(responsesElement)) 362 .build(); 363 mockService.addResponse(expectedResponse); 364 365 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 366 367 ListBudgetsPagedResponse pagedListResponse = client.listBudgets(parent); 368 369 List<Budget> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 370 371 Assert.assertEquals(1, resources.size()); 372 Assert.assertEquals(expectedResponse.getBudgetsList().get(0), resources.get(0)); 373 374 List<String> actualRequests = mockService.getRequestPaths(); 375 Assert.assertEquals(1, actualRequests.size()); 376 377 String apiClientHeaderKey = 378 mockService 379 .getRequestHeaders() 380 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 381 .iterator() 382 .next(); 383 Assert.assertTrue( 384 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 385 .matcher(apiClientHeaderKey) 386 .matches()); 387 } 388 389 @Test listBudgetsExceptionTest()390 public void listBudgetsExceptionTest() throws Exception { 391 ApiException exception = 392 ApiExceptionFactory.createException( 393 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 394 mockService.addException(exception); 395 396 try { 397 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 398 client.listBudgets(parent); 399 Assert.fail("No exception raised"); 400 } catch (InvalidArgumentException e) { 401 // Expected exception. 402 } 403 } 404 405 @Test listBudgetsTest2()406 public void listBudgetsTest2() throws Exception { 407 Budget responsesElement = Budget.newBuilder().build(); 408 ListBudgetsResponse expectedResponse = 409 ListBudgetsResponse.newBuilder() 410 .setNextPageToken("") 411 .addAllBudgets(Arrays.asList(responsesElement)) 412 .build(); 413 mockService.addResponse(expectedResponse); 414 415 String parent = "billingAccounts/billingAccount-7950"; 416 417 ListBudgetsPagedResponse pagedListResponse = client.listBudgets(parent); 418 419 List<Budget> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 420 421 Assert.assertEquals(1, resources.size()); 422 Assert.assertEquals(expectedResponse.getBudgetsList().get(0), resources.get(0)); 423 424 List<String> actualRequests = mockService.getRequestPaths(); 425 Assert.assertEquals(1, actualRequests.size()); 426 427 String apiClientHeaderKey = 428 mockService 429 .getRequestHeaders() 430 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 431 .iterator() 432 .next(); 433 Assert.assertTrue( 434 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 435 .matcher(apiClientHeaderKey) 436 .matches()); 437 } 438 439 @Test listBudgetsExceptionTest2()440 public void listBudgetsExceptionTest2() throws Exception { 441 ApiException exception = 442 ApiExceptionFactory.createException( 443 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 444 mockService.addException(exception); 445 446 try { 447 String parent = "billingAccounts/billingAccount-7950"; 448 client.listBudgets(parent); 449 Assert.fail("No exception raised"); 450 } catch (InvalidArgumentException e) { 451 // Expected exception. 452 } 453 } 454 455 @Test deleteBudgetTest()456 public void deleteBudgetTest() throws Exception { 457 Empty expectedResponse = Empty.newBuilder().build(); 458 mockService.addResponse(expectedResponse); 459 460 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 461 462 client.deleteBudget(name); 463 464 List<String> actualRequests = mockService.getRequestPaths(); 465 Assert.assertEquals(1, actualRequests.size()); 466 467 String apiClientHeaderKey = 468 mockService 469 .getRequestHeaders() 470 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 471 .iterator() 472 .next(); 473 Assert.assertTrue( 474 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 475 .matcher(apiClientHeaderKey) 476 .matches()); 477 } 478 479 @Test deleteBudgetExceptionTest()480 public void deleteBudgetExceptionTest() throws Exception { 481 ApiException exception = 482 ApiExceptionFactory.createException( 483 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 484 mockService.addException(exception); 485 486 try { 487 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 488 client.deleteBudget(name); 489 Assert.fail("No exception raised"); 490 } catch (InvalidArgumentException e) { 491 // Expected exception. 492 } 493 } 494 495 @Test deleteBudgetTest2()496 public void deleteBudgetTest2() throws Exception { 497 Empty expectedResponse = Empty.newBuilder().build(); 498 mockService.addResponse(expectedResponse); 499 500 String name = "billingAccounts/billingAccount-7221/budgets/budget-7221"; 501 502 client.deleteBudget(name); 503 504 List<String> actualRequests = mockService.getRequestPaths(); 505 Assert.assertEquals(1, actualRequests.size()); 506 507 String apiClientHeaderKey = 508 mockService 509 .getRequestHeaders() 510 .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) 511 .iterator() 512 .next(); 513 Assert.assertTrue( 514 GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() 515 .matcher(apiClientHeaderKey) 516 .matches()); 517 } 518 519 @Test deleteBudgetExceptionTest2()520 public void deleteBudgetExceptionTest2() throws Exception { 521 ApiException exception = 522 ApiExceptionFactory.createException( 523 new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); 524 mockService.addException(exception); 525 526 try { 527 String name = "billingAccounts/billingAccount-7221/budgets/budget-7221"; 528 client.deleteBudget(name); 529 Assert.fail("No exception raised"); 530 } catch (InvalidArgumentException e) { 531 // Expected exception. 532 } 533 } 534 } 535