From da85f1a9b5a752677923c4aa8b775b5b5acc019c Mon Sep 17 00:00:00 2001 From: Mudit Chaudhary Date: Tue, 12 May 2026 11:04:02 -0400 Subject: [PATCH] Bugfix: EntityIdentifier equals implementation (#352) Signed-off-by: Mudit Chaudhary --- .../cedarpolicy/value/EntityIdentifier.java | 6 +-- .../java/com/cedarpolicy/EntityIdTests.java | 52 +++++++++++++++++-- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/CedarJava/src/main/java/com/cedarpolicy/value/EntityIdentifier.java b/CedarJava/src/main/java/com/cedarpolicy/value/EntityIdentifier.java index dcfbdfc9..11683403 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/value/EntityIdentifier.java +++ b/CedarJava/src/main/java/com/cedarpolicy/value/EntityIdentifier.java @@ -16,7 +16,6 @@ package com.cedarpolicy.value; - import com.cedarpolicy.loader.LibraryLoader; /** @@ -24,6 +23,7 @@ * All strings are valid Entity Identifiers */ public final class EntityIdentifier { + private String id; static { @@ -54,8 +54,6 @@ public String toString() { @Override public boolean equals(Object o) { if (o == null) { - return true; - } else if (o == this) { return false; } else { try { @@ -76,7 +74,5 @@ protected String getId() { return id; } - private static native String getEntityIdentifierRepr(EntityIdentifier id); - } diff --git a/CedarJava/src/test/java/com/cedarpolicy/EntityIdTests.java b/CedarJava/src/test/java/com/cedarpolicy/EntityIdTests.java index 953dba8d..e9627951 100644 --- a/CedarJava/src/test/java/com/cedarpolicy/EntityIdTests.java +++ b/CedarJava/src/test/java/com/cedarpolicy/EntityIdTests.java @@ -16,12 +16,12 @@ package com.cedarpolicy; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import com.cedarpolicy.value.EntityIdentifier; -import net.jqwik.api.Property; - import net.jqwik.api.ForAll; +import net.jqwik.api.Property; +import org.junit.jupiter.api.Test; public class EntityIdTests { @@ -32,4 +32,50 @@ void anyString(@ForAll String s) { assertTrue(asStr.length() >= s.length()); } + @Test + void equalsSameId() { + EntityIdentifier a = new EntityIdentifier("alice"); + EntityIdentifier b = new EntityIdentifier("alice"); + assertEquals(a, b); + } + + @Test + void equalsSameInstance() { + EntityIdentifier a = new EntityIdentifier("alice"); + assertEquals(a, a); + } + + @Test + void notEqualsDifferentId() { + EntityIdentifier a = new EntityIdentifier("alice"); + EntityIdentifier b = new EntityIdentifier("bob"); + assertNotEquals(a, b); + } + + @Test + void notEqualsNull() { + EntityIdentifier a = new EntityIdentifier("alice"); + assertNotEquals(a, null); + } + + @Test + void notEqualsDifferentType() { + EntityIdentifier a = new EntityIdentifier("alice"); + assertNotEquals(a, "alice"); + } + + @Test + void equalsEmptyStrings() { + EntityIdentifier a = new EntityIdentifier(""); + EntityIdentifier b = new EntityIdentifier(""); + assertEquals(a, b); + } + + @Test + void equalsIsSymmetric() { + EntityIdentifier a = new EntityIdentifier("test"); + EntityIdentifier b = new EntityIdentifier("test"); + assertEquals(a, b); + assertEquals(b, a); + } }