|
| 1 | +import com.flagsmith.FlagsmithClient; |
| 2 | +import com.flagsmith.exceptions.FlagsmithClientError; |
| 3 | +import com.flagsmith.models.BaseFlag; |
| 4 | +import com.flagsmith.models.Flags; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.stream.Collectors; |
| 8 | + |
| 9 | +/** |
| 10 | + * Steps for the Flagsmith SDK behavioural spec: https://github.com/Flagsmith/sdk-spec |
| 11 | + * |
| 12 | + * <p>Each method implements the Gherkin step in the comment above it. |
| 13 | + */ |
| 14 | +public class Steps { |
| 15 | + |
| 16 | + /** A flag in the shape the spec expects. */ |
| 17 | + public static class SpecFlag { |
| 18 | + public final String feature; |
| 19 | + public final boolean enabled; |
| 20 | + public final Object value; |
| 21 | + |
| 22 | + SpecFlag(BaseFlag flag) { |
| 23 | + feature = flag.getFeatureName(); |
| 24 | + enabled = flag.getEnabled(); |
| 25 | + value = flag.getValue(); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + static List<SpecFlag> asSpecFlags(Flags flags) { |
| 30 | + return flags.getAllFlags().stream().map(SpecFlag::new).collect(Collectors.toList()); |
| 31 | + } |
| 32 | + |
| 33 | + // Create the SDK client |
| 34 | + static FlagsmithClient createClient(String environmentKey, String apiUrl) { |
| 35 | + return FlagsmithClient.newBuilder().setApiKey(environmentKey).withApiUrl(apiUrl).build(); |
| 36 | + } |
| 37 | + |
| 38 | + // When the application gets the environment flags |
| 39 | + static List<SpecFlag> getEnvironmentFlags(FlagsmithClient flagsmith) |
| 40 | + throws FlagsmithClientError { |
| 41 | + return asSpecFlags(flagsmith.getEnvironmentFlags()); |
| 42 | + } |
| 43 | + |
| 44 | + // When the application gets the flags for identity "{identifier}" with traits: |
| 45 | + static List<SpecFlag> getIdentityFlags( |
| 46 | + FlagsmithClient flagsmith, String identifier, Map<String, Object> traits) |
| 47 | + throws FlagsmithClientError { |
| 48 | + return asSpecFlags(flagsmith.getIdentityFlags(identifier, traits)); |
| 49 | + } |
| 50 | +} |
0 commit comments