Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CedarJava/src/main/java/com/cedarpolicy/model/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ public class Entity {
/** Tags on this entity (RFC 82) */
public final Map<String, Value> 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<EntityUID> parentsEUIDs) {
this(uid, new HashMap<>(), parentsEUIDs, new HashMap<>());
}

/**
* Create an entity from an EntityUIDs, a map of attributes, and a set of parent EntityUIDs.
*
Expand Down
36 changes: 34 additions & 2 deletions CedarJava/src/test/java/com/cedarpolicy/EntityTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +27,6 @@
import com.cedarpolicy.value.*;
import com.cedarpolicy.model.entity.Entity;


public class EntityTests {

@Test
Expand All @@ -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<EntityUID> parents = new HashSet<EntityUID>();
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);
}
}