Skip to content
Draft
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
31 changes: 31 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 @@ -66,6 +66,37 @@ public Entity(EntityUID uid, Map<String, Value> attributes, Set<EntityUID> 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<String, Value> attrs;
public final Set<EntityUID> 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<String, Value> getAttributes() {
return attrs;
}

public Set<EntityUID> getAncestors() {
return ancestors;
}
}

public InnerEntity intoInner() {
return new InnerEntity(this);
}

/**
* Get the value for the given attribute, or null if not present.
*
Expand Down
27 changes: 25 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 @@ -26,7 +26,7 @@

import com.cedarpolicy.value.*;
import com.cedarpolicy.model.entity.Entity;

import com.cedarpolicy.model.entity.Entity.InnerEntity;

public class EntityTests {

Expand All @@ -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<String, Value> attrs = new HashMap<>();
attrs.put("stringAttr", stringAttr);

EntityTypeName principalType = EntityTypeName.parse("User").get();

HashSet<EntityUID> parents = new HashSet<EntityUID>();
parents.add(principalType.of("Bob"));

PrimString longTag = new PrimString("longTagValue");
HashMap<String, Value> 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);
}
}