Skip to content

Commit 93f3068

Browse files
committed
Merge branch 'release/2.1.0'
2 parents 43b9734 + b9636fd commit 93f3068

File tree

9 files changed

+157
-3
lines changed

9 files changed

+157
-3
lines changed

CHANGELOG

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
Java 2.1.0 (2024-02-14)
2+
-----------------------
3+
Add support for serializing DateTime in self-describing data (#378) (thanks to @stephen-murby for the contribution!)
4+
Add equality functions for SelfDescribing and SelfDescribingJson so that they can be compared in unit tests (#380) (thanks to @stephen-murby for the contribution!)
5+
16
Java 2.0.0 (2024-01-12)
27
-----------------------
38
Add builder methods Subject to allow method chaining (#303)

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ wrapper.gradleVersion = '6.5.0'
2525

2626
group = 'com.snowplowanalytics'
2727
archivesBaseName = 'snowplow-java-tracker'
28-
version = '2.0.0'
28+
version = '2.1.0'
2929
sourceCompatibility = '1.8'
3030
targetCompatibility = '1.8'
3131

@@ -73,6 +73,7 @@ dependencies {
7373

7474
// Jackson JSON processor
7575
api 'com.fasterxml.jackson.core:jackson-databind:2.16.1'
76+
api 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.16.1'
7677

7778
// Testing libraries
7879
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'

src/main/java/com/snowplowanalytics/snowplow/tracker/Utils.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.fasterxml.jackson.core.JsonProcessingException;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
2222

23+
import com.fasterxml.jackson.databind.SerializationFeature;
24+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
2325
import org.slf4j.Logger;
2426
import org.slf4j.LoggerFactory;
2527

@@ -29,7 +31,10 @@
2931
public class Utils {
3032

3133
private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class);
32-
private static final ObjectMapper objectMapper = new ObjectMapper();
34+
private static final ObjectMapper objectMapper
35+
= new ObjectMapper()
36+
.registerModule(new JavaTimeModule())
37+
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
3338

3439
// Tracker Utils
3540

src/main/java/com/snowplowanalytics/snowplow/tracker/events/SelfDescribing.java

+18
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,22 @@ public TrackerPayload getPayload() {
9595
Parameter.SELF_DESCRIBING_ENCODED, Parameter.SELF_DESCRIBING);
9696
return putTrueTimestamp(payload);
9797
}
98+
99+
@Override
100+
public boolean equals(Object o) {
101+
if (this == o) return true;
102+
if (o == null || getClass() != o.getClass()) return false;
103+
104+
SelfDescribing that = (SelfDescribing) o;
105+
106+
if (base64Encode != that.base64Encode) return false;
107+
return Objects.equals(eventData, that.eventData);
108+
}
109+
110+
@Override
111+
public int hashCode() {
112+
int result = eventData != null ? eventData.hashCode() : 0;
113+
result = 31 * result + (base64Encode ? 1 : 0);
114+
return result;
115+
}
98116
}

src/main/java/com/snowplowanalytics/snowplow/tracker/payload/SelfDescribingJson.java

+15
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,19 @@ public long getByteSize() {
198198
public String toString() {
199199
return Utils.mapToJSONString(payload);
200200
}
201+
202+
@Override
203+
public boolean equals(Object o) {
204+
if (this == o) return true;
205+
if (o == null || getClass() != o.getClass()) return false;
206+
207+
SelfDescribingJson that = (SelfDescribingJson) o;
208+
209+
return payload.equals(that.payload);
210+
}
211+
212+
@Override
213+
public int hashCode() {
214+
return payload.hashCode();
215+
}
201216
}

src/test/java/com/snowplowanalytics/snowplow/tracker/TrackerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ public void testCreateWithConfiguration() {
576576
@Test
577577
public void testGetTrackerVersion() {
578578
Tracker tracker = new Tracker(new TrackerConfiguration("namespace", "an-app-id"), mockEmitter);
579-
assertEquals("java-2.0.0", tracker.getTrackerVersion());
579+
assertEquals("java-2.1.0", tracker.getTrackerVersion());
580580
}
581581

582582
@Test

src/test/java/com/snowplowanalytics/snowplow/tracker/UtilsTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
// Java
1919
import java.nio.charset.StandardCharsets;
20+
import java.time.LocalDate;
21+
import java.time.LocalDateTime;
2022
import java.util.LinkedHashMap;
2123
import java.util.Map;
2224

@@ -68,6 +70,14 @@ public void testMapToJSONString() {
6870
Map<String, Object> payload2 = new LinkedHashMap<>();
6971
payload2.put("k1", new Object());
7072
assertEquals("", Utils.mapToJSONString(payload2));
73+
74+
Map<String, Object> payload3 = new LinkedHashMap<>();
75+
payload3.put("k1", LocalDateTime.of(2020, 1, 1, 0, 0));
76+
assertEquals("{\"k1\":\"2020-01-01T00:00:00\"}", Utils.mapToJSONString(payload3));
77+
78+
Map<String, Object> payload4 = new LinkedHashMap<>();
79+
payload4.put("k1", LocalDate.of(2020, 1, 1));
80+
assertEquals("{\"k1\":\"2020-01-01\"}", Utils.mapToJSONString(payload4));
7181
}
7282

7383
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.snowplowanalytics.snowplow.tracker.events;
2+
3+
import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;
4+
5+
// JUnit
6+
import org.junit.Test;
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotEquals;
9+
10+
public class SelfDescribingTest {
11+
12+
@Test
13+
public void testEqualityOfTwoInstances() {
14+
SelfDescribing.Builder<?> builder = SelfDescribing.builder()
15+
.eventData(new SelfDescribingJson("schema-name"));
16+
17+
SelfDescribing a = new SelfDescribing( builder );
18+
SelfDescribing b = new SelfDescribing( builder );
19+
20+
assertEquals(a, b);
21+
}
22+
23+
@Test
24+
public void testNegativeEqualityOfTwoInstances() {
25+
SelfDescribing.Builder<?> builderOne = SelfDescribing.builder()
26+
.eventData(new SelfDescribingJson("schema-name-one"));
27+
28+
SelfDescribing.Builder<?> builderTwo = SelfDescribing.builder()
29+
.eventData(new SelfDescribingJson("schema-name-two"));
30+
31+
SelfDescribing a = new SelfDescribing( builderOne );
32+
SelfDescribing b = new SelfDescribing( builderTwo );
33+
34+
assertNotEquals(a, b);
35+
}
36+
37+
}

src/test/java/com/snowplowanalytics/snowplow/tracker/payload/SelfDescribingJsonTest.java

+63
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.junit.Test;
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertNotEquals;
24+
25+
2326

2427
public class SelfDescribingJsonTest {
2528

@@ -67,4 +70,64 @@ public void testMakeSdjWithSdj() {
6770
assertNotNull(sdj);
6871
assertEquals(expected, sdjString);
6972
}
73+
74+
@Test
75+
public void testEqualityOfTwoInstances_withSchemaNameOnly() {
76+
SelfDescribingJson a = new SelfDescribingJson("schema");
77+
SelfDescribingJson b = new SelfDescribingJson("schema");
78+
assertEquals(a, b);
79+
}
80+
81+
@Test
82+
public void testEqualityOfTwoInstances_withTrackerPayload() {
83+
TrackerPayload nestedData = new TrackerPayload();
84+
nestedData.add("key", "value");
85+
SelfDescribingJson a = new SelfDescribingJson("schema", nestedData);
86+
SelfDescribingJson b = new SelfDescribingJson("schema", nestedData);
87+
assertEquals(a, b);
88+
}
89+
90+
@Test
91+
public void testEqualityOfTwoInstances_withNestedEvent() {
92+
TrackerPayload nestedData = new TrackerPayload();
93+
nestedData.add("key", "value");
94+
SelfDescribingJson nestedEvent = new SelfDescribingJson("nested_event", nestedData);
95+
SelfDescribingJson a = new SelfDescribingJson("schema", nestedEvent);
96+
SelfDescribingJson b = new SelfDescribingJson("schema", nestedEvent);
97+
assertEquals(a, b);
98+
}
99+
100+
@Test
101+
public void testNegativeEqualityOfTwoInstances_withSchemaNameOnly() {
102+
SelfDescribingJson a = new SelfDescribingJson("schema-one");
103+
SelfDescribingJson b = new SelfDescribingJson("schema-two");
104+
assertNotEquals(a, b);
105+
}
106+
107+
@Test
108+
public void testNegativeEqualityOfTwoInstances_withTrackerPayload() {
109+
TrackerPayload nestedDataOne = new TrackerPayload();
110+
nestedDataOne.add("key", "value-one");
111+
TrackerPayload nestedDataTwo = new TrackerPayload();
112+
nestedDataTwo.add("key", "value-two");
113+
SelfDescribingJson a = new SelfDescribingJson("schema", nestedDataOne);
114+
SelfDescribingJson b = new SelfDescribingJson("schema", nestedDataTwo);
115+
assertNotEquals(a, b);
116+
}
117+
118+
@Test
119+
public void testNegativeEqualityOfTwoInstances_withNestedEvent() {
120+
TrackerPayload nestedDataOne = new TrackerPayload();
121+
nestedDataOne.add("key", "value-one");
122+
SelfDescribingJson nestedEventOne = new SelfDescribingJson("nested_event", nestedDataOne);
123+
124+
TrackerPayload nestedDataTwo = new TrackerPayload();
125+
nestedDataTwo.add("key", "value-two");
126+
SelfDescribingJson nestedEventTwo = new SelfDescribingJson("nested_event", nestedDataTwo);
127+
128+
129+
SelfDescribingJson a = new SelfDescribingJson("schema", nestedEventOne);
130+
SelfDescribingJson b = new SelfDescribingJson("schema", nestedEventTwo);
131+
assertNotEquals(a, b);
132+
}
70133
}

0 commit comments

Comments
 (0)