Search in sources :

Example 46 with Result

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

the class SearchAndSortingTest method test05SortByInt.

@Test
public void test05SortByInt() {
    try {
        Class type = TestOne.class;
        int number = 61;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        final PropertyKey key = TestOne.anInt;
        final int offset = 10;
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        try (final Tx tx = app.tx()) {
            int i = offset;
            for (NodeInterface node : nodes) {
                node.setProperty(AbstractNode.name, Integer.toString(i));
                node.setProperty(key, i);
                i++;
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = key;
            boolean sortDesc = false;
            int pageSize = 5;
            int page = 1;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            logger.info("Raw result size: {}, expected: {}", new Object[] { result.getRawResultCount(), number });
            assertTrue(result.getRawResultCount() == number);
            logger.info("Result size: {}, expected: {}", new Object[] { result.size(), pageSize });
            assertTrue(result.size() == Math.min(number, pageSize));
            for (int j = 0; j < pageSize; j++) {
                int expectedNumber = offset + j;
                int gotNumber = (Integer) result.get(j).getProperty(key);
                System.out.println("expected: " + expectedNumber + ", got: " + gotNumber);
                assertEquals(expectedNumber, gotNumber);
            }
            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) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 47 with Result

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

the class SearchAndSortingTest method test02SearchSingleNodeByDate.

@Test
public void test02SearchSingleNodeByDate() {
    try {
        PropertyMap props = new PropertyMap();
        PropertyKey key = TestOne.aDate;
        Date date = new Date();
        Class type = TestOne.class;
        props.put(key, date);
        NodeInterface node = createTestNode(type, props);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).and(key, date).includeDeletedAndHidden().getResult();
            assertEquals(1, result.size());
            assertTrue(result.get(0).equals(node));
        }
    } 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) Date(java.util.Date) NodeInterface(org.structr.core.graph.NodeInterface) Result(org.structr.core.Result) Test(org.junit.Test)

Example 48 with Result

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

the class SearchAndSortingTest method test01SortByName.

@Test
public void test01SortByName() {
    try {
        Class type = TestOne.class;
        // no more than 89 to avoid sort order TestOne-10, TestOne-100 ...
        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).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            int pageSize = 10;
            int page = 1;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
            logger.info("Raw result size: {}, expected: {}", new Object[] { result.getRawResultCount(), number });
            assertEquals(number, (int) result.getRawResultCount());
            logger.info("Result size: {}, expected: {}", new Object[] { result.size(), Math.min(number, pageSize) });
            assertEquals(Math.min(number, pageSize), result.size());
            for (int j = 0; j < Math.min(result.size(), pageSize); j++) {
                String expectedName = "TestOne-" + (offset + j);
                String gotName = result.get(j).getProperty(AbstractNode.name);
                System.out.println(expectedName + ", got: " + gotName);
                assertEquals(expectedName, gotName);
            }
            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) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 49 with Result

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

the class SearchAndSortingTest method test08SearchByStaticMethod02.

@Test
public void test08SearchByStaticMethod02() {
    try {
        PropertyMap props = new PropertyMap();
        final PropertyKey key = AbstractNode.name;
        final String name = "89w3hkl sdfghsdkljth";
        props.put(key, name);
        final AbstractNode node = createTestNode(TestOne.class, props);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(TestOne.class).andName(name).includeDeletedAndHidden().getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(node));
        }
    } 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) AbstractNode(org.structr.core.entity.AbstractNode) TestOne(org.structr.core.entity.TestOne) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result) Test(org.junit.Test)

Example 50 with Result

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

the class SearchAndSortingTest method test05SeachByDefaultValue.

@Test
public void test05SeachByDefaultValue() {
    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.stringWithDefault, "default value", 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

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