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.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.ArrayList; 35 import java.util.Arrays; 36 import java.util.List; 37 import java.util.UUID; 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 BudgetServiceClientTest { 48 private static MockBudgetService mockBudgetService; 49 private static MockServiceHelper mockServiceHelper; 50 private LocalChannelProvider channelProvider; 51 private BudgetServiceClient client; 52 53 @BeforeClass startStaticServer()54 public static void startStaticServer() { 55 mockBudgetService = new MockBudgetService(); 56 mockServiceHelper = 57 new MockServiceHelper( 58 UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(mockBudgetService)); 59 mockServiceHelper.start(); 60 } 61 62 @AfterClass stopServer()63 public static void stopServer() { 64 mockServiceHelper.stop(); 65 } 66 67 @Before setUp()68 public void setUp() throws IOException { 69 mockServiceHelper.reset(); 70 channelProvider = mockServiceHelper.createChannelProvider(); 71 BudgetServiceSettings settings = 72 BudgetServiceSettings.newBuilder() 73 .setTransportChannelProvider(channelProvider) 74 .setCredentialsProvider(NoCredentialsProvider.create()) 75 .build(); 76 client = BudgetServiceClient.create(settings); 77 } 78 79 @After tearDown()80 public void tearDown() throws Exception { 81 client.close(); 82 } 83 84 @Test createBudgetTest()85 public void createBudgetTest() throws Exception { 86 Budget expectedResponse = 87 Budget.newBuilder() 88 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 89 .setDisplayName("displayName1714148973") 90 .setBudgetFilter(Filter.newBuilder().build()) 91 .setAmount(BudgetAmount.newBuilder().build()) 92 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 93 .setNotificationsRule(NotificationsRule.newBuilder().build()) 94 .setEtag("etag3123477") 95 .build(); 96 mockBudgetService.addResponse(expectedResponse); 97 98 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 99 Budget budget = Budget.newBuilder().build(); 100 101 Budget actualResponse = client.createBudget(parent, budget); 102 Assert.assertEquals(expectedResponse, actualResponse); 103 104 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 105 Assert.assertEquals(1, actualRequests.size()); 106 CreateBudgetRequest actualRequest = ((CreateBudgetRequest) actualRequests.get(0)); 107 108 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 109 Assert.assertEquals(budget, actualRequest.getBudget()); 110 Assert.assertTrue( 111 channelProvider.isHeaderSent( 112 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 113 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 114 } 115 116 @Test createBudgetExceptionTest()117 public void createBudgetExceptionTest() throws Exception { 118 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 119 mockBudgetService.addException(exception); 120 121 try { 122 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 123 Budget budget = Budget.newBuilder().build(); 124 client.createBudget(parent, budget); 125 Assert.fail("No exception raised"); 126 } catch (InvalidArgumentException e) { 127 // Expected exception. 128 } 129 } 130 131 @Test createBudgetTest2()132 public void createBudgetTest2() throws Exception { 133 Budget expectedResponse = 134 Budget.newBuilder() 135 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 136 .setDisplayName("displayName1714148973") 137 .setBudgetFilter(Filter.newBuilder().build()) 138 .setAmount(BudgetAmount.newBuilder().build()) 139 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 140 .setNotificationsRule(NotificationsRule.newBuilder().build()) 141 .setEtag("etag3123477") 142 .build(); 143 mockBudgetService.addResponse(expectedResponse); 144 145 String parent = "parent-995424086"; 146 Budget budget = Budget.newBuilder().build(); 147 148 Budget actualResponse = client.createBudget(parent, budget); 149 Assert.assertEquals(expectedResponse, actualResponse); 150 151 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 152 Assert.assertEquals(1, actualRequests.size()); 153 CreateBudgetRequest actualRequest = ((CreateBudgetRequest) actualRequests.get(0)); 154 155 Assert.assertEquals(parent, actualRequest.getParent()); 156 Assert.assertEquals(budget, actualRequest.getBudget()); 157 Assert.assertTrue( 158 channelProvider.isHeaderSent( 159 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 160 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 161 } 162 163 @Test createBudgetExceptionTest2()164 public void createBudgetExceptionTest2() throws Exception { 165 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 166 mockBudgetService.addException(exception); 167 168 try { 169 String parent = "parent-995424086"; 170 Budget budget = Budget.newBuilder().build(); 171 client.createBudget(parent, budget); 172 Assert.fail("No exception raised"); 173 } catch (InvalidArgumentException e) { 174 // Expected exception. 175 } 176 } 177 178 @Test updateBudgetTest()179 public void updateBudgetTest() throws Exception { 180 Budget expectedResponse = 181 Budget.newBuilder() 182 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 183 .setDisplayName("displayName1714148973") 184 .setBudgetFilter(Filter.newBuilder().build()) 185 .setAmount(BudgetAmount.newBuilder().build()) 186 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 187 .setNotificationsRule(NotificationsRule.newBuilder().build()) 188 .setEtag("etag3123477") 189 .build(); 190 mockBudgetService.addResponse(expectedResponse); 191 192 Budget budget = Budget.newBuilder().build(); 193 FieldMask updateMask = FieldMask.newBuilder().build(); 194 195 Budget actualResponse = client.updateBudget(budget, updateMask); 196 Assert.assertEquals(expectedResponse, actualResponse); 197 198 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 199 Assert.assertEquals(1, actualRequests.size()); 200 UpdateBudgetRequest actualRequest = ((UpdateBudgetRequest) actualRequests.get(0)); 201 202 Assert.assertEquals(budget, actualRequest.getBudget()); 203 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 204 Assert.assertTrue( 205 channelProvider.isHeaderSent( 206 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 207 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 208 } 209 210 @Test updateBudgetExceptionTest()211 public void updateBudgetExceptionTest() throws Exception { 212 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 213 mockBudgetService.addException(exception); 214 215 try { 216 Budget budget = Budget.newBuilder().build(); 217 FieldMask updateMask = FieldMask.newBuilder().build(); 218 client.updateBudget(budget, updateMask); 219 Assert.fail("No exception raised"); 220 } catch (InvalidArgumentException e) { 221 // Expected exception. 222 } 223 } 224 225 @Test getBudgetTest()226 public void getBudgetTest() throws Exception { 227 Budget expectedResponse = 228 Budget.newBuilder() 229 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 230 .setDisplayName("displayName1714148973") 231 .setBudgetFilter(Filter.newBuilder().build()) 232 .setAmount(BudgetAmount.newBuilder().build()) 233 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 234 .setNotificationsRule(NotificationsRule.newBuilder().build()) 235 .setEtag("etag3123477") 236 .build(); 237 mockBudgetService.addResponse(expectedResponse); 238 239 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 240 241 Budget actualResponse = client.getBudget(name); 242 Assert.assertEquals(expectedResponse, actualResponse); 243 244 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 245 Assert.assertEquals(1, actualRequests.size()); 246 GetBudgetRequest actualRequest = ((GetBudgetRequest) actualRequests.get(0)); 247 248 Assert.assertEquals(name.toString(), actualRequest.getName()); 249 Assert.assertTrue( 250 channelProvider.isHeaderSent( 251 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 252 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 253 } 254 255 @Test getBudgetExceptionTest()256 public void getBudgetExceptionTest() throws Exception { 257 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 258 mockBudgetService.addException(exception); 259 260 try { 261 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 262 client.getBudget(name); 263 Assert.fail("No exception raised"); 264 } catch (InvalidArgumentException e) { 265 // Expected exception. 266 } 267 } 268 269 @Test getBudgetTest2()270 public void getBudgetTest2() throws Exception { 271 Budget expectedResponse = 272 Budget.newBuilder() 273 .setName(BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]").toString()) 274 .setDisplayName("displayName1714148973") 275 .setBudgetFilter(Filter.newBuilder().build()) 276 .setAmount(BudgetAmount.newBuilder().build()) 277 .addAllThresholdRules(new ArrayList<ThresholdRule>()) 278 .setNotificationsRule(NotificationsRule.newBuilder().build()) 279 .setEtag("etag3123477") 280 .build(); 281 mockBudgetService.addResponse(expectedResponse); 282 283 String name = "name3373707"; 284 285 Budget actualResponse = client.getBudget(name); 286 Assert.assertEquals(expectedResponse, actualResponse); 287 288 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 289 Assert.assertEquals(1, actualRequests.size()); 290 GetBudgetRequest actualRequest = ((GetBudgetRequest) actualRequests.get(0)); 291 292 Assert.assertEquals(name, actualRequest.getName()); 293 Assert.assertTrue( 294 channelProvider.isHeaderSent( 295 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 296 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 297 } 298 299 @Test getBudgetExceptionTest2()300 public void getBudgetExceptionTest2() throws Exception { 301 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 302 mockBudgetService.addException(exception); 303 304 try { 305 String name = "name3373707"; 306 client.getBudget(name); 307 Assert.fail("No exception raised"); 308 } catch (InvalidArgumentException e) { 309 // Expected exception. 310 } 311 } 312 313 @Test listBudgetsTest()314 public void listBudgetsTest() throws Exception { 315 Budget responsesElement = Budget.newBuilder().build(); 316 ListBudgetsResponse expectedResponse = 317 ListBudgetsResponse.newBuilder() 318 .setNextPageToken("") 319 .addAllBudgets(Arrays.asList(responsesElement)) 320 .build(); 321 mockBudgetService.addResponse(expectedResponse); 322 323 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 324 325 ListBudgetsPagedResponse pagedListResponse = client.listBudgets(parent); 326 327 List<Budget> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 328 329 Assert.assertEquals(1, resources.size()); 330 Assert.assertEquals(expectedResponse.getBudgetsList().get(0), resources.get(0)); 331 332 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 333 Assert.assertEquals(1, actualRequests.size()); 334 ListBudgetsRequest actualRequest = ((ListBudgetsRequest) actualRequests.get(0)); 335 336 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 337 Assert.assertTrue( 338 channelProvider.isHeaderSent( 339 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 340 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 341 } 342 343 @Test listBudgetsExceptionTest()344 public void listBudgetsExceptionTest() throws Exception { 345 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 346 mockBudgetService.addException(exception); 347 348 try { 349 BillingAccountName parent = BillingAccountName.of("[BILLING_ACCOUNT]"); 350 client.listBudgets(parent); 351 Assert.fail("No exception raised"); 352 } catch (InvalidArgumentException e) { 353 // Expected exception. 354 } 355 } 356 357 @Test listBudgetsTest2()358 public void listBudgetsTest2() throws Exception { 359 Budget responsesElement = Budget.newBuilder().build(); 360 ListBudgetsResponse expectedResponse = 361 ListBudgetsResponse.newBuilder() 362 .setNextPageToken("") 363 .addAllBudgets(Arrays.asList(responsesElement)) 364 .build(); 365 mockBudgetService.addResponse(expectedResponse); 366 367 String parent = "parent-995424086"; 368 369 ListBudgetsPagedResponse pagedListResponse = client.listBudgets(parent); 370 371 List<Budget> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 372 373 Assert.assertEquals(1, resources.size()); 374 Assert.assertEquals(expectedResponse.getBudgetsList().get(0), resources.get(0)); 375 376 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 377 Assert.assertEquals(1, actualRequests.size()); 378 ListBudgetsRequest actualRequest = ((ListBudgetsRequest) actualRequests.get(0)); 379 380 Assert.assertEquals(parent, actualRequest.getParent()); 381 Assert.assertTrue( 382 channelProvider.isHeaderSent( 383 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 384 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 385 } 386 387 @Test listBudgetsExceptionTest2()388 public void listBudgetsExceptionTest2() throws Exception { 389 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 390 mockBudgetService.addException(exception); 391 392 try { 393 String parent = "parent-995424086"; 394 client.listBudgets(parent); 395 Assert.fail("No exception raised"); 396 } catch (InvalidArgumentException e) { 397 // Expected exception. 398 } 399 } 400 401 @Test deleteBudgetTest()402 public void deleteBudgetTest() throws Exception { 403 Empty expectedResponse = Empty.newBuilder().build(); 404 mockBudgetService.addResponse(expectedResponse); 405 406 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 407 408 client.deleteBudget(name); 409 410 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 411 Assert.assertEquals(1, actualRequests.size()); 412 DeleteBudgetRequest actualRequest = ((DeleteBudgetRequest) actualRequests.get(0)); 413 414 Assert.assertEquals(name.toString(), actualRequest.getName()); 415 Assert.assertTrue( 416 channelProvider.isHeaderSent( 417 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 418 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 419 } 420 421 @Test deleteBudgetExceptionTest()422 public void deleteBudgetExceptionTest() throws Exception { 423 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 424 mockBudgetService.addException(exception); 425 426 try { 427 BudgetName name = BudgetName.of("[BILLING_ACCOUNT]", "[BUDGET]"); 428 client.deleteBudget(name); 429 Assert.fail("No exception raised"); 430 } catch (InvalidArgumentException e) { 431 // Expected exception. 432 } 433 } 434 435 @Test deleteBudgetTest2()436 public void deleteBudgetTest2() throws Exception { 437 Empty expectedResponse = Empty.newBuilder().build(); 438 mockBudgetService.addResponse(expectedResponse); 439 440 String name = "name3373707"; 441 442 client.deleteBudget(name); 443 444 List<AbstractMessage> actualRequests = mockBudgetService.getRequests(); 445 Assert.assertEquals(1, actualRequests.size()); 446 DeleteBudgetRequest actualRequest = ((DeleteBudgetRequest) actualRequests.get(0)); 447 448 Assert.assertEquals(name, actualRequest.getName()); 449 Assert.assertTrue( 450 channelProvider.isHeaderSent( 451 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 452 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 453 } 454 455 @Test deleteBudgetExceptionTest2()456 public void deleteBudgetExceptionTest2() throws Exception { 457 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 458 mockBudgetService.addException(exception); 459 460 try { 461 String name = "name3373707"; 462 client.deleteBudget(name); 463 Assert.fail("No exception raised"); 464 } catch (InvalidArgumentException e) { 465 // Expected exception. 466 } 467 } 468 } 469