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..60e159b2 100644 --- a/CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java +++ b/CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java @@ -66,6 +66,37 @@ public Entity(EntityUID uid, Map attributes, Set paren this.tags = new HashMap<>(tags); } + /** + * An InnerEntity is a data wrapper for an Entity that holds its entity uid, attributes, and ancestors + */ + public static class InnerEntity { + public final EntityUID euid; + public final Map attrs; + public final Set ancestors; + + protected InnerEntity(Entity entity) { + this.euid = entity.getEUID(); + this.attrs = entity.attrs; + this.ancestors = entity.getParents(); + } + + public EntityUID getEntityUid() { + return this.euid; + } + + public Map getAttributes() { + return attrs; + } + + public Set getAncestors() { + return ancestors; + } + } + + public InnerEntity intoInner() { + return new InnerEntity(this); + } + /** * Get the value for the given attribute, or null if not present. * diff --git a/CedarJava/src/test/java/com/cedarpolicy/EntityTests.java b/CedarJava/src/test/java/com/cedarpolicy/EntityTests.java index 94c78379..e71e9978 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; @@ -26,7 +26,7 @@ import com.cedarpolicy.value.*; import com.cedarpolicy.model.entity.Entity; - +import com.cedarpolicy.model.entity.Entity.InnerEntity; public class EntityTests { @@ -49,4 +49,27 @@ public void getAttrTests() { // Test key not found assertEquals(principal.getAttr("decimalAttr"), null); } + + @Test + public void intoInnerTests() { + PrimString stringAttr = new PrimString("stringAttrValue"); + HashMap attrs = new HashMap<>(); + attrs.put("stringAttr", stringAttr); + + EntityTypeName principalType = EntityTypeName.parse("User").get(); + + HashSet parents = new HashSet(); + parents.add(principalType.of("Bob")); + + PrimString longTag = new PrimString("longTagValue"); + HashMap tags = new HashMap<>(); + tags.put("tag", longTag); + + Entity principal = new Entity(principalType.of("Alice"), attrs, parents, tags); + InnerEntity innerPrincipal = principal.intoInner(); + + assertEquals(innerPrincipal.getEntityUid(), principalType.of("Alice")); + assertEquals(innerPrincipal.getAttributes(), attrs); + assertEquals(innerPrincipal.getAncestors(), parents); + } }