-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureToggleEvaluationDeserializationTests.java
More file actions
134 lines (109 loc) · 5.51 KB
/
FeatureToggleEvaluationDeserializationTests.java
File metadata and controls
134 lines (109 loc) · 5.51 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
package com.octopus.openfeature.provider;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import org.junit.jupiter.api.Test;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class FeatureToggleEvaluationDeserializationTests {
private final ObjectMapper objectMapper = OctopusObjectMapper.INSTANCE;
private InputStream resource(String name) {
return getClass().getResourceAsStream(name);
}
private void assertSegmentsContain(List<Segment> segments, Segment... expected) {
assertThat(segments).usingRecursiveFieldByFieldElementComparator().contains(expected);
}
@Test
void shouldDeserializeEnabledToggle() throws Exception {
FeatureToggleEvaluation result = objectMapper.readValue(resource("toggle-enabled-no-segments.json"), FeatureToggleEvaluation.class);
assertThat(result.getSlug()).isEqualTo("my-feature");
assertThat(result.isEnabled()).isTrue();
assertThat(result.getEvaluationKey()).hasValue("eval-key-1");
assertThat(result.getSegments()).isPresent();
assertThat(result.getSegments().orElseThrow()).isEmpty();
assertThat(result.getClientRolloutPercentage()).hasValue(100);
}
@Test
void shouldDeserializeDisabledToggle() throws Exception {
FeatureToggleEvaluation result = objectMapper.readValue(resource("toggle-disabled.json"), FeatureToggleEvaluation.class);
assertThat(result.isEnabled()).isFalse();
assertThat(result.getEvaluationKey()).isEmpty();
assertThat(result.getSegments()).isEmpty();
assertThat(result.getClientRolloutPercentage()).isEmpty();
}
@Test
void shouldDeserializeToggleWithMissingSegmentsField() throws Exception {
FeatureToggleEvaluation result = objectMapper.readValue(resource("toggle-missing-segments.json"), FeatureToggleEvaluation.class);
assertThat(result.getSegments()).isEmpty();
}
@Test
void shouldDeserializeToggleWithSegments() throws Exception {
FeatureToggleEvaluation result = objectMapper.readValue(
resource("toggle-with-segments.json"), FeatureToggleEvaluation.class);
var segments = result.getSegments().orElseThrow();
assertThat(segments).hasSize(2);
assertSegmentsContain(
segments,
new Segment("license-type", "free"),
new Segment("country", "au")
);
}
@Test
void shouldDeserializeListOfToggles() throws Exception {
var result = objectMapper.readValue(
resource("toggle-list.json"),
new TypeReference<List<FeatureToggleEvaluation>>() {}
);
assertThat(result).hasSize(2);
assertThat(result.get(0).getSlug()).isEqualTo("feature-a");
assertThat(result.get(0).isEnabled()).isTrue();
assertThat(result.get(1).getSlug()).isEqualTo("feature-b");
assertThat(result.get(1).isEnabled()).isFalse();
}
@Test
void shouldDeserializeListOfTogglesWithVariousFieldCasings() throws Exception {
var result = objectMapper.readValue(
resource("toggles-with-different-field-capitalisation.json"),
new TypeReference<List<FeatureToggleEvaluation>>() {}
);
assertThat(result).hasSize(3);
assertThat(result.get(0).getSlug()).isEqualTo("feature-a");
assertThat(result.get(0).isEnabled()).isTrue();
assertSegmentsContain(result.get(0).getSegments().orElseThrow(), new Segment("license-type", "free"));
assertThat(result.get(1).getSlug()).isEqualTo("feature-b");
assertThat(result.get(1).isEnabled()).isTrue();
assertSegmentsContain(result.get(1).getSegments().orElseThrow(), new Segment("plan", "enterprise"));
assertThat(result.get(2).getSlug()).isEqualTo("feature-c");
assertThat(result.get(2).isEnabled()).isTrue();
assertSegmentsContain(result.get(2).getSegments().orElseThrow(), new Segment("country", "au"));
}
@Test
void shouldFailDeserializationWhenSegmentKeyIsMissing() {
assertThatThrownBy(() -> objectMapper.readValue(
resource("toggle-segment-missing-key.json"), FeatureToggleEvaluation.class))
.isInstanceOf(MismatchedInputException.class);
}
@Test
void shouldFailDeserializationWhenSegmentValueIsMissing() {
assertThatThrownBy(() -> objectMapper.readValue(
resource("toggle-segment-missing-value.json"), FeatureToggleEvaluation.class))
.isInstanceOf(MismatchedInputException.class);
}
@Test
void shouldFailDeserializationWhenSegmentKeyAndValueAreMissing() {
assertThatThrownBy(() -> objectMapper.readValue(
resource("toggle-segment-missing-key-and-value.json"), FeatureToggleEvaluation.class))
.isInstanceOf(MismatchedInputException.class);
}
@Test
void shouldIgnoreExtraneousProperties() throws Exception {
FeatureToggleEvaluation result = objectMapper.readValue(
resource("toggle-with-extraneous-properties.json"), FeatureToggleEvaluation.class);
assertThat(result.getSlug()).isEqualTo("my-feature");
assertThat(result.isEnabled()).isTrue();
assertSegmentsContain(result.getSegments().orElseThrow(), new Segment("license-type", "free"));
}
}