Skip to content

Commit 4943f87

Browse files
jtbraincdisselkoen
andcommitted
Add additional Entity constructors (cedar-policy#288)
Signed-off-by: John Brain <jnbrain@amazon.com> Co-authored-by: Craig Disselkoen <craigdissel@gmail.com>
1 parent 6a95b4b commit 4943f87

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ public class Entity {
4040
/** Tags on this entity (RFC 82) */
4141
public final Map<String, Value> tags;
4242

43+
/**
44+
* Create an entity from an EntityUID. It will have no attributes, parents, or tags.
45+
*
46+
* @param uid EUID of the Entity.
47+
*/
48+
public Entity(EntityUID uid) {
49+
this(uid, new HashMap<>(), new HashSet<>(), new HashMap<>());
50+
}
51+
52+
/**
53+
* Create an entity from an EntityUID and a set of parent EntityUIDs. It will have no attributes or tags.
54+
*
55+
* @param uid EUID of the Entity.
56+
* @param parentsEUIDs Set of parent entities' EUIDs.
57+
*/
58+
public Entity(EntityUID uid, Set<EntityUID> parentsEUIDs) {
59+
this(uid, new HashMap<>(), parentsEUIDs, new HashMap<>());
60+
}
61+
4362
/**
4463
* Create an entity from an EntityUIDs, a map of attributes, and a set of parent EntityUIDs.
4564
*

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

Lines changed: 34 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;
@@ -27,7 +27,6 @@
2727
import com.cedarpolicy.value.*;
2828
import com.cedarpolicy.model.entity.Entity;
2929

30-
3130
public class EntityTests {
3231

3332
@Test
@@ -49,4 +48,37 @@ public void getAttrTests() {
4948
// Test key not found
5049
assertEquals(principal.getAttr("decimalAttr"), null);
5150
}
51+
52+
@Test
53+
public void newWithEntityUIDTests() {
54+
EntityTypeName principalType = EntityTypeName.parse("User").get();
55+
Entity principal = new Entity(principalType.of("Alice"));
56+
57+
// Test the Entity's uid
58+
assertEquals(principal.getEUID(), principalType.of("Alice"));
59+
60+
// Test that a key is not found
61+
assertEquals(principal.getAttr("stringAttr"), null);
62+
63+
// Test the Entity's parents
64+
assertEquals(principal.getParents().size(), 0);
65+
}
66+
67+
@Test
68+
public void newWithoutAttributesTests() {
69+
EntityTypeName principalType = EntityTypeName.parse("User").get();
70+
HashSet<EntityUID> parents = new HashSet<EntityUID>();
71+
parents.add(principalType.of("Bob"));
72+
73+
Entity principal = new Entity(principalType.of("Alice"), parents);
74+
75+
// Test the Entity's uid
76+
assertEquals(principal.getEUID(), principalType.of("Alice"));
77+
78+
// Test that a key is not found
79+
assertEquals(principal.getAttr("stringAttr"), null);
80+
81+
// Test the Entity's parents
82+
assertEquals(principal.getParents(), parents);
83+
}
5284
}

0 commit comments

Comments
 (0)