Repository navigation
Expand file tree
/
Copy pathSteps.java
More file actions
104 lines (92 loc) · 3.6 KB
/
Copy pathSteps.java
File metadata and controls
104 lines (92 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import com.flagsmith.FlagsmithClient;
import com.flagsmith.config.FlagsmithConfig;
import com.flagsmith.exceptions.FlagsmithClientError;
import com.flagsmith.models.BaseFlag;
import com.flagsmith.models.Flags;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Steps for the Flagsmith SDK behavioural spec: https://github.com/Flagsmith/sdk-spec
*
* <p>Each method implements the Gherkin step in the comment above it.
*/
public class Steps {
/** A flag in the shape the spec expects. */
public static class SpecFlag {
public final String feature;
public final boolean enabled;
public final Object value;
SpecFlag(BaseFlag flag) {
feature = flag.getFeatureName();
enabled = flag.getEnabled();
value = flag.getValue();
}
}
static List<SpecFlag> asSpecFlags(Flags flags) {
return flags.getAllFlags().stream().map(SpecFlag::new).collect(Collectors.toList());
}
// Create the SDK client
static FlagsmithClient createClient(String environmentKey, String apiUrl) {
return FlagsmithClient.newBuilder().setApiKey(environmentKey).withApiUrl(apiUrl).build();
}
// Create the SDK client with events enabled
static FlagsmithClient createClientWithEvents(
String environmentKey, String apiUrl, String eventsUrl) {
return FlagsmithClient.newBuilder()
.setApiKey(environmentKey)
.withConfiguration(FlagsmithConfig.newBuilder()
.baseUri(apiUrl)
.withEnableEvents(true)
.eventsUri(eventsUrl)
.build())
.build();
}
// Create the SDK client with local evaluation
static FlagsmithClient createClientWithLocalEvaluation(String environmentKey, String apiUrl) {
return FlagsmithClient.newBuilder()
.setApiKey(environmentKey)
.withConfiguration(FlagsmithConfig.newBuilder()
.baseUri(apiUrl)
.withLocalEvaluation(true)
.build())
.build();
}
// When the application gets the environment flags
static List<SpecFlag> getEnvironmentFlags(FlagsmithClient flagsmith)
throws FlagsmithClientError {
return asSpecFlags(flagsmith.getEnvironmentFlags());
}
// When the application gets the flags for identity "{identifier}" with traits:
static List<SpecFlag> getIdentityFlags(
FlagsmithClient flagsmith, String identifier, Map<String, Object> traits)
throws FlagsmithClientError {
return asSpecFlags(flagsmith.getIdentityFlags(identifier, traits));
}
// When the application gets the experiment flag "{feature}" for identity "{identifier}" with traits:
static List<SpecFlag> getExperimentFlag(
FlagsmithClient flagsmith, String feature, String identifier, Map<String, Object> traits)
throws FlagsmithClientError {
return Collections.singletonList(
new SpecFlag(flagsmith.getExperimentFlag(feature, identifier, traits)));
}
// When the application tracks event "{event}" for identity "{identifier}" with value {value} and metadata:
static void trackEvent(
FlagsmithClient flagsmith,
String event,
String identifier,
Object value,
Map<String, Object> metadata) {
flagsmith.trackEvent(event, identifier, value, null, metadata);
}
// When the application tracks an exposure of feature "{feature}" for identity "{identifier}" with value {value} and metadata:
static void trackExposureEvent(
FlagsmithClient flagsmith,
String feature,
String identifier,
Object value,
Map<String, Object> metadata) {
flagsmith.trackExposureEvent(feature, identifier, value, null, metadata);
}
}