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.dialogflow.cx.v3; 18 19 import static com.google.cloud.dialogflow.cx.v3.ExperimentsClient.ListExperimentsPagedResponse; 20 import static com.google.cloud.dialogflow.cx.v3.ExperimentsClient.ListLocationsPagedResponse; 21 22 import com.google.api.gax.core.NoCredentialsProvider; 23 import com.google.api.gax.grpc.GaxGrpcProperties; 24 import com.google.api.gax.grpc.testing.LocalChannelProvider; 25 import com.google.api.gax.grpc.testing.MockGrpcService; 26 import com.google.api.gax.grpc.testing.MockServiceHelper; 27 import com.google.api.gax.rpc.ApiClientHeaderProvider; 28 import com.google.api.gax.rpc.InvalidArgumentException; 29 import com.google.cloud.location.GetLocationRequest; 30 import com.google.cloud.location.ListLocationsRequest; 31 import com.google.cloud.location.ListLocationsResponse; 32 import com.google.cloud.location.Location; 33 import com.google.common.collect.Lists; 34 import com.google.protobuf.AbstractMessage; 35 import com.google.protobuf.Any; 36 import com.google.protobuf.Duration; 37 import com.google.protobuf.Empty; 38 import com.google.protobuf.FieldMask; 39 import com.google.protobuf.Timestamp; 40 import io.grpc.StatusRuntimeException; 41 import java.io.IOException; 42 import java.util.ArrayList; 43 import java.util.Arrays; 44 import java.util.HashMap; 45 import java.util.List; 46 import java.util.UUID; 47 import javax.annotation.Generated; 48 import org.junit.After; 49 import org.junit.AfterClass; 50 import org.junit.Assert; 51 import org.junit.Before; 52 import org.junit.BeforeClass; 53 import org.junit.Test; 54 55 @Generated("by gapic-generator-java") 56 public class ExperimentsClientTest { 57 private static MockExperiments mockExperiments; 58 private static MockLocations mockLocations; 59 private static MockServiceHelper mockServiceHelper; 60 private LocalChannelProvider channelProvider; 61 private ExperimentsClient client; 62 63 @BeforeClass startStaticServer()64 public static void startStaticServer() { 65 mockExperiments = new MockExperiments(); 66 mockLocations = new MockLocations(); 67 mockServiceHelper = 68 new MockServiceHelper( 69 UUID.randomUUID().toString(), 70 Arrays.<MockGrpcService>asList(mockExperiments, mockLocations)); 71 mockServiceHelper.start(); 72 } 73 74 @AfterClass stopServer()75 public static void stopServer() { 76 mockServiceHelper.stop(); 77 } 78 79 @Before setUp()80 public void setUp() throws IOException { 81 mockServiceHelper.reset(); 82 channelProvider = mockServiceHelper.createChannelProvider(); 83 ExperimentsSettings settings = 84 ExperimentsSettings.newBuilder() 85 .setTransportChannelProvider(channelProvider) 86 .setCredentialsProvider(NoCredentialsProvider.create()) 87 .build(); 88 client = ExperimentsClient.create(settings); 89 } 90 91 @After tearDown()92 public void tearDown() throws Exception { 93 client.close(); 94 } 95 96 @Test listExperimentsTest()97 public void listExperimentsTest() throws Exception { 98 Experiment responsesElement = Experiment.newBuilder().build(); 99 ListExperimentsResponse expectedResponse = 100 ListExperimentsResponse.newBuilder() 101 .setNextPageToken("") 102 .addAllExperiments(Arrays.asList(responsesElement)) 103 .build(); 104 mockExperiments.addResponse(expectedResponse); 105 106 EnvironmentName parent = 107 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 108 109 ListExperimentsPagedResponse pagedListResponse = client.listExperiments(parent); 110 111 List<Experiment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 112 113 Assert.assertEquals(1, resources.size()); 114 Assert.assertEquals(expectedResponse.getExperimentsList().get(0), resources.get(0)); 115 116 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 117 Assert.assertEquals(1, actualRequests.size()); 118 ListExperimentsRequest actualRequest = ((ListExperimentsRequest) actualRequests.get(0)); 119 120 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 121 Assert.assertTrue( 122 channelProvider.isHeaderSent( 123 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 124 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 125 } 126 127 @Test listExperimentsExceptionTest()128 public void listExperimentsExceptionTest() throws Exception { 129 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 130 mockExperiments.addException(exception); 131 132 try { 133 EnvironmentName parent = 134 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 135 client.listExperiments(parent); 136 Assert.fail("No exception raised"); 137 } catch (InvalidArgumentException e) { 138 // Expected exception. 139 } 140 } 141 142 @Test listExperimentsTest2()143 public void listExperimentsTest2() throws Exception { 144 Experiment responsesElement = Experiment.newBuilder().build(); 145 ListExperimentsResponse expectedResponse = 146 ListExperimentsResponse.newBuilder() 147 .setNextPageToken("") 148 .addAllExperiments(Arrays.asList(responsesElement)) 149 .build(); 150 mockExperiments.addResponse(expectedResponse); 151 152 String parent = "parent-995424086"; 153 154 ListExperimentsPagedResponse pagedListResponse = client.listExperiments(parent); 155 156 List<Experiment> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 157 158 Assert.assertEquals(1, resources.size()); 159 Assert.assertEquals(expectedResponse.getExperimentsList().get(0), resources.get(0)); 160 161 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 162 Assert.assertEquals(1, actualRequests.size()); 163 ListExperimentsRequest actualRequest = ((ListExperimentsRequest) actualRequests.get(0)); 164 165 Assert.assertEquals(parent, actualRequest.getParent()); 166 Assert.assertTrue( 167 channelProvider.isHeaderSent( 168 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 169 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 170 } 171 172 @Test listExperimentsExceptionTest2()173 public void listExperimentsExceptionTest2() throws Exception { 174 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 175 mockExperiments.addException(exception); 176 177 try { 178 String parent = "parent-995424086"; 179 client.listExperiments(parent); 180 Assert.fail("No exception raised"); 181 } catch (InvalidArgumentException e) { 182 // Expected exception. 183 } 184 } 185 186 @Test getExperimentTest()187 public void getExperimentTest() throws Exception { 188 Experiment expectedResponse = 189 Experiment.newBuilder() 190 .setName( 191 ExperimentName.of( 192 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 193 .toString()) 194 .setDisplayName("displayName1714148973") 195 .setDescription("description-1724546052") 196 .setDefinition(Experiment.Definition.newBuilder().build()) 197 .setRolloutConfig(RolloutConfig.newBuilder().build()) 198 .setRolloutState(RolloutState.newBuilder().build()) 199 .setRolloutFailureReason("rolloutFailureReason958797373") 200 .setResult(Experiment.Result.newBuilder().build()) 201 .setCreateTime(Timestamp.newBuilder().build()) 202 .setStartTime(Timestamp.newBuilder().build()) 203 .setEndTime(Timestamp.newBuilder().build()) 204 .setLastUpdateTime(Timestamp.newBuilder().build()) 205 .setExperimentLength(Duration.newBuilder().build()) 206 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 207 .build(); 208 mockExperiments.addResponse(expectedResponse); 209 210 ExperimentName name = 211 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 212 213 Experiment actualResponse = client.getExperiment(name); 214 Assert.assertEquals(expectedResponse, actualResponse); 215 216 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 217 Assert.assertEquals(1, actualRequests.size()); 218 GetExperimentRequest actualRequest = ((GetExperimentRequest) actualRequests.get(0)); 219 220 Assert.assertEquals(name.toString(), actualRequest.getName()); 221 Assert.assertTrue( 222 channelProvider.isHeaderSent( 223 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 224 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 225 } 226 227 @Test getExperimentExceptionTest()228 public void getExperimentExceptionTest() throws Exception { 229 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 230 mockExperiments.addException(exception); 231 232 try { 233 ExperimentName name = 234 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 235 client.getExperiment(name); 236 Assert.fail("No exception raised"); 237 } catch (InvalidArgumentException e) { 238 // Expected exception. 239 } 240 } 241 242 @Test getExperimentTest2()243 public void getExperimentTest2() throws Exception { 244 Experiment expectedResponse = 245 Experiment.newBuilder() 246 .setName( 247 ExperimentName.of( 248 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 249 .toString()) 250 .setDisplayName("displayName1714148973") 251 .setDescription("description-1724546052") 252 .setDefinition(Experiment.Definition.newBuilder().build()) 253 .setRolloutConfig(RolloutConfig.newBuilder().build()) 254 .setRolloutState(RolloutState.newBuilder().build()) 255 .setRolloutFailureReason("rolloutFailureReason958797373") 256 .setResult(Experiment.Result.newBuilder().build()) 257 .setCreateTime(Timestamp.newBuilder().build()) 258 .setStartTime(Timestamp.newBuilder().build()) 259 .setEndTime(Timestamp.newBuilder().build()) 260 .setLastUpdateTime(Timestamp.newBuilder().build()) 261 .setExperimentLength(Duration.newBuilder().build()) 262 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 263 .build(); 264 mockExperiments.addResponse(expectedResponse); 265 266 String name = "name3373707"; 267 268 Experiment actualResponse = client.getExperiment(name); 269 Assert.assertEquals(expectedResponse, actualResponse); 270 271 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 272 Assert.assertEquals(1, actualRequests.size()); 273 GetExperimentRequest actualRequest = ((GetExperimentRequest) actualRequests.get(0)); 274 275 Assert.assertEquals(name, actualRequest.getName()); 276 Assert.assertTrue( 277 channelProvider.isHeaderSent( 278 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 279 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 280 } 281 282 @Test getExperimentExceptionTest2()283 public void getExperimentExceptionTest2() throws Exception { 284 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 285 mockExperiments.addException(exception); 286 287 try { 288 String name = "name3373707"; 289 client.getExperiment(name); 290 Assert.fail("No exception raised"); 291 } catch (InvalidArgumentException e) { 292 // Expected exception. 293 } 294 } 295 296 @Test createExperimentTest()297 public void createExperimentTest() throws Exception { 298 Experiment expectedResponse = 299 Experiment.newBuilder() 300 .setName( 301 ExperimentName.of( 302 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 303 .toString()) 304 .setDisplayName("displayName1714148973") 305 .setDescription("description-1724546052") 306 .setDefinition(Experiment.Definition.newBuilder().build()) 307 .setRolloutConfig(RolloutConfig.newBuilder().build()) 308 .setRolloutState(RolloutState.newBuilder().build()) 309 .setRolloutFailureReason("rolloutFailureReason958797373") 310 .setResult(Experiment.Result.newBuilder().build()) 311 .setCreateTime(Timestamp.newBuilder().build()) 312 .setStartTime(Timestamp.newBuilder().build()) 313 .setEndTime(Timestamp.newBuilder().build()) 314 .setLastUpdateTime(Timestamp.newBuilder().build()) 315 .setExperimentLength(Duration.newBuilder().build()) 316 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 317 .build(); 318 mockExperiments.addResponse(expectedResponse); 319 320 EnvironmentName parent = 321 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 322 Experiment experiment = Experiment.newBuilder().build(); 323 324 Experiment actualResponse = client.createExperiment(parent, experiment); 325 Assert.assertEquals(expectedResponse, actualResponse); 326 327 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 328 Assert.assertEquals(1, actualRequests.size()); 329 CreateExperimentRequest actualRequest = ((CreateExperimentRequest) actualRequests.get(0)); 330 331 Assert.assertEquals(parent.toString(), actualRequest.getParent()); 332 Assert.assertEquals(experiment, actualRequest.getExperiment()); 333 Assert.assertTrue( 334 channelProvider.isHeaderSent( 335 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 336 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 337 } 338 339 @Test createExperimentExceptionTest()340 public void createExperimentExceptionTest() throws Exception { 341 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 342 mockExperiments.addException(exception); 343 344 try { 345 EnvironmentName parent = 346 EnvironmentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]"); 347 Experiment experiment = Experiment.newBuilder().build(); 348 client.createExperiment(parent, experiment); 349 Assert.fail("No exception raised"); 350 } catch (InvalidArgumentException e) { 351 // Expected exception. 352 } 353 } 354 355 @Test createExperimentTest2()356 public void createExperimentTest2() throws Exception { 357 Experiment expectedResponse = 358 Experiment.newBuilder() 359 .setName( 360 ExperimentName.of( 361 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 362 .toString()) 363 .setDisplayName("displayName1714148973") 364 .setDescription("description-1724546052") 365 .setDefinition(Experiment.Definition.newBuilder().build()) 366 .setRolloutConfig(RolloutConfig.newBuilder().build()) 367 .setRolloutState(RolloutState.newBuilder().build()) 368 .setRolloutFailureReason("rolloutFailureReason958797373") 369 .setResult(Experiment.Result.newBuilder().build()) 370 .setCreateTime(Timestamp.newBuilder().build()) 371 .setStartTime(Timestamp.newBuilder().build()) 372 .setEndTime(Timestamp.newBuilder().build()) 373 .setLastUpdateTime(Timestamp.newBuilder().build()) 374 .setExperimentLength(Duration.newBuilder().build()) 375 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 376 .build(); 377 mockExperiments.addResponse(expectedResponse); 378 379 String parent = "parent-995424086"; 380 Experiment experiment = Experiment.newBuilder().build(); 381 382 Experiment actualResponse = client.createExperiment(parent, experiment); 383 Assert.assertEquals(expectedResponse, actualResponse); 384 385 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 386 Assert.assertEquals(1, actualRequests.size()); 387 CreateExperimentRequest actualRequest = ((CreateExperimentRequest) actualRequests.get(0)); 388 389 Assert.assertEquals(parent, actualRequest.getParent()); 390 Assert.assertEquals(experiment, actualRequest.getExperiment()); 391 Assert.assertTrue( 392 channelProvider.isHeaderSent( 393 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 394 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 395 } 396 397 @Test createExperimentExceptionTest2()398 public void createExperimentExceptionTest2() throws Exception { 399 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 400 mockExperiments.addException(exception); 401 402 try { 403 String parent = "parent-995424086"; 404 Experiment experiment = Experiment.newBuilder().build(); 405 client.createExperiment(parent, experiment); 406 Assert.fail("No exception raised"); 407 } catch (InvalidArgumentException e) { 408 // Expected exception. 409 } 410 } 411 412 @Test updateExperimentTest()413 public void updateExperimentTest() throws Exception { 414 Experiment expectedResponse = 415 Experiment.newBuilder() 416 .setName( 417 ExperimentName.of( 418 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 419 .toString()) 420 .setDisplayName("displayName1714148973") 421 .setDescription("description-1724546052") 422 .setDefinition(Experiment.Definition.newBuilder().build()) 423 .setRolloutConfig(RolloutConfig.newBuilder().build()) 424 .setRolloutState(RolloutState.newBuilder().build()) 425 .setRolloutFailureReason("rolloutFailureReason958797373") 426 .setResult(Experiment.Result.newBuilder().build()) 427 .setCreateTime(Timestamp.newBuilder().build()) 428 .setStartTime(Timestamp.newBuilder().build()) 429 .setEndTime(Timestamp.newBuilder().build()) 430 .setLastUpdateTime(Timestamp.newBuilder().build()) 431 .setExperimentLength(Duration.newBuilder().build()) 432 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 433 .build(); 434 mockExperiments.addResponse(expectedResponse); 435 436 Experiment experiment = Experiment.newBuilder().build(); 437 FieldMask updateMask = FieldMask.newBuilder().build(); 438 439 Experiment actualResponse = client.updateExperiment(experiment, updateMask); 440 Assert.assertEquals(expectedResponse, actualResponse); 441 442 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 443 Assert.assertEquals(1, actualRequests.size()); 444 UpdateExperimentRequest actualRequest = ((UpdateExperimentRequest) actualRequests.get(0)); 445 446 Assert.assertEquals(experiment, actualRequest.getExperiment()); 447 Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); 448 Assert.assertTrue( 449 channelProvider.isHeaderSent( 450 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 451 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 452 } 453 454 @Test updateExperimentExceptionTest()455 public void updateExperimentExceptionTest() throws Exception { 456 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 457 mockExperiments.addException(exception); 458 459 try { 460 Experiment experiment = Experiment.newBuilder().build(); 461 FieldMask updateMask = FieldMask.newBuilder().build(); 462 client.updateExperiment(experiment, updateMask); 463 Assert.fail("No exception raised"); 464 } catch (InvalidArgumentException e) { 465 // Expected exception. 466 } 467 } 468 469 @Test deleteExperimentTest()470 public void deleteExperimentTest() throws Exception { 471 Empty expectedResponse = Empty.newBuilder().build(); 472 mockExperiments.addResponse(expectedResponse); 473 474 ExperimentName name = 475 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 476 477 client.deleteExperiment(name); 478 479 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 480 Assert.assertEquals(1, actualRequests.size()); 481 DeleteExperimentRequest actualRequest = ((DeleteExperimentRequest) actualRequests.get(0)); 482 483 Assert.assertEquals(name.toString(), actualRequest.getName()); 484 Assert.assertTrue( 485 channelProvider.isHeaderSent( 486 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 487 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 488 } 489 490 @Test deleteExperimentExceptionTest()491 public void deleteExperimentExceptionTest() throws Exception { 492 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 493 mockExperiments.addException(exception); 494 495 try { 496 ExperimentName name = 497 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 498 client.deleteExperiment(name); 499 Assert.fail("No exception raised"); 500 } catch (InvalidArgumentException e) { 501 // Expected exception. 502 } 503 } 504 505 @Test deleteExperimentTest2()506 public void deleteExperimentTest2() throws Exception { 507 Empty expectedResponse = Empty.newBuilder().build(); 508 mockExperiments.addResponse(expectedResponse); 509 510 String name = "name3373707"; 511 512 client.deleteExperiment(name); 513 514 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 515 Assert.assertEquals(1, actualRequests.size()); 516 DeleteExperimentRequest actualRequest = ((DeleteExperimentRequest) actualRequests.get(0)); 517 518 Assert.assertEquals(name, actualRequest.getName()); 519 Assert.assertTrue( 520 channelProvider.isHeaderSent( 521 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 522 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 523 } 524 525 @Test deleteExperimentExceptionTest2()526 public void deleteExperimentExceptionTest2() throws Exception { 527 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 528 mockExperiments.addException(exception); 529 530 try { 531 String name = "name3373707"; 532 client.deleteExperiment(name); 533 Assert.fail("No exception raised"); 534 } catch (InvalidArgumentException e) { 535 // Expected exception. 536 } 537 } 538 539 @Test startExperimentTest()540 public void startExperimentTest() throws Exception { 541 Experiment expectedResponse = 542 Experiment.newBuilder() 543 .setName( 544 ExperimentName.of( 545 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 546 .toString()) 547 .setDisplayName("displayName1714148973") 548 .setDescription("description-1724546052") 549 .setDefinition(Experiment.Definition.newBuilder().build()) 550 .setRolloutConfig(RolloutConfig.newBuilder().build()) 551 .setRolloutState(RolloutState.newBuilder().build()) 552 .setRolloutFailureReason("rolloutFailureReason958797373") 553 .setResult(Experiment.Result.newBuilder().build()) 554 .setCreateTime(Timestamp.newBuilder().build()) 555 .setStartTime(Timestamp.newBuilder().build()) 556 .setEndTime(Timestamp.newBuilder().build()) 557 .setLastUpdateTime(Timestamp.newBuilder().build()) 558 .setExperimentLength(Duration.newBuilder().build()) 559 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 560 .build(); 561 mockExperiments.addResponse(expectedResponse); 562 563 ExperimentName name = 564 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 565 566 Experiment actualResponse = client.startExperiment(name); 567 Assert.assertEquals(expectedResponse, actualResponse); 568 569 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 570 Assert.assertEquals(1, actualRequests.size()); 571 StartExperimentRequest actualRequest = ((StartExperimentRequest) actualRequests.get(0)); 572 573 Assert.assertEquals(name.toString(), actualRequest.getName()); 574 Assert.assertTrue( 575 channelProvider.isHeaderSent( 576 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 577 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 578 } 579 580 @Test startExperimentExceptionTest()581 public void startExperimentExceptionTest() throws Exception { 582 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 583 mockExperiments.addException(exception); 584 585 try { 586 ExperimentName name = 587 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 588 client.startExperiment(name); 589 Assert.fail("No exception raised"); 590 } catch (InvalidArgumentException e) { 591 // Expected exception. 592 } 593 } 594 595 @Test startExperimentTest2()596 public void startExperimentTest2() throws Exception { 597 Experiment expectedResponse = 598 Experiment.newBuilder() 599 .setName( 600 ExperimentName.of( 601 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 602 .toString()) 603 .setDisplayName("displayName1714148973") 604 .setDescription("description-1724546052") 605 .setDefinition(Experiment.Definition.newBuilder().build()) 606 .setRolloutConfig(RolloutConfig.newBuilder().build()) 607 .setRolloutState(RolloutState.newBuilder().build()) 608 .setRolloutFailureReason("rolloutFailureReason958797373") 609 .setResult(Experiment.Result.newBuilder().build()) 610 .setCreateTime(Timestamp.newBuilder().build()) 611 .setStartTime(Timestamp.newBuilder().build()) 612 .setEndTime(Timestamp.newBuilder().build()) 613 .setLastUpdateTime(Timestamp.newBuilder().build()) 614 .setExperimentLength(Duration.newBuilder().build()) 615 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 616 .build(); 617 mockExperiments.addResponse(expectedResponse); 618 619 String name = "name3373707"; 620 621 Experiment actualResponse = client.startExperiment(name); 622 Assert.assertEquals(expectedResponse, actualResponse); 623 624 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 625 Assert.assertEquals(1, actualRequests.size()); 626 StartExperimentRequest actualRequest = ((StartExperimentRequest) actualRequests.get(0)); 627 628 Assert.assertEquals(name, actualRequest.getName()); 629 Assert.assertTrue( 630 channelProvider.isHeaderSent( 631 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 632 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 633 } 634 635 @Test startExperimentExceptionTest2()636 public void startExperimentExceptionTest2() throws Exception { 637 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 638 mockExperiments.addException(exception); 639 640 try { 641 String name = "name3373707"; 642 client.startExperiment(name); 643 Assert.fail("No exception raised"); 644 } catch (InvalidArgumentException e) { 645 // Expected exception. 646 } 647 } 648 649 @Test stopExperimentTest()650 public void stopExperimentTest() throws Exception { 651 Experiment expectedResponse = 652 Experiment.newBuilder() 653 .setName( 654 ExperimentName.of( 655 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 656 .toString()) 657 .setDisplayName("displayName1714148973") 658 .setDescription("description-1724546052") 659 .setDefinition(Experiment.Definition.newBuilder().build()) 660 .setRolloutConfig(RolloutConfig.newBuilder().build()) 661 .setRolloutState(RolloutState.newBuilder().build()) 662 .setRolloutFailureReason("rolloutFailureReason958797373") 663 .setResult(Experiment.Result.newBuilder().build()) 664 .setCreateTime(Timestamp.newBuilder().build()) 665 .setStartTime(Timestamp.newBuilder().build()) 666 .setEndTime(Timestamp.newBuilder().build()) 667 .setLastUpdateTime(Timestamp.newBuilder().build()) 668 .setExperimentLength(Duration.newBuilder().build()) 669 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 670 .build(); 671 mockExperiments.addResponse(expectedResponse); 672 673 ExperimentName name = 674 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 675 676 Experiment actualResponse = client.stopExperiment(name); 677 Assert.assertEquals(expectedResponse, actualResponse); 678 679 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 680 Assert.assertEquals(1, actualRequests.size()); 681 StopExperimentRequest actualRequest = ((StopExperimentRequest) actualRequests.get(0)); 682 683 Assert.assertEquals(name.toString(), actualRequest.getName()); 684 Assert.assertTrue( 685 channelProvider.isHeaderSent( 686 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 687 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 688 } 689 690 @Test stopExperimentExceptionTest()691 public void stopExperimentExceptionTest() throws Exception { 692 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 693 mockExperiments.addException(exception); 694 695 try { 696 ExperimentName name = 697 ExperimentName.of("[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]"); 698 client.stopExperiment(name); 699 Assert.fail("No exception raised"); 700 } catch (InvalidArgumentException e) { 701 // Expected exception. 702 } 703 } 704 705 @Test stopExperimentTest2()706 public void stopExperimentTest2() throws Exception { 707 Experiment expectedResponse = 708 Experiment.newBuilder() 709 .setName( 710 ExperimentName.of( 711 "[PROJECT]", "[LOCATION]", "[AGENT]", "[ENVIRONMENT]", "[EXPERIMENT]") 712 .toString()) 713 .setDisplayName("displayName1714148973") 714 .setDescription("description-1724546052") 715 .setDefinition(Experiment.Definition.newBuilder().build()) 716 .setRolloutConfig(RolloutConfig.newBuilder().build()) 717 .setRolloutState(RolloutState.newBuilder().build()) 718 .setRolloutFailureReason("rolloutFailureReason958797373") 719 .setResult(Experiment.Result.newBuilder().build()) 720 .setCreateTime(Timestamp.newBuilder().build()) 721 .setStartTime(Timestamp.newBuilder().build()) 722 .setEndTime(Timestamp.newBuilder().build()) 723 .setLastUpdateTime(Timestamp.newBuilder().build()) 724 .setExperimentLength(Duration.newBuilder().build()) 725 .addAllVariantsHistory(new ArrayList<VariantsHistory>()) 726 .build(); 727 mockExperiments.addResponse(expectedResponse); 728 729 String name = "name3373707"; 730 731 Experiment actualResponse = client.stopExperiment(name); 732 Assert.assertEquals(expectedResponse, actualResponse); 733 734 List<AbstractMessage> actualRequests = mockExperiments.getRequests(); 735 Assert.assertEquals(1, actualRequests.size()); 736 StopExperimentRequest actualRequest = ((StopExperimentRequest) actualRequests.get(0)); 737 738 Assert.assertEquals(name, actualRequest.getName()); 739 Assert.assertTrue( 740 channelProvider.isHeaderSent( 741 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 742 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 743 } 744 745 @Test stopExperimentExceptionTest2()746 public void stopExperimentExceptionTest2() throws Exception { 747 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 748 mockExperiments.addException(exception); 749 750 try { 751 String name = "name3373707"; 752 client.stopExperiment(name); 753 Assert.fail("No exception raised"); 754 } catch (InvalidArgumentException e) { 755 // Expected exception. 756 } 757 } 758 759 @Test listLocationsTest()760 public void listLocationsTest() throws Exception { 761 Location responsesElement = Location.newBuilder().build(); 762 ListLocationsResponse expectedResponse = 763 ListLocationsResponse.newBuilder() 764 .setNextPageToken("") 765 .addAllLocations(Arrays.asList(responsesElement)) 766 .build(); 767 mockLocations.addResponse(expectedResponse); 768 769 ListLocationsRequest request = 770 ListLocationsRequest.newBuilder() 771 .setName("name3373707") 772 .setFilter("filter-1274492040") 773 .setPageSize(883849137) 774 .setPageToken("pageToken873572522") 775 .build(); 776 777 ListLocationsPagedResponse pagedListResponse = client.listLocations(request); 778 779 List<Location> resources = Lists.newArrayList(pagedListResponse.iterateAll()); 780 781 Assert.assertEquals(1, resources.size()); 782 Assert.assertEquals(expectedResponse.getLocationsList().get(0), resources.get(0)); 783 784 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 785 Assert.assertEquals(1, actualRequests.size()); 786 ListLocationsRequest actualRequest = ((ListLocationsRequest) actualRequests.get(0)); 787 788 Assert.assertEquals(request.getName(), actualRequest.getName()); 789 Assert.assertEquals(request.getFilter(), actualRequest.getFilter()); 790 Assert.assertEquals(request.getPageSize(), actualRequest.getPageSize()); 791 Assert.assertEquals(request.getPageToken(), actualRequest.getPageToken()); 792 Assert.assertTrue( 793 channelProvider.isHeaderSent( 794 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 795 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 796 } 797 798 @Test listLocationsExceptionTest()799 public void listLocationsExceptionTest() throws Exception { 800 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 801 mockLocations.addException(exception); 802 803 try { 804 ListLocationsRequest request = 805 ListLocationsRequest.newBuilder() 806 .setName("name3373707") 807 .setFilter("filter-1274492040") 808 .setPageSize(883849137) 809 .setPageToken("pageToken873572522") 810 .build(); 811 client.listLocations(request); 812 Assert.fail("No exception raised"); 813 } catch (InvalidArgumentException e) { 814 // Expected exception. 815 } 816 } 817 818 @Test getLocationTest()819 public void getLocationTest() throws Exception { 820 Location expectedResponse = 821 Location.newBuilder() 822 .setName("name3373707") 823 .setLocationId("locationId1541836720") 824 .setDisplayName("displayName1714148973") 825 .putAllLabels(new HashMap<String, String>()) 826 .setMetadata(Any.newBuilder().build()) 827 .build(); 828 mockLocations.addResponse(expectedResponse); 829 830 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 831 832 Location actualResponse = client.getLocation(request); 833 Assert.assertEquals(expectedResponse, actualResponse); 834 835 List<AbstractMessage> actualRequests = mockLocations.getRequests(); 836 Assert.assertEquals(1, actualRequests.size()); 837 GetLocationRequest actualRequest = ((GetLocationRequest) actualRequests.get(0)); 838 839 Assert.assertEquals(request.getName(), actualRequest.getName()); 840 Assert.assertTrue( 841 channelProvider.isHeaderSent( 842 ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), 843 GaxGrpcProperties.getDefaultApiClientHeaderPattern())); 844 } 845 846 @Test getLocationExceptionTest()847 public void getLocationExceptionTest() throws Exception { 848 StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT); 849 mockLocations.addException(exception); 850 851 try { 852 GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build(); 853 client.getLocation(request); 854 Assert.fail("No exception raised"); 855 } catch (InvalidArgumentException e) { 856 // Expected exception. 857 } 858 } 859 } 860