@@ -1527,7 +1527,7 @@ public void getVariationCmabExperimentServiceError() {
15271527 cmabExperiment .getKey (),
15281528 cmabExperiment .getStatus (),
15291529 cmabExperiment .getLayerId (),
1530- cmabExperiment .getAudienceIds (),
1530+ cmabExperiment .getAudienceIds (),
15311531 cmabExperiment .getAudienceConditions (),
15321532 cmabExperiment .getVariations (),
15331533 Collections .emptyMap (), // No whitelisting
@@ -1574,65 +1574,69 @@ public void getVariationCmabExperimentServiceError() {
15741574 */
15751575 @ Test
15761576 public void getVariationCmabExperimentServiceSuccess () {
1577- // Create a CMAB experiment
1578- Experiment cmabExperiment = createMockCmabExperiment ( );
1579- Variation expectedVariation = cmabExperiment .getVariations ().get (1 ); // Use second variation
1577+ // Use an existing experiment from v4ProjectConfig and modify it to be CMAB
1578+ Experiment baseExperiment = v4ProjectConfig . getExperiments (). get ( 0 );
1579+ Variation expectedVariation = baseExperiment .getVariations ().get (0 );
15801580
15811581 // Create mock Cmab object
15821582 Cmab mockCmab = mock (Cmab .class );
1583- when (mockCmab .getTrafficAllocation ()).thenReturn (4000 );
1583+ when (mockCmab .getTrafficAllocation ()).thenReturn (10000 ); // 100% allocation
15841584
1585- // Create experiment with CMAB config (no whitelisting, no forced variations)
1586- Experiment experiment = new Experiment (
1587- cmabExperiment .getId (),
1588- cmabExperiment .getKey (),
1589- cmabExperiment .getStatus (),
1590- cmabExperiment .getLayerId (),
1591- cmabExperiment .getAudienceIds (),
1592- cmabExperiment .getAudienceConditions (),
1593- cmabExperiment .getVariations (),
1585+ // Create CMAB experiment using existing experiment structure
1586+ Experiment cmabExperiment = new Experiment (
1587+ baseExperiment .getId (),
1588+ baseExperiment .getKey (),
1589+ baseExperiment .getStatus (),
1590+ baseExperiment .getLayerId (),
1591+ baseExperiment .getAudienceIds (),
1592+ baseExperiment .getAudienceConditions (),
1593+ baseExperiment .getVariations (),
15941594 Collections .emptyMap (), // No whitelisting
1595- cmabExperiment .getTrafficAllocation (),
1595+ baseExperiment .getTrafficAllocation (),
15961596 mockCmab // This makes it a CMAB experiment
15971597 );
15981598
1599+ // Mock bucketer to return a variation (user is in CMAB traffic)
1600+ Variation bucketedVariation = new Variation ("$" , "$" );
15991601 Bucketer mockBucketer = mock (Bucketer .class );
1600- when (mockBucketer .bucketForCmab (any (Experiment .class ), anyString (), any (ProjectConfig .class )))
1601- .thenReturn (DecisionResponse .responseNoReasons ("$" ));
1602+ when (mockBucketer .bucket (any (Experiment .class ), anyString (), any (ProjectConfig .class ), eq (true )))
1603+ .thenReturn (DecisionResponse .responseNoReasons (bucketedVariation ));
1604+
16021605 DecisionService decisionServiceWithMockCmabService = new DecisionService (
16031606 mockBucketer ,
16041607 mockErrorHandler ,
16051608 null ,
16061609 mockCmabService
16071610 );
16081611
1609- // Mock CmabService.getDecision to return a valid decision
1612+ // Mock CmabService.getDecision to return the expected variation ID
16101613 CmabDecision mockCmabDecision = mock (CmabDecision .class );
16111614 when (mockCmabDecision .getVariationId ()).thenReturn (expectedVariation .getId ());
16121615 when (mockCmabService .getDecision (any (), any (), any (), any ()))
16131616 .thenReturn (mockCmabDecision );
16141617
16151618 // Call getVariation
16161619 DecisionResponse <Variation > result = decisionServiceWithMockCmabService .getVariation (
1617- experiment ,
1620+ cmabExperiment ,
16181621 optimizely .createUserContext (genericUserId , Collections .emptyMap ()),
16191622 v4ProjectConfig
16201623 );
16211624
16221625 // Verify that CMAB service decision is returned
1626+ assertNotNull ("Result should not be null" , result .getResult ());
16231627 assertEquals (expectedVariation , result .getResult ());
16241628
16251629 // Verify that the result is not an error
16261630 assertFalse (result .isError ());
16271631
1628- // Assert that CmabService.getDecision was called exactly once
1632+ // Verify CmabService.getDecision was called
16291633 verify (mockCmabService , times (1 )).getDecision (any (), any (), any (), any ());
16301634
16311635 // Verify that the correct parameters were passed to CMAB service
16321636 verify (mockCmabService ).getDecision (
16331637 eq (v4ProjectConfig ),
16341638 any (OptimizelyUserContext .class ),
1635- eq (experiment .getId ()),
1639+ eq (cmabExperiment .getId ()),
16361640 any (List .class )
16371641 );
16381642 }
@@ -1648,7 +1652,7 @@ public void getVariationCmabExperimentUserNotInTrafficAllocation() {
16481652
16491653 // Create mock Cmab object
16501654 Cmab mockCmab = mock (Cmab .class );
1651- when (mockCmab .getTrafficAllocation ()).thenReturn (5000 ); // 50% traffic allocation
1655+ when (mockCmab .getTrafficAllocation ()).thenReturn (5000 );
16521656
16531657 // Create experiment with CMAB config (no whitelisting, no forced variations)
16541658 Experiment experiment = new Experiment (
@@ -1666,7 +1670,7 @@ public void getVariationCmabExperimentUserNotInTrafficAllocation() {
16661670
16671671 // Mock bucketer to return null for CMAB allocation (user not in CMAB traffic)
16681672 Bucketer mockBucketer = mock (Bucketer .class );
1669- when (mockBucketer .bucketForCmab (any (Experiment .class ), anyString (), any (ProjectConfig .class )))
1673+ when (mockBucketer .bucket (any (Experiment .class ), anyString (), any (ProjectConfig .class ), eq ( true )))
16701674 .thenReturn (DecisionResponse .nullNoReasons ());
16711675
16721676 DecisionService decisionServiceWithMockCmabService = new DecisionService (
@@ -1693,7 +1697,7 @@ public void getVariationCmabExperimentUserNotInTrafficAllocation() {
16931697 verify (mockCmabService , never ()).getDecision (any (), any (), any (), any ());
16941698
16951699 // Verify that bucketer was called for CMAB allocation
1696- verify (mockBucketer , times (1 )).bucketForCmab (any (Experiment .class ), anyString (), any (ProjectConfig .class ));
1700+ verify (mockBucketer , times (1 )).bucket (any (Experiment .class ), anyString (), any (ProjectConfig .class ), eq ( true ));
16971701 }
16981702
16991703 private Experiment createMockCmabExperiment () {
0 commit comments