Search in sources :

Example 41 with NodeInterface

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

the class SearchAndSortingTest method test01SeachByName.

@Test
public void test01SeachByName() {
    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<TestOne> result = app.nodeQuery(type).getResult();
            assertEquals(4, result.size());
            for (NodeInterface node : result.getResults()) {
                System.out.println(node);
            }
            result = app.nodeQuery(type).andName("TestOne-12").getResult();
            assertEquals(1, 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) Test(org.junit.Test)

Example 42 with NodeInterface

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

the class SearchAndSortingTest method test04SortByDateDesc.

@Test
public void test04SortByDateDesc() {
    try {
        Class type = TestOne.class;
        int number = 131;
        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) {
                name = Integer.toString(i);
                i++;
                node.setProperty(AbstractNode.name, name);
                // slow down execution speed to make sure distinct changes fall in different milliseconds
                try {
                    Thread.sleep(2);
                } catch (Throwable t) {
                }
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = AbstractNode.lastModifiedDate;
            boolean sortDesc = true;
            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 });
            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 = number + offset - 1 - j;
                String gotName = result.get(j).getProperty(AbstractNode.name);
                System.out.println(expectedNumber + ", got: " + gotName);
                assertEquals(Integer.toString(expectedNumber), gotName);
            }
            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) Result(org.structr.core.Result) Random(java.util.Random) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 43 with NodeInterface

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

the class SearchAndSortingTest method test04SearchByLocation.

@Test
public void test04SearchByLocation() {
    try {
        final PropertyMap props = new PropertyMap();
        final PropertyKey lat = TestSeven.latitude;
        final PropertyKey lon = TestSeven.longitude;
        final Class type = TestSeven.class;
        props.put(lat, 50.12284d);
        props.put(lon, 8.73923d);
        props.put(AbstractNode.name, "TestSeven-0");
        NodeInterface node = createTestNode(type, props);
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).location("Hanauer Landstraße", "200", "60314", "Frankfurt", "Germany", 10.0).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 : TestSeven(org.structr.core.entity.TestSeven) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) Result(org.structr.core.Result) Test(org.junit.Test)

Example 44 with NodeInterface

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

the class SearchAndSortingTest method test03SortByDate.

@Test
public void test03SortByDate() {
    try {
        Class type = TestOne.class;
        int number = 97;
        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) {
                name = Integer.toString(i);
                i++;
                node.setProperty(AbstractNode.name, "TestOne-" + name);
                node.setProperty(TestOne.aDate, new Date());
                // slow down execution speed to make sure distinct changes fall in different milliseconds
                try {
                    Thread.sleep(2);
                } catch (Throwable t) {
                }
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            Result result = app.nodeQuery(type).getResult();
            assertEquals(number, result.size());
            PropertyKey sortKey = TestOne.aDate;
            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 });
            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 < 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 : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Date(java.util.Date) Result(org.structr.core.Result) Random(java.util.Random) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 45 with NodeInterface

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

the class SearchAndSortingTest method test02SortByNameDesc.

@Test
public void test02SortByNameDesc() {
    try {
        Class type = TestOne.class;
        int number = 43;
        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) {
                name = Integer.toString(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 = true;
            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 });
            assertTrue(result.getRawResultCount() == number);
            logger.info("Result size: {}, expected: {}", new Object[] { result.size(), Math.min(number, pageSize) });
            assertTrue(result.size() == Math.min(number, pageSize));
            for (int j = 0; j < Math.min(result.size(), pageSize); j++) {
                int expectedNumber = number + offset - 1 - j;
                String gotName = result.get(j).getProperty(AbstractNode.name);
                System.out.println(expectedNumber + ", got: " + gotName);
                assertEquals(Integer.toString(expectedNumber), 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)

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