diff --git a/CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java b/CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java index bdb6dfe6..8b5e348b 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java @@ -40,6 +40,25 @@ public class Entity { /** Tags on this entity (RFC 82) */ public final Map tags; + /** + * Create an entity from an EntityUID. It will have no attributes, parents, or tags. + * + * @param uid EUID of the Entity. + */ + public Entity(EntityUID uid) { + this(uid, new HashMap<>(), new HashSet<>(), new HashMap<>()); + } + + /** + * Create an entity from an EntityUID and a set of parent EntityUIDs. It will have no attributes or tags. + * + * @param uid EUID of the Entity. + * @param parentsEUIDs Set of parent entities' EUIDs. + */ + public Entity(EntityUID uid, Set parentsEUIDs) { + this(uid, new HashMap<>(), parentsEUIDs, new HashMap<>()); + } + /** * Create an entity from an EntityUIDs, a map of attributes, and a set of parent EntityUIDs. * diff --git a/CedarJava/src/test/java/com/cedarpolicy/EntityTests.java b/CedarJava/src/test/java/com/cedarpolicy/EntityTests.java index 94c78379..84a0bb2a 100644 --- a/CedarJava/src/test/java/com/cedarpolicy/EntityTests.java +++ b/CedarJava/src/test/java/com/cedarpolicy/EntityTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ - package com.cedarpolicy; +package com.cedarpolicy; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -27,7 +27,6 @@ import com.cedarpolicy.value.*; import com.cedarpolicy.model.entity.Entity; - public class EntityTests { @Test @@ -49,4 +48,37 @@ public void getAttrTests() { // Test key not found assertEquals(principal.getAttr("decimalAttr"), null); } + + @Test + public void newWithEntityUIDTests() { + EntityTypeName principalType = EntityTypeName.parse("User").get(); + Entity principal = new Entity(principalType.of("Alice")); + + // Test the Entity's uid + assertEquals(principal.getEUID(), principalType.of("Alice")); + + // Test that a key is not found + assertEquals(principal.getAttr("stringAttr"), null); + + // Test the Entity's parents + assertEquals(principal.getParents().size(), 0); + } + + @Test + public void newWithoutAttributesTests() { + EntityTypeName principalType = EntityTypeName.parse("User").get(); + HashSet parents = new HashSet(); + parents.add(principalType.of("Bob")); + + Entity principal = new Entity(principalType.of("Alice"), parents); + + // Test the Entity's uid + assertEquals(principal.getEUID(), principalType.of("Alice")); + + // Test that a key is not found + assertEquals(principal.getAttr("stringAttr"), null); + + // Test the Entity's parents + assertEquals(principal.getParents(), parents); + } }