Search in sources :

Example 31 with PropertyKey

use of org.structr.core.property.PropertyKey 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 32 with PropertyKey

use of org.structr.core.property.PropertyKey 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)

Example 33 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class SearchAndSortingTest method test03SearchRelationship.

@Test
public void test03SearchRelationship() {
    try {
        final NodeHasLocation rel = createTestRelationships(NodeHasLocation.class, 1).get(0);
        final PropertyKey key1 = new StringProperty("jghsdkhgshdhgsdjkfgh").indexed();
        final Class type = NodeHasLocation.class;
        final String val1 = "54354354546806849870";
        final Result<RelationshipInterface> result;
        try (final Tx tx = app.tx()) {
            rel.setProperty(key1, val1);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            assertTrue(rel.getProperty(key1).equals(val1));
            result = app.relationshipQuery(type).and(key1, val1).getResult();
            assertTrue(result.size() == 1);
            assertTrue(result.get(0).equals(rel));
        }
        final String val2 = "ölllldjöoa8w4rasf";
        try (final Tx tx = app.tx()) {
            rel.setProperty(key1, val2);
            tx.success();
        }
        assertTrue(result.size() == 1);
        assertTrue(result.get(0).equals(rel));
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) RelationshipInterface(org.structr.core.graph.RelationshipInterface) StringProperty(org.structr.core.property.StringProperty) NodeHasLocation(org.structr.core.entity.relationship.NodeHasLocation) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 34 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class SearchAndSortingTest method test07SearchByStaticMethod01.

@Test
public void test07SearchByStaticMethod01() {
    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 35 with PropertyKey

use of org.structr.core.property.PropertyKey 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)

Aggregations

PropertyKey (org.structr.core.property.PropertyKey)177 FrameworkException (org.structr.common.error.FrameworkException)108 Test (org.junit.Test)69 NodeInterface (org.structr.core.graph.NodeInterface)62 Tx (org.structr.core.graph.Tx)61 GraphObject (org.structr.core.GraphObject)59 StructrTest (org.structr.common.StructrTest)39 PropertyMap (org.structr.core.property.PropertyMap)37 List (java.util.List)31 Result (org.structr.core.Result)28 ConfigurationProvider (org.structr.schema.ConfigurationProvider)27 SecurityContext (org.structr.common.SecurityContext)26 LinkedList (java.util.LinkedList)22 StringProperty (org.structr.core.property.StringProperty)22 ErrorToken (org.structr.common.error.ErrorToken)20 Map (java.util.Map)18 PropertyConverter (org.structr.core.converter.PropertyConverter)18 NodeAttribute (org.structr.core.graph.NodeAttribute)17 App (org.structr.core.app.App)16 StructrApp (org.structr.core.app.StructrApp)16