use of org.structr.core.entity.relationship.PrincipalOwnsNode in project structr by structr.
the class AccessControlTest method test02SetDifferentPrincipalTypesAsOwner.
@Test
public void test02SetDifferentPrincipalTypesAsOwner() {
try (final Tx tx = app.tx()) {
final List<Principal> users = createTestNodes(Principal.class, 2);
final Principal user1 = (Principal) users.get(0);
final Group group1 = createTestNode(Group.class, "test group");
final TestOne t1 = createTestNode(TestOne.class);
t1.setProperty(AbstractNode.owner, user1);
t1.setProperty(AbstractNode.owner, group1);
assertEquals(group1, t1.getProperty(AbstractNode.owner));
Ownership ownerRel = t1.getIncomingRelationship(PrincipalOwnsNode.class);
assertNotNull(ownerRel);
// Do additional low-level check here to ensure cardinality!
List<Relationship> incomingRels = Iterables.toList(t1.getNode().getRelationships(Direction.INCOMING, new PrincipalOwnsNode()));
assertEquals(1, incomingRels.size());
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.relationship.PrincipalOwnsNode in project structr by structr.
the class BasicTest method testRelationshipsOnNodeCreation.
@Test
public void testRelationshipsOnNodeCreation() {
Principal user = null;
TestOne test = null;
// create user
try (final Tx tx = app.tx()) {
user = app.create(Principal.class, "tester");
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final SecurityContext ctx = SecurityContext.getInstance(user, AccessMode.Backend);
final App app = StructrApp.getInstance(ctx);
// create object with user context
try (final Tx tx = app.tx()) {
test = app.create(TestOne.class);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// query for relationships
try (final Tx tx = app.tx()) {
final List<? extends RelationshipInterface> rels1 = app.relationshipQuery().and(AbstractRelationship.sourceId, user.getUuid()).getAsList();
final List<Class> classes1 = rels1.stream().map(r -> r.getClass()).collect(Collectors.toList());
assertEquals("Invalid number of relationships after object creation", 2, rels1.size());
assertTrue("Invalid relationship type after object creation", classes1.contains(Security.class));
assertTrue("Invalid relationship type after object creation", classes1.contains(PrincipalOwnsNode.class));
final List<? extends RelationshipInterface> rels2 = app.relationshipQuery().and(AbstractRelationship.targetId, test.getUuid()).getAsList();
final List<Class> classes2 = rels2.stream().map(r -> r.getClass()).collect(Collectors.toList());
assertEquals("Invalid number of relationships after object creation", 2, rels2.size());
assertTrue("Invalid relationship type after object creation", classes2.contains(Security.class));
assertTrue("Invalid relationship type after object creation", classes2.contains(PrincipalOwnsNode.class));
final List<? extends RelationshipInterface> rels3 = Iterables.toList(test.getIncomingRelationships());
final List<Class> classes3 = rels3.stream().map(r -> r.getClass()).collect(Collectors.toList());
assertEquals("Invalid number of relationships after object creation", 2, rels3.size());
assertTrue("Invalid relationship type after object creation", classes3.contains(Security.class));
assertTrue("Invalid relationship type after object creation", classes3.contains(PrincipalOwnsNode.class));
final Security sec = app.relationshipQuery(Security.class).getFirst();
assertNotNull("Relationship caching on node creation is broken", sec);
final PrincipalOwnsNode owns = app.relationshipQuery(PrincipalOwnsNode.class).getFirst();
assertNotNull("Relationship caching on node creation is broken", owns);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
Aggregations