Search in sources :

Example 11 with Result

use of org.structr.core.Result in project structr by structr.

the class AccessControlTest method test05FrontendUserAccessToProtectedNode.

@Test
public void test05FrontendUserAccessToProtectedNode() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        List<Principal> users = createTestNodes(Principal.class, 2);
        Principal user1 = (Principal) users.get(0);
        Principal user2 = (Principal) users.get(1);
        PropertyMap props = new PropertyMap();
        props.put(AbstractNode.visibleToPublicUsers, true);
        // Create two nodes with user context, one of them is visible to public users
        Class type = TestOne.class;
        TestOne t1 = createTestNode(TestOne.class, props, user1);
        props = new PropertyMap();
        props.put(AbstractNode.visibleToAuthenticatedUsers, true);
        TestOne t2 = createTestNode(TestOne.class, props, user1);
        // Let another user search
        SecurityContext user2Context = SecurityContext.getInstance(user2, AccessMode.Frontend);
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(user2Context).nodeQuery(type).getResult();
            assertEquals(2, result.size());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) Result(org.structr.core.Result) Test(org.junit.Test)

Example 12 with Result

use of org.structr.core.Result in project structr by structr.

the class AccessControlTest method test07ResultCountWithPaging.

@Test
public void test07ResultCountWithPaging() {
    // remove auto-generated resource access objects
    clearResourceAccess();
    try {
        final Class type = TestOne.class;
        final List<NodeInterface> nodes = createTestNodes(type, 10);
        int count = 0;
        try (final Tx tx = app.tx()) {
            // add names to make sorting work...
            for (final NodeInterface node : nodes) {
                node.setProperty(AbstractNode.name, "node0" + count++);
            }
            nodes.get(3).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(5).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(7).setProperty(AbstractNode.visibleToPublicUsers, true);
            nodes.get(9).setProperty(AbstractNode.visibleToPublicUsers, true);
            tx.success();
        }
        SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
        PropertyKey sortKey = AbstractNode.name;
        boolean sortDesc = false;
        int pageSize = 2;
        int page = 1;
        try (final Tx tx = app.tx()) {
            Result result = StructrApp.getInstance(publicContext).nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            assertEquals(2, result.size());
            assertEquals(4, (int) result.getRawResultCount());
            assertEquals(nodes.get(3).getUuid(), result.get(0).getUuid());
            assertEquals(nodes.get(5).getUuid(), result.get(1).getUuid());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        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) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 13 with Result

use of org.structr.core.Result in project structr by structr.

the class BasicTest method test01DeleteNode.

/**
 * Test successful deletion of a node.
 *
 * The node shouldn't be found afterwards.
 * Creation and deletion are executed in two different transactions.
 */
@Test
public void test01DeleteNode() {
    try {
        final PropertyMap props = new PropertyMap();
        final String type = "GenericNode";
        final String name = "GenericNode-name";
        NodeInterface node = null;
        String uuid = null;
        props.put(AbstractNode.type, type);
        props.put(AbstractNode.name, name);
        try (final Tx tx = app.tx()) {
            node = app.create(GenericNode.class, props);
            tx.success();
        }
        assertTrue(node != null);
        try (final Tx tx = app.tx()) {
            uuid = node.getUuid();
        }
        try (final Tx tx = app.tx()) {
            app.delete(node);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery().uuid(uuid).getResult();
            assertEquals("Node should have been deleted", 0, result.size());
        } catch (FrameworkException fe) {
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GenericNode(org.structr.core.entity.GenericNode) NodeInterface(org.structr.core.graph.NodeInterface) Result(org.structr.core.Result) Test(org.junit.Test)

Example 14 with Result

use of org.structr.core.Result 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 15 with Result

use of org.structr.core.Result in project structr by structr.

the class SearchAndSortingTest method test08SearchByStaticMethodWithNullSearchValue01.

@Test
public void test08SearchByStaticMethodWithNullSearchValue01() {
    try {
        PropertyMap props = new PropertyMap();
        final PropertyKey key = AbstractNode.name;
        final String name = "abc";
        props.put(key, name);
        createTestNode(TestOne.class, props);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(TestOne.class).andName(null).includeDeletedAndHidden().getResult();
            assertTrue(result.isEmpty());
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Aggregations

Result (org.structr.core.Result)66 FrameworkException (org.structr.common.error.FrameworkException)45 Tx (org.structr.core.graph.Tx)36 Test (org.junit.Test)34 TestOne (org.structr.core.entity.TestOne)31 PropertyKey (org.structr.core.property.PropertyKey)28 PropertyMap (org.structr.core.property.PropertyMap)21 NodeInterface (org.structr.core.graph.NodeInterface)18 GraphObject (org.structr.core.GraphObject)14 AbstractNode (org.structr.core.entity.AbstractNode)12 RestMethodResult (org.structr.rest.RestMethodResult)12 Random (java.util.Random)11 SecurityContext (org.structr.common.SecurityContext)11 GraphObjectMap (org.structr.core.GraphObjectMap)10 LinkedList (java.util.LinkedList)9 Query (org.structr.core.app.Query)9 App (org.structr.core.app.App)8 StructrApp (org.structr.core.app.StructrApp)8 List (java.util.List)7 Principal (org.structr.core.entity.Principal)7