|
37 | 37 | import com.optimizely.ab.odp.ODPEvent; |
38 | 38 | import com.optimizely.ab.odp.ODPEventManager; |
39 | 39 | import com.optimizely.ab.odp.ODPManager; |
| 40 | +import com.optimizely.ab.optimizelydecision.DecisionReasons; |
40 | 41 | import com.optimizely.ab.optimizelydecision.DecisionResponse; |
| 42 | +import com.optimizely.ab.optimizelydecision.DefaultDecisionReasons; |
| 43 | +import com.optimizely.ab.optimizelydecision.OptimizelyDecision; |
41 | 44 | import com.optimizely.ab.optimizelyjson.OptimizelyJSON; |
42 | 45 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
43 | 46 | import org.junit.Before; |
@@ -4993,4 +4996,55 @@ public void identifyUser() { |
4993 | 4996 | optimizely.identifyUser("the-user"); |
4994 | 4997 | Mockito.verify(mockODPEventManager, times(1)).identifyUser("the-user"); |
4995 | 4998 | } |
| 4999 | + |
| 5000 | + @Test |
| 5001 | + public void testDecideReturnsErrorDecisionWhenDecisionServiceFails() throws Exception { |
| 5002 | + assumeTrue(datafileVersion >= Integer.parseInt(ProjectConfig.Version.V4.toString())); |
| 5003 | + |
| 5004 | + // Use the CMAB datafile |
| 5005 | + Optimizely optimizely = Optimizely.builder() |
| 5006 | + .withDatafile(validConfigJsonCMAB()) |
| 5007 | + .withDecisionService(mockDecisionService) |
| 5008 | + .build(); |
| 5009 | + |
| 5010 | + // Mock decision service to return an error from CMAB |
| 5011 | + DecisionReasons reasons = new DefaultDecisionReasons(); |
| 5012 | + FeatureDecision errorFeatureDecision = new FeatureDecision(null, null, FeatureDecision.DecisionSource.ROLLOUT); |
| 5013 | + DecisionResponse<FeatureDecision> errorDecisionResponse = new DecisionResponse<>( |
| 5014 | + errorFeatureDecision, |
| 5015 | + reasons, |
| 5016 | + true, |
| 5017 | + null |
| 5018 | + ); |
| 5019 | + |
| 5020 | + // Mock validatedForcedDecision to return no forced decision (but not null!) |
| 5021 | + DecisionResponse<Variation> noForcedDecision = new DecisionResponse<>(null, new DefaultDecisionReasons()); |
| 5022 | + when(mockDecisionService.validatedForcedDecision( |
| 5023 | + any(OptimizelyDecisionContext.class), |
| 5024 | + any(ProjectConfig.class), |
| 5025 | + any(OptimizelyUserContext.class) |
| 5026 | + )).thenReturn(noForcedDecision); |
| 5027 | + |
| 5028 | + // Mock getVariationsForFeatureList to return the error decision |
| 5029 | + when(mockDecisionService.getVariationsForFeatureList( |
| 5030 | + any(List.class), |
| 5031 | + any(OptimizelyUserContext.class), |
| 5032 | + any(ProjectConfig.class), |
| 5033 | + any(List.class) |
| 5034 | + )).thenReturn(Arrays.asList(errorDecisionResponse)); |
| 5035 | + |
| 5036 | + |
| 5037 | + // Use the feature flag from your CMAB config |
| 5038 | + OptimizelyUserContext userContext = optimizely.createUserContext("test_user"); |
| 5039 | + OptimizelyDecision decision = userContext.decide("feature_1"); // This is the feature flag key from cmab-config.json |
| 5040 | + |
| 5041 | + System.out.println("reasons: " + decision.getReasons()); |
| 5042 | + // Verify the decision contains the error information |
| 5043 | + assertFalse(decision.getEnabled()); |
| 5044 | + assertNull(decision.getVariationKey()); |
| 5045 | + assertNull(decision.getRuleKey()); |
| 5046 | + assertEquals("feature_1", decision.getFlagKey()); |
| 5047 | + assertTrue(decision.getReasons().contains("Decision service error occured for key \"feature_1\".")); |
| 5048 | + } |
| 5049 | + |
4996 | 5050 | } |
0 commit comments