Skip to content

Commit

Permalink
Add equality functions for SelfDescribing and SelfDescribingJson so t…
Browse files Browse the repository at this point in the history
…hat they can be compared in unit tests (close  #380)

PR #381
  • Loading branch information
stephen-murby authored Feb 14, 2024
1 parent 26655a9 commit edc2d23
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,22 @@ public TrackerPayload getPayload() {
Parameter.SELF_DESCRIBING_ENCODED, Parameter.SELF_DESCRIBING);
return putTrueTimestamp(payload);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SelfDescribing that = (SelfDescribing) o;

if (base64Encode != that.base64Encode) return false;
return Objects.equals(eventData, that.eventData);
}

@Override
public int hashCode() {
int result = eventData != null ? eventData.hashCode() : 0;
result = 31 * result + (base64Encode ? 1 : 0);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,19 @@ public long getByteSize() {
public String toString() {
return Utils.mapToJSONString(payload);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SelfDescribingJson that = (SelfDescribingJson) o;

return payload.equals(that.payload);
}

@Override
public int hashCode() {
return payload.hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.snowplowanalytics.snowplow.tracker.events;

import com.snowplowanalytics.snowplow.tracker.payload.SelfDescribingJson;

// JUnit
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

public class SelfDescribingTest {

@Test
public void testEqualityOfTwoInstances() {
SelfDescribing.Builder<?> builder = SelfDescribing.builder()
.eventData(new SelfDescribingJson("schema-name"));

SelfDescribing a = new SelfDescribing( builder );
SelfDescribing b = new SelfDescribing( builder );

assertEquals(a, b);
}

@Test
public void testNegativeEqualityOfTwoInstances() {
SelfDescribing.Builder<?> builderOne = SelfDescribing.builder()
.eventData(new SelfDescribingJson("schema-name-one"));

SelfDescribing.Builder<?> builderTwo = SelfDescribing.builder()
.eventData(new SelfDescribingJson("schema-name-two"));

SelfDescribing a = new SelfDescribing( builderOne );
SelfDescribing b = new SelfDescribing( builderTwo );

assertNotEquals(a, b);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotEquals;



public class SelfDescribingJsonTest {

Expand Down Expand Up @@ -67,4 +70,64 @@ public void testMakeSdjWithSdj() {
assertNotNull(sdj);
assertEquals(expected, sdjString);
}

@Test
public void testEqualityOfTwoInstances_withSchemaNameOnly() {
SelfDescribingJson a = new SelfDescribingJson("schema");
SelfDescribingJson b = new SelfDescribingJson("schema");
assertEquals(a, b);
}

@Test
public void testEqualityOfTwoInstances_withTrackerPayload() {
TrackerPayload nestedData = new TrackerPayload();
nestedData.add("key", "value");
SelfDescribingJson a = new SelfDescribingJson("schema", nestedData);
SelfDescribingJson b = new SelfDescribingJson("schema", nestedData);
assertEquals(a, b);
}

@Test
public void testEqualityOfTwoInstances_withNestedEvent() {
TrackerPayload nestedData = new TrackerPayload();
nestedData.add("key", "value");
SelfDescribingJson nestedEvent = new SelfDescribingJson("nested_event", nestedData);
SelfDescribingJson a = new SelfDescribingJson("schema", nestedEvent);
SelfDescribingJson b = new SelfDescribingJson("schema", nestedEvent);
assertEquals(a, b);
}

@Test
public void testNegativeEqualityOfTwoInstances_withSchemaNameOnly() {
SelfDescribingJson a = new SelfDescribingJson("schema-one");
SelfDescribingJson b = new SelfDescribingJson("schema-two");
assertNotEquals(a, b);
}

@Test
public void testNegativeEqualityOfTwoInstances_withTrackerPayload() {
TrackerPayload nestedDataOne = new TrackerPayload();
nestedDataOne.add("key", "value-one");
TrackerPayload nestedDataTwo = new TrackerPayload();
nestedDataTwo.add("key", "value-two");
SelfDescribingJson a = new SelfDescribingJson("schema", nestedDataOne);
SelfDescribingJson b = new SelfDescribingJson("schema", nestedDataTwo);
assertNotEquals(a, b);
}

@Test
public void testNegativeEqualityOfTwoInstances_withNestedEvent() {
TrackerPayload nestedDataOne = new TrackerPayload();
nestedDataOne.add("key", "value-one");
SelfDescribingJson nestedEventOne = new SelfDescribingJson("nested_event", nestedDataOne);

TrackerPayload nestedDataTwo = new TrackerPayload();
nestedDataTwo.add("key", "value-two");
SelfDescribingJson nestedEventTwo = new SelfDescribingJson("nested_event", nestedDataTwo);


SelfDescribingJson a = new SelfDescribingJson("schema", nestedEventOne);
SelfDescribingJson b = new SelfDescribingJson("schema", nestedEventTwo);
assertNotEquals(a, b);
}
}

0 comments on commit edc2d23

Please sign in to comment.