Search in sources :

Example 36 with NodeInterface

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

the class BasicTest method test07CascadeDeleteConditional.

/**
 * DELETE_IF_CONSTRAINT_WOULD_BE_VIOLATED should
 * trigger delete cascade from start to end node only
 * if the remote node would not be valid afterwards
 */
@Test
public void test07CascadeDeleteConditional() {
    try {
        AbstractRelationship rel = cascadeRel(TestOne.class, TestTwo.class, Relation.CONSTRAINT_BASED);
        NodeInterface sourceNode;
        String startNodeId;
        String endNodeId;
        try (final Tx tx = app.tx()) {
            startNodeId = rel.getSourceNode().getUuid();
            endNodeId = rel.getTargetNode().getUuid();
            sourceNode = rel.getSourceNode();
        }
        deleteCascade(sourceNode);
        try (final Tx tx = app.tx()) {
            // Start node should be deleted
            assertNodeNotFound(startNodeId);
            // End node should be deleted
            assertNodeNotFound(endNodeId);
        }
        rel = cascadeRel(TestOne.class, TestThree.class, Relation.CONSTRAINT_BASED);
        try (final Tx tx = app.tx()) {
            startNodeId = rel.getSourceNode().getUuid();
            endNodeId = rel.getTargetNode().getUuid();
            sourceNode = rel.getSourceNode();
        }
        deleteCascade(sourceNode);
        try (final Tx tx = app.tx()) {
            // Start node should be deleted
            assertNodeNotFound(startNodeId);
            // End node should still be there
            assertNodeExists(endNodeId);
        }
        rel = cascadeRel(TestOne.class, TestFour.class, Relation.CONSTRAINT_BASED);
        try (final Tx tx = app.tx()) {
            startNodeId = rel.getSourceNode().getUuid();
            endNodeId = rel.getTargetNode().getUuid();
            sourceNode = rel.getSourceNode();
        }
        deleteCascade(sourceNode);
        try (final Tx tx = app.tx()) {
            // Start node should be deleted
            assertNodeNotFound(startNodeId);
            // End node should still be there
            assertNodeExists(endNodeId);
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : TestFour(org.structr.core.entity.TestFour) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestThree(org.structr.core.entity.TestThree) AbstractRelationship(org.structr.core.entity.AbstractRelationship) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 37 with NodeInterface

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

the class CustomPermissionQueriesTest method test01SimplePermissionResolutionRead.

@Test
public void test01SimplePermissionResolutionRead() {
    final Class<Principal> principalType = StructrApp.getConfiguration().getNodeEntityClass("Principal");
    Principal user1 = null;
    Class type1 = null;
    try (final Tx tx = app.tx()) {
        // create a test user
        user1 = app.create(principalType, "user1");
        final SchemaNode t1 = app.create(SchemaNode.class, "Type1");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    Assert.assertNotNull("User should have been created", user1);
    try (final Tx tx = app.tx()) {
        type1 = StructrApp.getConfiguration().getNodeEntityClass("Type1");
        Assert.assertNotNull("Node type Type1 should exist.", type1);
        final NodeInterface instance1 = app.create(type1, "instance1OfType1");
        Assert.assertNotNull("Instance of type Type1 should exist", instance1);
        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.assertNull("User1 should NOT be able to find instance of type Type1", userApp.nodeQuery(type1).getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // set custom permission query on user
    try (final Tx tx = userApp.tx()) {
        // query returns always true if user exists
        user1.setProperty(StructrApp.key(Principal.class, "customPermissionQueryRead"), "MATCH (p:Principal {id: {principalUuid}}) RETURN p IS NOT NULL");
        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());
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // set custom permission query on user
    try (final Tx tx = userApp.tx()) {
        // query returns always false if user exists
        user1.setProperty(StructrApp.key(Principal.class, "customPermissionQueryRead"), "MATCH (p:Principal {id: {principalUuid}}) RETURN p IS NULL");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    // check access for user1 on instance1
    try (final Tx tx = userApp.tx()) {
        Assert.assertNull("User1 should NOT be able to find instance of type Type1", userApp.nodeQuery(type1).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) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 38 with NodeInterface

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

the class SearchAndSortingTest method test02Paging.

/**
 * Test different pages and page sizes
 */
@Test
public void test02Paging() {
    try {
        boolean includeDeletedAndHidden = false;
        boolean publicOnly = false;
        Class type = TestOne.class;
        // no more than 89 to avoid sort order TestOne-10, TestOne-100 ...
        int number = 89;
        final int offset = 10;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        try (final Tx tx = app.tx()) {
            int i = offset;
            for (NodeInterface node : nodes) {
                // System.out.println("Node ID: " + node.getNodeId());
                String _name = "TestOne-" + i;
                i++;
                node.setProperty(AbstractNode.name, _name);
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            // test pages sizes from 0 to 10
            for (int ps = 0; ps < 10; ps++) {
                // test all pages
                for (int p = 0; p < (number / Math.max(1, ps)) + 1; p++) {
                    testPaging(type, ps, p, number, offset, includeDeletedAndHidden, publicOnly, sortKey, sortDesc);
                }
            }
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Random(java.util.Random) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 39 with NodeInterface

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

the class SearchAndSortingTest method test07SortByIntWithNullValues.

@Test
public void test07SortByIntWithNullValues() {
    try {
        final List<TestOne> nodes = this.createTestNodes(TestOne.class, 10);
        try (final Tx tx = app.tx()) {
            int i = 0;
            for (NodeInterface node : nodes) {
                node.setProperty(AbstractNode.name, Long.toString(i));
                if (i < 7) {
                    node.setProperty(TestOne.anInt, i);
                }
                i++;
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            boolean sortDesc = false;
            final List<TestOne> result = app.nodeQuery(TestOne.class).sort(TestOne.anInt).order(sortDesc).getAsList();
            // check that the sorting is stable, i.e. the position of nodes
            // with equal values (and null) is not modified by sorting
            final Iterator<TestOne> nameIterator = result.iterator();
            while (nameIterator.hasNext()) {
                // nulls first
                assertEquals("Invalid sort result with mixed values (null vs. int)", "7", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "8", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "9", nameIterator.next().getProperty(TestOne.name));
                // other values after that
                assertEquals("Invalid sort result with mixed values (null vs. int)", "0", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "1", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "2", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "3", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "4", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "5", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "6", nameIterator.next().getProperty(TestOne.name));
            }
            // check that the sorting is "nulls first" as documented
            final Iterator<TestOne> intIterator = result.iterator();
            while (intIterator.hasNext()) {
                // nulls first
                assertEquals("Invalid sort result with mixed values (null vs. int)", null, intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", null, intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", null, intIterator.next().getProperty(TestOne.anInt));
                // other values after that
                assertEquals("Invalid sort result with mixed values (null vs. int)", 0L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 1L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 2L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 3L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 4L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 5L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 6L, (long) intIterator.next().getProperty(TestOne.anInt));
            }
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 40 with NodeInterface

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

the class SearchAndSortingTest method test03SeachByNamePropertyLooseLowercase.

@Test
public void test03SeachByNamePropertyLooseLowercase() {
    try {
        Class type = TestOne.class;
        int number = 4;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        final int offset = 10;
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        try (final Tx tx = app.tx()) {
            int i = offset;
            String name;
            for (NodeInterface node : nodes) {
                // System.out.println("Node ID: " + node.getNodeId());
                name = "TestOne-" + i;
                i++;
                node.setProperty(AbstractNode.name, name);
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).and(TestOne.name, "testone", false).getResult();
            assertEquals(4, result.size());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Random(java.util.Random) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) Result(org.structr.core.Result) Test(org.junit.Test)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12