Search in sources :

Example 41 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class StructrUiTest method createTestNode.

protected <T extends NodeInterface> T createTestNode(final Class<T> type, final NodeAttribute... attrs) throws FrameworkException {
    final PropertyMap props = new PropertyMap();
    props.put(AbstractNode.type, type.getSimpleName());
    props.put(AbstractNode.name, type.getSimpleName());
    for (final NodeAttribute attr : attrs) {
        props.put(attr.getKey(), attr.getValue());
    }
    return app.create(type, props);
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) PropertyMap(org.structr.core.property.PropertyMap)

Example 42 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class AdvancedSearchTest method testSearchWithOwnerAndEnumOnDynamicNodes.

@Test
public void testSearchWithOwnerAndEnumOnDynamicNodes() {
    createTestUserType();
    try {
        // create 3 users
        final String user1 = createEntity("/test_users", "{ name: user1 }");
        final String user2 = createEntity("/test_users", "{ name: user2 }");
        final String user3 = createEntity("/test_users", "{ name: user3 }");
        SchemaNode node = null;
        // setup schema
        try (final Tx tx = app.tx()) {
            node = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "TestType"));
            node.setProperty(new StringProperty("_status"), "Enum(one, two, three)");
            node.setProperty(new StringProperty("___ui"), "status");
            tx.success();
        }
        // create 9 test entities
        final String test1 = createEntity("/test_types", "{ name: test1, owner: " + user1 + ", status: one }");
        final String test2 = createEntity("/test_types", "{ name: test2, owner: " + user2 + ", status: two }");
        final String test3 = createEntity("/test_types", "{ name: test3, owner: " + user3 + ", status: one }");
        final String test4 = createEntity("/test_types", "{ name: test4, owner: " + user1 + ", status: two }");
        final String test5 = createEntity("/test_types", "{ name: test5, owner: " + user2 + ", status: three }");
        final String test6 = createEntity("/test_types", "{ name: test6, owner: " + user3 + ", status: one }");
        final String test7 = createEntity("/test_types", "{ name: test7, owner: " + user1 + ", status: two }");
        final String test8 = createEntity("/test_types", "{ name: test8, owner: " + user2 + ", status: three }");
        final String test9 = createEntity("/test_types", "{ name: test9, owner: " + user3 + ", status: one }");
        // check that all entities are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(9)).body("result_count", equalTo(9)).body("result[0].id", equalTo(test1)).body("result[1].id", equalTo(test2)).body("result[2].id", equalTo(test3)).body("result[3].id", equalTo(test4)).body("result[4].id", equalTo(test5)).body("result[5].id", equalTo(test6)).body("result[6].id", equalTo(test7)).body("result[7].id", equalTo(test8)).body("result[8].id", equalTo(test9)).when().get("/test_types/ui?sort=name");
        // check entities of user1 are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(3)).body("result_count", equalTo(3)).body("result[0].id", equalTo(test1)).body("result[1].id", equalTo(test4)).body("result[2].id", equalTo(test7)).when().get("/test_types/ui?sort=createdDate&owner=" + user1);
        // check entities of user2 are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(3)).body("result_count", equalTo(3)).body("result[0].id", equalTo(test2)).body("result[1].id", equalTo(test5)).body("result[2].id", equalTo(test8)).when().get("/test_types/ui?sort=createdDate&owner=" + user2);
        // check entities of user3 are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(3)).body("result_count", equalTo(3)).body("result[0].id", equalTo(test3)).body("result[1].id", equalTo(test6)).body("result[2].id", equalTo(test9)).when().get("/test_types/ui?sort=createdDate&owner=" + user3);
        // check entities of user1 with a given enum are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(1)).body("result[0].id", equalTo(test1)).when().get("/test_types/ui?sort=createdDate&owner=" + user1 + "&status=one");
        // check entities of user1 with a given enum are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(2)).body("result_count", equalTo(2)).body("result[0].id", equalTo(test4)).body("result[1].id", equalTo(test7)).when().get("/test_types/ui?sort=createdDate&owner=" + user1 + "&status=two");
        // check entities of user1 with a given enum are there
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("result", hasSize(2)).body("result_count", equalTo(2)).body("result[0].id", equalTo(test5)).body("result[1].id", equalTo(test8)).when().get("/test_types/ui?sort=createdDate&owner=" + user2 + "&status=three");
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) Test(org.junit.Test) IndexingTest(org.structr.rest.common.IndexingTest)

Example 43 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class PermissionResolutionTest method test01SimplePermissionResolution.

