Skip to content

Commit 6584cfc

Browse files
yasirfolio3Michael Ng
authored andcommitted
feat(gherkin): Added new steps and support for testing activate, get_variation API's and feature-variable api listeners. (#150)
1 parent de222a1 commit 6584cfc

File tree

8 files changed

+219
-42
lines changed

8 files changed

+219
-42
lines changed

tests/go.mod

Whitespace-only changes.

tests/integration/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func FeatureContext(s *godog.Suite) {
6464
s.Step(`^the result should match list "([^"]*)"$`, context.TheResultShouldMatchList)
6565
s.Step(`^in the response, "([^"]*)" should be "([^"]*)"$`, context.InTheResponseKeyShouldBeObject)
6666
s.Step(`^in the response, "([^"]*)" should match$`, context.InTheResponseShouldMatch)
67+
s.Step(`^in the response, "([^"]*)" should have this exactly (\d+) times$`, context.ResponseShouldHaveThisExactlyNTimes)
6768
s.Step(`^in the response, "([^"]*)" should have each one of these$`, context.InTheResponseShouldHaveEachOneOfThese)
69+
s.Step(`^the number of dispatched events is (\d+)$`, context.TheNumberOfDispatchedEventsIs)
6870
s.Step(`^there are no dispatched events$`, context.ThereAreNoDispatchedEvents)
6971
s.Step(`^dispatched events payloads include$`, context.DispatchedEventsPayloadsInclude)
7072
}

tests/integration/models/constants.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,30 @@ const EventProcessorDefaultQueueSize = 1
3636

3737
// EventProcessorDefaultFlushInterval - The default value for event processor flush interval
3838
const EventProcessorDefaultFlushInterval = 250 * time.Millisecond
39+
40+
// SDKAPI - represents api's of sdk
41+
type SDKAPI string
42+
43+
const (
44+
// IsFeatureEnabled - the api type is IsFeatureEnabled
45+
IsFeatureEnabled SDKAPI = "is_feature_enabled"
46+
// GetFeatureVariable - the api type is GetFeatureVariable
47+
GetFeatureVariable SDKAPI = "get_feature_variable"
48+
// GetFeatureVariableInteger - the api type is GetFeatureVariableInteger
49+
GetFeatureVariableInteger SDKAPI = "get_feature_variable_integer"
50+
// GetFeatureVariableDouble - the api type is GetFeatureVariableDouble
51+
GetFeatureVariableDouble SDKAPI = "get_feature_variable_double"
52+
// GetFeatureVariableBoolean - the api type is GetFeatureVariableBoolean
53+
GetFeatureVariableBoolean SDKAPI = "get_feature_variable_boolean"
54+
// GetFeatureVariableString - the api type is GetFeatureVariableString
55+
GetFeatureVariableString SDKAPI = "get_feature_variable_string"
56+
// GetEnabledFeatures - the api type is GetEnabledFeatures
57+
GetEnabledFeatures SDKAPI = "get_enabled_features"
58+
// GetVariation - the api type is GetVariation
59+
GetVariation SDKAPI = "get_variation"
60+
// Activate - the api type is Activate
61+
Activate SDKAPI = "activate"
62+
)
63+
64+
// KeyListenerCalled - Key for listener called
65+
const KeyListenerCalled = "listener_called"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 models
18+
19+
// GetVariationRequestParams represents params required for GetVariation API
20+
type GetVariationRequestParams struct {
21+
ExperimentKey string `yaml:"experiment_key"`
22+
UserID string `yaml:"user_id"`
23+
Attributes map[string]interface{} `yaml:"attributes"`
24+
}

