-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpecificationTests.java
More file actions
172 lines (150 loc) · 6.11 KB
/
SpecificationTests.java
File metadata and controls
172 lines (150 loc) · 6.11 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.octopus.openfeature.provider;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.StreamReadFeature;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.json.JsonMapper;
import dev.openfeature.sdk.*;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
class SpecificationTests {
private static Server server;
@BeforeAll
static void startServer() {
server = new Server();
}
@AfterAll
static void stopServer() {
server.stop();
}
@AfterEach
void shutdownApi() {
OpenFeatureAPI.getInstance().shutdown();
}
@ParameterizedTest(name = "[{0}] {1}")
@MethodSource("fixtureTestCases")
void evaluate(String fileName, String description, String responseJson, FixtureCase testCase) {
String token = server.configure(responseJson);
OctopusConfiguration config = new OctopusConfiguration(token);
config.setServerUri(URI.create(server.baseUrl()));
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(new OctopusProvider(config));
Client client = api.getClient();
EvaluationContext ctx = buildContext(testCase.configuration.context);
FlagEvaluationDetails<Boolean> result = client.getBooleanDetails(
testCase.configuration.slug,
testCase.configuration.defaultValue,
ctx
);
assertThat(result.getValue())
.as("[%s] %s → value", fileName, description)
.isEqualTo(testCase.expected.value);
assertThat(result.getErrorCode())
.as("[%s] %s → errorCode", fileName, description)
.isEqualTo(mapErrorCode(testCase.expected.errorCode));
}
static Stream<Arguments> fixtureTestCases() throws IOException {
ObjectMapper mapper = JsonMapper.builder()
.enable(StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.build();
List<Path> jsonFiles;
try (Stream<Path> files = Files.list(Path.of("specification", "Fixtures"))) {
jsonFiles = files
.filter(p -> p.getFileName().toString().endsWith(".json"))
.collect(Collectors.toList());
}
if (jsonFiles.isEmpty()) {
throw new IllegalStateException(
"No fixture files found under 'specification/Fixtures/'. " +
"Ensure the git submodule is initialised: git submodule update --init");
}
return jsonFiles.stream().flatMap(path -> {
try {
String fileContent = Files.readString(path);
Fixture fixture = mapper.readValue(fileContent, Fixture.class);
String fileName = path.getFileName().toString();
return Stream.of(fixture.cases)
.map(c -> Arguments.of(fileName, c.description, fixture.response, c));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
private static EvaluationContext buildContext(Map<String, String> context) {
MutableContext ctx = new MutableContext();
if (context != null) {
context.forEach(ctx::add);
}
return ctx;
}
private static ErrorCode mapErrorCode(String code) {
if (code == null) return null;
switch (code) {
case "FLAG_NOT_FOUND":
return ErrorCode.FLAG_NOT_FOUND;
case "PARSE_ERROR":
return ErrorCode.PARSE_ERROR;
case "TYPE_MISMATCH":
return ErrorCode.TYPE_MISMATCH;
case "TARGETING_KEY_MISSING":
return ErrorCode.TARGETING_KEY_MISSING;
case "PROVIDER_NOT_READY":
return ErrorCode.PROVIDER_NOT_READY;
case "INVALID_CONTEXT":
return ErrorCode.INVALID_CONTEXT;
case "PROVIDER_FATAL":
return ErrorCode.PROVIDER_FATAL;
case "GENERAL":
return ErrorCode.GENERAL;
default:
throw new IllegalArgumentException("Unknown error code in fixture: " + code);
}
}
static class Fixture {
@JsonDeserialize(using = RawJsonDeserializer.class)
public String response;
public FixtureCase[] cases;
}
static class FixtureCase {
public String description;
public FixtureConfiguration configuration;
public FixtureExpected expected;
}
static class FixtureConfiguration {
public String slug;
public boolean defaultValue;
public Map<String, String> context;
}
static class FixtureExpected {
public boolean value;
public String errorCode;
}
static class RawJsonDeserializer extends JsonDeserializer<String> {
@Override
public String deserialize(JsonParser jp, DeserializationContext dc) throws IOException {
long begin = jp.currentLocation().getCharOffset();
jp.skipChildren();
long end = jp.currentLocation().getCharOffset();
String json = jp.currentLocation().contentReference().getRawContent().toString();
return json.substring((int) begin - 1, (int) end);
}
}
}