@Test
public void test01SimplePermissionResolution() {
    SchemaRelationshipNode rel = null;
    PropertyKey key = null;
    Principal user1 = null;
    Class type1 = null;
    Class type2 = null;
    try (final Tx tx = app.tx()) {
        // create a test user
        user1 = app.create(Principal.class, "user1");
        // create schema setup with permission propagation
        final SchemaNode t1 = app.create(SchemaNode.class, "Type1");
        final SchemaNode t2 = app.create(SchemaNode.class, "Type2");
        rel = app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, t1), new NodeAttribute<>(SchemaRelationshipNode.targetNode, t2), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "RELATED"), new NodeAttribute<>(SchemaRelationshipNode.sourceMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.targetMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.sourceJsonName, "source"), new NodeAttribute<>(SchemaRelationshipNode.targetJsonName, "target"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    Assert.assertNotNull("User should have been created", user1);
    // expect object of type 2 to be visible as well
    try (final Tx tx = app.tx()) {
        type1 = StructrApp.getConfiguration().getNodeEntityClass("Type1");
        type2 = StructrApp.getConfiguration().getNodeEntityClass("Type2");
        key = StructrApp.key(type1, "target");
        Assert.assertNotNull("Node type Type1 should exist.", type1);
        Assert.assertNotNull("Node type Type2 should exist.", type2);
        Assert.assertNotNull("Property key \"target\" should exist.", key);
        final NodeInterface instance1 = app.create(type1, "instance1OfType1");
        final NodeInterface instance2 = app.create(type2, "instance1OfType2");
        Assert.assertNotNull("Instance of type Type1 should exist", instance1);
        Assert.assertNotNull("Instance of type Type2 should exist", instance2);
        instance1.setProperty(key, instance2);
        // make instance1 visible to user1
        instance1.grant(Permission.read, user1);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // check access for user1 on instance1
    final App userApp = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Backend));
    try (final Tx tx = userApp.tx()) {
        Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
        Assert.assertNull("User1 should NOT be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // because the resolution direction is wrong.
    try (final Tx tx = app.tx()) {
        rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.In);
        rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // check access for user1 on instance1
    try (final Tx tx = userApp.tx()) {
        Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
        Assert.assertNull("User1 should NOT be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // because the resolution direction is correct
    try (final Tx tx = app.tx()) {
        rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.Out);
        rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // check access for user1 on instance1
    try (final Tx tx = userApp.tx()) {
        Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
        Assert.assertNotNull("User1 should be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // because both resolution directions are enabled
    try (final Tx tx = app.tx()) {
        rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.Both);
        rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // check access for user1 on instance1
    try (final Tx tx = userApp.tx()) {
        Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
        Assert.assertNotNull("User1 should be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // object invisible again.
    try (final Tx tx = app.tx()) {
        rel.setProperty(SchemaRelationshipNode.permissionPropagation, Direction.None);
        rel.setProperty(SchemaRelationshipNode.readPropagation, Propagation.Add);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // check access for user1 on instance1
    try (final Tx tx = userApp.tx()) {
        Assert.assertNotNull("User1 should be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
        Assert.assertNull("User1 should NOT be able to find instance of type Type2", userApp.nodeQuery(type2).getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SchemaNode(org.structr.core.entity.SchemaNode) NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) PropertyKey(org.structr.core.property.PropertyKey) Principal(org.structr.core.entity.Principal) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 44 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class SearchAndSortingTest method test08InexactSearch.

@Test
public void test08InexactSearch() {
    try {
        final Date date = new Date();
        final TestOne test = createTestNode(TestOne.class, new NodeAttribute(TestOne.name, "TestOne"), new NodeAttribute(TestOne.aBoolean, true), new NodeAttribute(TestOne.aDate, date), new NodeAttribute(TestOne.aDouble, 1.234), new NodeAttribute(TestOne.aLong, 12345L), new NodeAttribute(TestOne.anEnum, TestOne.Status.One), new NodeAttribute(TestOne.anInt, 123));
        try (final Tx tx = app.tx()) {
            assertEquals("Invalid inexact search result for type String", test, app.nodeQuery(TestOne.class).and(TestOne.name, "TestOne", false).getFirst());
            assertEquals("Invalid inexact search result for type Boolean", test, app.nodeQuery(TestOne.class).and(TestOne.aBoolean, true, false).getFirst());
            assertEquals("Invalid inexact search result for type Date", test, app.nodeQuery(TestOne.class).and(TestOne.aDate, date, false).getFirst());
            assertEquals("Invalid inexact search result for type Double", test, app.nodeQuery(TestOne.class).and(TestOne.aDouble, 1.234, false).getFirst());
            assertEquals("Invalid inexact search result for type Long", test, app.nodeQuery(TestOne.class).and(TestOne.aLong, 12345L, false).getFirst());
            assertEquals("Invalid inexact search result for type String", test, app.nodeQuery(TestOne.class).and(TestOne.anEnum, TestOne.Status.One, false).getFirst());
            assertEquals("Invalid inexact search result for type Enum", test, app.nodeQuery(TestOne.class).and(TestOne.anInt, 123, false).getFirst());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Date(java.util.Date) Test(org.junit.Test)

Example 45 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class SystemTest method testCallbackOrder.

@Test
public void testCallbackOrder() {
    try {
        // ##################################### test creation callbacks
        TestEight test = null;
        try (final Tx tx = app.tx()) {
            test = app.create(TestEight.class, new NodeAttribute(TestEight.testProperty, 123));
            tx.success();
        }
        // only the creation methods should have been called now!
        assertTrue("onCreationTimestamp should be != 0", test.getOnCreationTimestamp() != 0L);
        assertEquals("onModificationTimestamp should be == 0", 0L, test.getOnModificationTimestamp());
        assertEquals("onDeletionTimestamp should be == 0", 0L, test.getOnDeletionTimestamp());
        // only the creation methods should have been called now!
        assertTrue("afterCreationTimestamp should be != 0", test.getAfterCreationTimestamp() != 0L);
        assertEquals("afterModificationTimestamp should be == 0", 0L, test.getAfterModificationTimestamp());
        // ##################################### test modification callbacks
        // reset timestamps
        test.resetTimestamps();
        try (final Tx tx = app.tx()) {
            test.setProperty(TestEight.testProperty, 234);
            tx.success();
        }
        // only the modification methods should have been called now!
        assertEquals("onCreationTimestamp should be == 0", 0L, test.getOnCreationTimestamp());
        assertTrue("onModificationTimestamp should be != 0", test.getOnModificationTimestamp() != 0L);
        assertEquals("onDeletionTimestamp should be == 0", 0L, test.getOnDeletionTimestamp());
        // only the modification methods should have been called now!
        assertEquals("afterCreationTimestamp should be == 0", 0L, test.getAfterCreationTimestamp());
        assertTrue("afterModificationTimestamp should be != 0", test.getAfterModificationTimestamp() != 0L);
        // ##################################### test non-modifying set operation
        // reset timestamps
        test.resetTimestamps();
        try (final Tx tx = app.tx()) {
            test.setProperty(TestEight.testProperty, 234);
            tx.success();
        }
        // only the creation methods should have been called now!
        assertEquals("onCreationTimestamp should be == 0", 0L, test.getOnCreationTimestamp());
        assertEquals("onModificationTimestamp should be == 0", 0L, test.getOnModificationTimestamp());
        assertEquals("onDeletionTimestamp should be == 0", 0L, test.getOnDeletionTimestamp());
        // only the creation methods should have been called now!
        assertEquals("afterCreationTimestamp should be == 0", 0L, test.getAfterCreationTimestamp());
        assertEquals("afterModificationTimestamp should be == 0", 0L, test.getAfterModificationTimestamp());
        // ##################################### test deletion
        // reset timestamps
        test.resetTimestamps();
        try (final Tx tx = app.tx()) {
            app.delete(test);
            tx.success();
        }
        // only the creation methods should have been called now!
        assertEquals("onCreationTimestamp should be == 0", 0L, test.getOnCreationTimestamp());
        assertEquals("onModificationTimestamp should be == 0", 0L, test.getOnModificationTimestamp());
        assertTrue("onDeletionTimestamp should be != 0", test.getOnDeletionTimestamp() != 0L);
        // only the creation methods should have been called now!
        assertEquals("afterCreationTimestamp should be == 0", 0L, test.getAfterCreationTimestamp());
        assertEquals("afterModificationTimestamp should be == 0", 0L, test.getAfterModificationTimestamp());
    } catch (FrameworkException ex) {
        logger.error("Error", ex);
        fail("Unexpected exception.");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestEight(org.structr.core.entity.TestEight) Test(org.junit.Test)

Aggregations

NodeAttribute (org.structr.core.graph.NodeAttribute)73 Tx (org.structr.core.graph.Tx)55 FrameworkException (org.structr.common.error.FrameworkException)54 Test (org.junit.Test)45 App (org.structr.core.app.App)23 StructrApp (org.structr.core.app.StructrApp)23 StructrUiTest (org.structr.web.StructrUiTest)21 LinkedList (java.util.LinkedList)19 PropertyKey (org.structr.core.property.PropertyKey)17 SchemaNode (org.structr.core.entity.SchemaNode)14 NodeInterface (org.structr.core.graph.NodeInterface)13 Principal (org.structr.core.entity.Principal)12 PropertyMap (org.structr.core.property.PropertyMap)12 StringProperty (org.structr.core.property.StringProperty)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 File (org.structr.web.entity.File)10 Folder (org.structr.web.entity.Folder)9 AbstractFile (org.structr.web.entity.AbstractFile)8 List (java.util.List)7 StructrTest (org.structr.common.StructrTest)7