tests/integration/optlyplugins/test_composite_service.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,9 @@ func (c *TestCompositeService) decisionNotificationCallback(notification notific
7070

7171
func getDecisionInfoForNotification(notification notification.DecisionNotification) map[string]interface{} {
7272
decisionInfoDict := make(map[string]interface{})
73-
switch notificationType := notification.Type; notificationType {
74-
case "feature":
75-
decisionInfoDict = notification.DecisionInfo["feature"].(map[string]interface{})
76-
source := ""
77-
if decisionSource, ok := decisionInfoDict["source"].(decision.Source); ok {
78-
source = string(decisionSource)
79-
} else {
80-
source = decisionInfoDict["source"].(string)
81-
}
82-
decisionInfoDict["source"] = source
8373

74+
updateSourceInfo := func(source string) {
75+
decisionInfoDict["source_info"] = make(map[string]interface{})
8476
if source == "feature-test" {
8577
if sourceInfo, ok := notification.DecisionInfo["source_info"].(map[string]interface{}); ok {
8678
if experimentKey, ok := sourceInfo["experiment_key"].(string); ok {
@@ -93,7 +85,36 @@ func getDecisionInfoForNotification(notification notification.DecisionNotificati
9385
}
9486
}
9587
}
88+
}
9689

90+
switch notificationType := notification.Type; notificationType {
91+
case "ab-test", "feature-test":
92+
decisionInfoDict["experiment_key"] = notification.DecisionInfo["experimentKey"]
93+
decisionInfoDict["variation_key"] = notification.DecisionInfo["variationKey"]
94+
break
95+
case "feature":
96+
decisionInfoDict = notification.DecisionInfo["feature"].(map[string]interface{})
97+
source := ""
98+
if decisionSource, ok := decisionInfoDict["source"].(decision.Source); ok {
99+
source = string(decisionSource)
100+
} else {
101+
source = decisionInfoDict["source"].(string)
102+
}
103+
decisionInfoDict["source"] = source
104+
updateSourceInfo(source)
105+
case "feature-variable":
106+
decisionInfoDict = notification.DecisionInfo["feature"].(map[string]interface{})
107+
source := ""
108+
if decisionSource, ok := decisionInfoDict["source"].(decision.Source); ok {
109+
source = string(decisionSource)
110+
} else {
111+
source = decisionInfoDict["source"].(string)
112+
}
113+
decisionInfoDict["source"] = source
114+
decisionInfoDict["variable_key"] = notification.DecisionInfo["variable_key"]
115+
decisionInfoDict["variable_type"] = notification.DecisionInfo["variable_type"]
116+
decisionInfoDict["variable_value"] = notification.DecisionInfo["variable_value"]
117+
updateSourceInfo(source)
97118
default:
98119
}
99120
return decisionInfoDict

tests/integration/support/client_wrapper.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"os"
2323
"path"
2424
"path/filepath"
25-
"strings"
2625

2726
"github.com/optimizely/go-sdk/pkg/client"
2827
"github.com/optimizely/go-sdk/pkg/config"
@@ -89,28 +88,34 @@ func (c *ClientWrapper) InvokeAPI(request models.APIOptions) (models.APIResponse
8988
var response models.APIResponse
9089
var err error
9190

92-
switch request.APIName {
93-
case "is_feature_enabled":
91+
switch models.SDKAPI(request.APIName) {
92+
case models.IsFeatureEnabled:
9493
response, err = c.isFeatureEnabled(request)
9594
break
96-
case "get_feature_variable":
95+
case models.GetFeatureVariable:
9796
response, err = c.getFeatureVariable(request)
9897
break
99-
case "get_feature_variable_integer":
98+
case models.GetFeatureVariableInteger:
10099
response, err = c.getFeatureVariableInteger(request)
101100
break
102-
case "get_feature_variable_double":
101+
case models.GetFeatureVariableDouble:
103102
response, err = c.getFeatureVariableDouble(request)
104103
break
105-
case "get_feature_variable_boolean":
104+
case models.GetFeatureVariableBoolean:
106105
response, err = c.getFeatureVariableBoolean(request)
107106
break
108-
case "get_feature_variable_string":
107+
case models.GetFeatureVariableString:
109108
response, err = c.getFeatureVariableString(request)
110109
break
111-
case "get_enabled_features":
110+
case models.GetEnabledFeatures:
112111
response, err = c.getEnabledFeatures(request)
113112
break
113+
case models.GetVariation:
114+
response, err = c.getVariation(request)
115+
break
116+
case models.Activate:
117+
response, err = c.activate(request)
118+
break
114119
default:
115120
break
116121
}
@@ -230,15 +235,49 @@ func (c *ClientWrapper) getEnabledFeatures(request models.APIOptions) (models.AP
230235
var response models.APIResponse
231236
err := yaml.Unmarshal([]byte(request.Arguments), &params)
232237
if err == nil {
233-
enabledFeatures := ""
238+
var enabledFeatures []string
234239
user := entities.UserContext{
235240
ID: params.UserID,
236241
Attributes: params.Attributes,
237242
}
238243
if values, err := c.Client.GetEnabledFeatures(user); err == nil {
239-
enabledFeatures = strings.Join(values, ",")
244+
enabledFeatures = values
240245
}
241246
response.Result = enabledFeatures
242247
}
243248
return response, err
244249
}
250+
251+
func (c *ClientWrapper) getVariation(request models.APIOptions) (models.APIResponse, error) {
252+
var params models.GetVariationRequestParams
253+
var response models.APIResponse
254+
err := yaml.Unmarshal([]byte(request.Arguments), &params)
255+
if err == nil {
256+
user := entities.UserContext{
257+
ID: params.UserID,
258+
Attributes: params.Attributes,
259+
}
260+
response.Result, _ = c.Client.GetVariation(params.ExperimentKey, user)
261+
if response.Result == "" {
262+
response.Result = "NULL"
263+
}
264+
}
265+
return response, err
266+
}
267+
268+
func (c *ClientWrapper) activate(request models.APIOptions) (models.APIResponse, error) {
269+
var params models.GetVariationRequestParams
270+
var response models.APIResponse
271+
err := yaml.Unmarshal([]byte(request.Arguments), &params)
272+
if err == nil {
273+
user := entities.UserContext{
274+
ID: params.UserID,
275+
Attributes: params.Attributes,
276+
}
277+
response.Result, _ = c.Client.Activate(params.ExperimentKey, user)
278+
if response.Result == "" {
279+
response.Result = "NULL"
280+
}
281+
}
282+
return response, err
283+
}

0 commit comments

Comments
 (0)