Search in sources :

Example 1 with PrincipalOwnsNode

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");
    }
}
Also used : Group(org.structr.core.entity.Group) Ownership(org.structr.core.entity.relationship.Ownership) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Relationship(org.structr.api.graph.Relationship) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 2 with PrincipalOwnsNode

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.");
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) LoggerFactory(org.slf4j.LoggerFactory) RelationshipType(org.structr.api.graph.RelationshipType) TestTen(org.structr.core.entity.TestTen) StringUtils(org.apache.commons.lang3.StringUtils) GenericNode(org.structr.core.entity.GenericNode) FrameworkException(org.structr.common.error.FrameworkException) App(org.structr.core.app.App) StringProperty(org.structr.core.property.StringProperty) Assert.fail(org.junit.Assert.fail) Location(org.structr.core.entity.Location) ResourceAccess(org.structr.core.entity.ResourceAccess) OneThreeOneToOne(org.structr.core.entity.OneThreeOneToOne) NodeHasLocation(org.structr.core.entity.relationship.NodeHasLocation) AbstractRelationship(org.structr.core.entity.AbstractRelationship) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) NotInTransactionException(org.structr.api.NotInTransactionException) GraphObject(org.structr.core.GraphObject) OneTwoOneToOne(org.structr.core.entity.OneTwoOneToOne) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SixOneManyToMany(org.structr.core.entity.SixOneManyToMany) List(java.util.List) DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) StructrApp(org.structr.core.app.StructrApp) TestOne(org.structr.core.entity.TestOne) Security(org.structr.core.entity.Security) TestSix(org.structr.core.entity.TestSix) PropertyMap(org.structr.core.property.PropertyMap) NotFoundException(org.structr.api.NotFoundException) NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyKey(org.structr.core.property.PropertyKey) TestNine(org.structr.core.entity.TestNine) Result(org.structr.core.Result) LinkedList(java.util.LinkedList) AbstractNode(org.structr.core.entity.AbstractNode) TestTwo(org.structr.core.entity.TestTwo) NodeInterface(org.structr.core.graph.NodeInterface) SixThreeOneToMany(org.structr.core.entity.SixThreeOneToMany) Logger(org.slf4j.Logger) Iterables(org.structr.api.util.Iterables) GenericRelationship(org.structr.core.entity.GenericRelationship) Assert.assertNotNull(org.junit.Assert.assertNotNull) Group(org.structr.core.entity.Group) Tx(org.structr.core.graph.Tx) Principal(org.structr.core.entity.Principal) Relation(org.structr.core.entity.Relation) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) TestSeven(org.structr.core.entity.TestSeven) TestThree(org.structr.core.entity.TestThree) TestFour(org.structr.core.entity.TestFour) NodeServiceCommand(org.structr.core.graph.NodeServiceCommand) Localization(org.structr.core.entity.Localization) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) RelationshipInterface(org.structr.core.graph.RelationshipInterface) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SchemaNode(org.structr.core.entity.SchemaNode) PrincipalOwnsNode(org.structr.core.entity.relationship.PrincipalOwnsNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Security(org.structr.core.entity.Security) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 FrameworkException (org.structr.common.error.FrameworkException)2 Group (org.structr.core.entity.Group)2 Principal (org.structr.core.entity.Principal)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Assert.fail (org.junit.Assert.fail)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 NotFoundException (org.structr.api.NotFoundException)1 NotInTransactionException (org.structr.api.NotInTransactionException)1 Relationship (org.structr.api.graph.Relationship)1