Skip to content

Commit 1131417

Browse files
author
Bernd Warmuth
committed
fixup! feat: implemented support for tracking
Signed-off-by: Bernd Warmuth <[email protected]>
1 parent ae1e7c5 commit 1131417

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dev.openfeature.sdk;
2+
3+
import dev.openfeature.sdk.internal.ExcludeFromGeneratedCoverageReport;
4+
import lombok.Getter;
5+
import lombok.experimental.Delegate;
6+
7+
import java.util.Map;
8+
import java.util.function.Function;
9+
10+
11+
/**
12+
* ImmutableTrackingEventDetails represents data pertinent to a particular tracking event.
13+
*/
14+
public class ImmutableTrackingEventDetails implements TrackingEventDetails {
15+
16+
@Delegate(excludes = DelegateExclusions.class)
17+
private final ImmutableStructure structure;
18+
19+
@Getter
20+
private final Number value;
21+
22+
public ImmutableTrackingEventDetails() {
23+
this.value = 0f;
24+
this.structure = new ImmutableStructure();
25+
}
26+
27+
public ImmutableTrackingEventDetails(final Number value) {
28+
this.value = value;
29+
this.structure = new ImmutableStructure();
30+
}
31+
32+
public ImmutableTrackingEventDetails(final Number value, final Map<String, Value> attributes) {
33+
this.value = value;
34+
this.structure = new ImmutableStructure(attributes);
35+
}
36+
37+
38+
@SuppressWarnings("all")
39+
private static class DelegateExclusions {
40+
@ExcludeFromGeneratedCoverageReport
41+
public <T extends Structure> Map<String, Value> merge(Function<Map<String, Value>, Structure> newStructure,
42+
Map<String, Value> base,
43+
Map<String, Value> overriding) {
44+
return null;
45+
}
46+
}
47+
}

src/main/java/dev/openfeature/sdk/MutableTrackingEventDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public MutableTrackingEventDetails() {
2626
this.structure = new MutableStructure();
2727
}
2828

29-
public MutableTrackingEventDetails(final float value) {
29+
public MutableTrackingEventDetails(final Number value) {
3030
this.value = value;
3131
this.structure = new MutableStructure();
3232
}

src/test/java/dev/openfeature/sdk/TrackingSpecTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.openfeature.sdk;
22

3+
import com.google.common.collect.ImmutableMap;
34
import com.google.common.collect.Maps;
45
import dev.openfeature.sdk.fixtures.ProviderFixture;
56
import dev.openfeature.sdk.testutils.FeatureProviderTestUtils;
@@ -137,21 +138,50 @@ void noopProvider() {
137138
@Test
138139
void eventDetails() {
139140
assertEquals(0f, new MutableTrackingEventDetails().getValue());
141+
assertEquals(0f, new ImmutableTrackingEventDetails().getValue());
140142
assertEquals(9.87f, new MutableTrackingEventDetails(9.87f).getValue());
143+
assertEquals(9.87f, new ImmutableTrackingEventDetails(9.87f).getValue());
144+
145+
146+
// using mutable tracking event details
141147
Map<String, Value> expectedMap = Maps.newHashMap();
142148
expectedMap.put("my-str", new Value("str"));
143149
expectedMap.put("my-num", new Value(1));
144150
expectedMap.put("my-bool", new Value(true));
145151
expectedMap.put("my-struct", new Value(new MutableTrackingEventDetails()));
146152

147-
148153
MutableStructure details = new MutableTrackingEventDetails()
149154
.add("my-str", new Value("str"))
150155
.add("my-num", new Value(1))
151156
.add("my-bool", new Value(true))
152157
.add("my-struct", new Value(new MutableTrackingEventDetails()));
153158

154159
assertEquals(expectedMap, details.asMap());
160+
assertThatCode(() -> OpenFeatureAPI.getInstance().getClient().
161+
track(
162+
"tracking-event-name",
163+
new ImmutableContext(),
164+
new MutableTrackingEventDetails()))
165+
.doesNotThrowAnyException();
166+
167+
168+
// using immutable tracking event details
169+
ImmutableMap<String, Value> expectedImmutable = ImmutableMap.of("my-str", new Value("str"),
170+
"my-num", new Value(1),
171+
"my-bool", new Value(true),
172+
"my-struct", new Value(new ImmutableStructure())
173+
);
174+
175+
ImmutableTrackingEventDetails immutableDetails = new ImmutableTrackingEventDetails(2, expectedMap);
176+
assertEquals(expectedImmutable, immutableDetails.asMap());
177+
assertThatCode(() -> OpenFeatureAPI.getInstance().getClient().
178+
track(
179+
"tracking-event-name",
180+
new ImmutableContext(),
181+
new ImmutableTrackingEventDetails()))
182+
.doesNotThrowAnyException();
183+
184+
155185
}
156186

157187
}

0 commit comments

Comments
 (0)