Skip to content

Commit 282c411

Browse files
committed
Entity into_inner method
Signed-off-by: John Brain <jnbrain@amazon.com>
1 parent 89268f6 commit 282c411

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,37 @@ public Entity(EntityUID uid, Map<String, Value> attributes, Set<EntityUID> paren
6666
this.tags = new HashMap<>(tags);
6767
}
6868

69+
/**
70+
* An InnerEntity is a data wrapper for an Entity that holds its entity uid, attributes, and ancestors
71+
*/
72+
public static class InnerEntity {
73+
public final EntityUID euid;
74+
public final Map<String, Value> attrs;
75+
public final Set<EntityUID> ancestors;
76+
77+
protected InnerEntity(Entity entity) {
78+
this.euid = entity.getEUID();
79+
this.attrs = entity.attrs;
80+
this.ancestors = entity.getParents();
81+
}
82+
83+
public EntityUID getEntityUid() {
84+
return this.euid;
85+
}
86+
87+
public Map<String, Value> getAttributes() {
88+
return attrs;
89+
}
90+
91+
public Set<EntityUID> getAncestors() {
92+
return ancestors;
93+
}
94+
}
95+
96+
public InnerEntity intoInner() {
97+
return new InnerEntity(this);
98+
}
99+
69100
/**
70101
* Get the value for the given attribute, or null if not present.
71102
*

CedarJava/src/test/java/com/cedarpolicy/EntityTests.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.cedarpolicy;
17+
package com.cedarpolicy;
1818

1919
import org.junit.jupiter.api.Test;
2020
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -26,7 +26,7 @@
2626

2727
import com.cedarpolicy.value.*;
2828
import com.cedarpolicy.model.entity.Entity;
29-
29+
import com.cedarpolicy.model.entity.Entity.InnerEntity;
3030

3131
public class EntityTests {
3232

@@ -49,4 +49,27 @@ public void getAttrTests() {
4949
// Test key not found
5050
assertEquals(principal.getAttr("decimalAttr"), null);
5151
}
52+
53+
@Test
54+
public void intoInnerTests() {
55+
PrimString stringAttr = new PrimString("stringAttrValue");
56+
HashMap<String, Value> attrs = new HashMap<>();
57+
attrs.put("stringAttr", stringAttr);
58+
59+
EntityTypeName principalType = EntityTypeName.parse("User").get();
60+
61+
HashSet<EntityUID> parents = new HashSet<EntityUID>();
62+
parents.add(principalType.of("Bob"));
63+
64+
PrimString longTag = new PrimString("longTagValue");
65+
HashMap<String, Value> tags = new HashMap<>();
66+
tags.put("tag", longTag);
67+
68+
Entity principal = new Entity(principalType.of("Alice"), attrs, parents, tags);
69+
InnerEntity innerPrincipal = principal.intoInner();
70+
71+
assertEquals(innerPrincipal.getEntityUid(), principalType.of("Alice"));
72+
assertEquals(innerPrincipal.getAttributes(), attrs);
73+
assertEquals(innerPrincipal.getAncestors(), parents);
74+
}
5275
}

0 commit comments

Comments
 (0)