|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2019, Optimizely, Inc. and contributors * |
| 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 | + * http://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 decision |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/optimizely/go-sdk/optimizely/decision/reasons" |
| 21 | + "github.com/optimizely/go-sdk/optimizely/entities" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +func TestCompositeFeatureServiceGetDecisionFeatureExperiment(t *testing.T) { |
| 27 | + mockProjectConfig := new(mockProjectConfig) |
| 28 | + testFeatureDecisionContext := FeatureDecisionContext{ |
| 29 | + Feature: &testFeat3335, |
| 30 | + ProjectConfig: mockProjectConfig, |
| 31 | + } |
| 32 | + testExperimentDecisionContext := ExperimentDecisionContext{ |
| 33 | + Experiment: &testExp1113, |
| 34 | + ProjectConfig: mockProjectConfig, |
| 35 | + } |
| 36 | + mockExperimentDecision := ExperimentDecision{ |
| 37 | + Decision: Decision{reasons.BucketedIntoVariation}, |
| 38 | + Variation: &testExp1113Var2223, |
| 39 | + } |
| 40 | + |
| 41 | + testUserContext := entities.UserContext{ |
| 42 | + ID: "test_user_1", |
| 43 | + } |
| 44 | + |
| 45 | + mockExperimentService := new(MockExperimentDecisionService) |
| 46 | + mockRolloutService := new(MockFeatureDecisionService) |
| 47 | + // Mock to return decision from feature experiment service |
| 48 | + mockExperimentService.On("GetDecision", testExperimentDecisionContext, testUserContext).Return(mockExperimentDecision, nil) |
| 49 | + |
| 50 | + // Decision is returned from feature test evaluation |
| 51 | + expectedDecision := FeatureDecision{ |
| 52 | + Decision: Decision{reasons.BucketedIntoVariation}, |
| 53 | + Source: FeatureTest, |
| 54 | + Experiment: testExp1113, |
| 55 | + Variation: &testExp1113Var2223, |
| 56 | + } |
| 57 | + |
| 58 | + compositeFeatureService := &CompositeFeatureService{ |
| 59 | + featureExperimentService: mockExperimentService, |
| 60 | + rolloutDecisionService: mockRolloutService, |
| 61 | + } |
| 62 | + actualDecision, err := compositeFeatureService.GetDecision(testFeatureDecisionContext, testUserContext) |
| 63 | + assert.NoError(t, err) |
| 64 | + assert.Equal(t, expectedDecision, actualDecision) |
| 65 | +} |
| 66 | + |
| 67 | +func TestCompositeFeatureServiceGetDecisionRollout(t *testing.T) { |
| 68 | + mockProjectConfig := new(mockProjectConfig) |
| 69 | + testFeatureDecisionContext := FeatureDecisionContext{ |
| 70 | + Feature: &testFeat3335, |
| 71 | + ProjectConfig: mockProjectConfig, |
| 72 | + } |
| 73 | + testExperimentDecisionContext1 := ExperimentDecisionContext{ |
| 74 | + Experiment: &testExp1113, |
| 75 | + ProjectConfig: mockProjectConfig, |
| 76 | + } |
| 77 | + testExperimentDecisionContext2 := ExperimentDecisionContext{ |
| 78 | + Experiment: &testExp1114, |
| 79 | + ProjectConfig: mockProjectConfig, |
| 80 | + } |
| 81 | + |
| 82 | + // Mock to not bucket user in feature experiment |
| 83 | + mockExperimentDecision := ExperimentDecision{ |
| 84 | + Decision: Decision{reasons.NotBucketedIntoVariation}, |
| 85 | + Variation: nil, |
| 86 | + } |
| 87 | + |
| 88 | + testUserContext := entities.UserContext{ |
| 89 | + ID: "test_user_1", |
| 90 | + } |
| 91 | + |
| 92 | + mockFeatureDecision := FeatureDecision{ |
| 93 | + Decision: Decision{reasons.BucketedIntoVariation}, |
| 94 | + Source: Rollout, |
| 95 | + Experiment: testExp1115, |
| 96 | + Variation: &testExp1115Var2227, |
| 97 | + } |
| 98 | + |
| 99 | + mockExperimentService := new(MockExperimentDecisionService) |
| 100 | + mockRolloutService := new(MockFeatureDecisionService) |
| 101 | + // Mock to return decision from feature experiment service which causes rollout service to be called |
| 102 | + mockExperimentService.On("GetDecision", testExperimentDecisionContext1, testUserContext).Return(mockExperimentDecision, nil) |
| 103 | + mockExperimentService.On("GetDecision", testExperimentDecisionContext2, testUserContext).Return(mockExperimentDecision, nil) |
| 104 | + mockRolloutService.On("GetDecision", testFeatureDecisionContext, testUserContext).Return(mockFeatureDecision, nil) |
| 105 | + |
| 106 | + // Decision is returned from rollout evaluation |
| 107 | + expectedDecision := mockFeatureDecision |
| 108 | + |
| 109 | + compositeFeatureService := &CompositeFeatureService{ |
| 110 | + featureExperimentService: mockExperimentService, |
| 111 | + rolloutDecisionService: mockRolloutService, |
| 112 | + } |
| 113 | + actualDecision, err := compositeFeatureService.GetDecision(testFeatureDecisionContext, testUserContext) |
| 114 | + assert.NoError(t, err) |
| 115 | + assert.Equal(t, expectedDecision, actualDecision) |
| 116 | +} |
0 commit comments