|
| 1 | +package com.octopus.openfeature.provider; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonParser; |
| 4 | +import com.fasterxml.jackson.core.StreamReadFeature; |
| 5 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 6 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 7 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 8 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 10 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 11 | +import dev.openfeature.sdk.*; |
| 12 | +import org.junit.jupiter.api.AfterAll; |
| 13 | +import org.junit.jupiter.api.AfterEach; |
| 14 | +import org.junit.jupiter.api.BeforeAll; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.Arguments; |
| 17 | +import org.junit.jupiter.params.provider.MethodSource; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.UncheckedIOException; |
| 21 | +import java.net.URI; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import java.util.stream.Stream; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +class SpecificationTests { |
| 32 | + |
| 33 | + private static Server server; |
| 34 | + |
| 35 | + @BeforeAll |
| 36 | + static void startServer() { |
| 37 | + server = new Server(); |
| 38 | + } |
| 39 | + |
| 40 | + @AfterAll |
| 41 | + static void stopServer() { |
| 42 | + server.stop(); |
| 43 | + } |
| 44 | + |
| 45 | + @AfterEach |
| 46 | + void shutdownApi() { |
| 47 | + OpenFeatureAPI.getInstance().shutdown(); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest(name = "[{0}] {1}") |
| 51 | + @MethodSource("fixtureTestCases") |
| 52 | + void evaluate(String fileName, String description, String responseJson, FixtureCase testCase) { |
| 53 | + String token = server.configure(responseJson); |
| 54 | + OctopusConfiguration config = new OctopusConfiguration(token); |
| 55 | + config.setServerUri(URI.create(server.baseUrl())); |
| 56 | + |
| 57 | + OpenFeatureAPI api = OpenFeatureAPI.getInstance(); |
| 58 | + api.setProviderAndWait(new OctopusProvider(config)); |
| 59 | + Client client = api.getClient(); |
| 60 | + |
| 61 | + EvaluationContext ctx = buildContext(testCase.configuration.context); |
| 62 | + FlagEvaluationDetails<Boolean> result = client.getBooleanDetails( |
| 63 | + testCase.configuration.slug, |
| 64 | + testCase.configuration.defaultValue, |
| 65 | + ctx |
| 66 | + ); |
| 67 | + |
| 68 | + assertThat(result.getValue()) |
| 69 | + .as("[%s] %s → value", fileName, description) |
| 70 | + .isEqualTo(testCase.expected.value); |
| 71 | + assertThat(result.getErrorCode()) |
| 72 | + .as("[%s] %s → errorCode", fileName, description) |
| 73 | + .isEqualTo(mapErrorCode(testCase.expected.errorCode)); |
| 74 | + } |
| 75 | + |
| 76 | + static Stream<Arguments> fixtureTestCases() throws IOException { |
| 77 | + ObjectMapper mapper = JsonMapper.builder() |
| 78 | + .enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION) |
| 79 | + .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
| 80 | + .build(); |
| 81 | + |
| 82 | + List<Path> jsonFiles; |
| 83 | + try (Stream<Path> files = Files.list(Path.of("specification", "Fixtures"))) { |
| 84 | + jsonFiles = files |
| 85 | + .filter(p -> p.getFileName().toString().endsWith(".json")) |
| 86 | + .collect(Collectors.toList()); |
| 87 | + } |
| 88 | + if (jsonFiles.isEmpty()) { |
| 89 | + throw new IllegalStateException( |
| 90 | + "No fixture files found under 'specification/Fixtures/'. " + |
| 91 | + "Ensure the git submodule is initialised: git submodule update --init"); |
| 92 | + } |
| 93 | + return jsonFiles.stream().flatMap(path -> { |
| 94 | + try { |
| 95 | + String fileContent = Files.readString(path); |
| 96 | + Fixture fixture = mapper.readValue(fileContent, Fixture.class); |
| 97 | + String fileName = path.getFileName().toString(); |
| 98 | + return Stream.of(fixture.cases) |
| 99 | + .map(c -> Arguments.of(fileName, c.description, fixture.response, c)); |
| 100 | + } catch (IOException e) { |
| 101 | + throw new UncheckedIOException(e); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + private static EvaluationContext buildContext(Map<String, String> context) { |
| 107 | + MutableContext ctx = new MutableContext(); |
| 108 | + if (context != null) { |
| 109 | + context.forEach(ctx::add); |
| 110 | + } |
| 111 | + return ctx; |
| 112 | + } |
| 113 | + |
| 114 | + private static ErrorCode mapErrorCode(String code) { |
| 115 | + if (code == null) return null; |
| 116 | + switch (code) { |
| 117 | + case "FLAG_NOT_FOUND": |
| 118 | + return ErrorCode.FLAG_NOT_FOUND; |
| 119 | + case "PARSE_ERROR": |
| 120 | + return ErrorCode.PARSE_ERROR; |
| 121 | + case "TYPE_MISMATCH": |
| 122 | + return ErrorCode.TYPE_MISMATCH; |
| 123 | + case "TARGETING_KEY_MISSING": |
| 124 | + return ErrorCode.TARGETING_KEY_MISSING; |
| 125 | + case "PROVIDER_NOT_READY": |
| 126 | + return ErrorCode.PROVIDER_NOT_READY; |
| 127 | + case "INVALID_CONTEXT": |
| 128 | + return ErrorCode.INVALID_CONTEXT; |
| 129 | + case "PROVIDER_FATAL": |
| 130 | + return ErrorCode.PROVIDER_FATAL; |
| 131 | + case "GENERAL": |
| 132 | + return ErrorCode.GENERAL; |
| 133 | + default: |
| 134 | + throw new IllegalArgumentException("Unknown error code in fixture: " + code); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + static class Fixture { |
| 139 | + @JsonDeserialize(using = RawJsonDeserializer.class) |
| 140 | + public String response; |
| 141 | + public FixtureCase[] cases; |
| 142 | + } |
| 143 | + |
| 144 | + static class FixtureCase { |
| 145 | + public String description; |
| 146 | + public FixtureConfiguration configuration; |
| 147 | + public FixtureExpected expected; |
| 148 | + } |
| 149 | + |
| 150 | + static class FixtureConfiguration { |
| 151 | + public String slug; |
| 152 | + public boolean defaultValue; |
| 153 | + public Map<String, String> context; |
| 154 | + } |
| 155 | + |
| 156 | + static class FixtureExpected { |
| 157 | + public boolean value; |
| 158 | + public String errorCode; |
| 159 | + } |
| 160 | + |
| 161 | + static class RawJsonDeserializer extends JsonDeserializer<String> { |
| 162 | + @Override |
| 163 | + public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException { |
| 164 | + long begin = jp.currentLocation().getCharOffset(); |
| 165 | + jp.skipChildren(); |
| 166 | + long end = jp.currentLocation().getCharOffset(); |
| 167 | + String json = jp.currentLocation().contentReference().getRawContent().toString(); |
| 168 | + return json.substring((int) begin - 1, (int) end); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments