Search in sources :

Example 36 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class SearchAndSortingTest method test06SortByDateWitNullValues.

@Test
public void test06SortByDateWitNullValues() {
    try {
        Class type = TestOne.class;
        int number = 20;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        final int offset = 10;
        Collections.shuffle(nodes, new Random(System.nanoTime()));
        int i = offset;
        String name;
        try (final Tx tx = app.tx()) {
            for (NodeInterface node : nodes) {
                name = Integer.toString(i);
                i++;
                node.setProperty(AbstractNode.name, "TestOne-" + name);
                if ((i % 2) != 0) {
                    node.setProperty(TestOne.aDate, new Date());
                    System.out.println("TestOne-" + name + ": indexed with date");
                } else {
                    node.setProperty(TestOne.aDate, null);
                    System.out.println("TestOne-" + name + ": null 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 = 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 < Math.min(result.size(), pageSize); j++) {
                String expectedName = "TestOne-" + (30 - (j + 1) * 2);
                String gotName = result.get(j).getProperty(AbstractNode.name);
                System.out.println(j + ": " + 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 37 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class SearchAndSortingTest method test02PagingAndCreate.

@Test
public void test02PagingAndCreate() {
    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 = 20;
        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;
            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()) {
            List<NodeInterface> result = app.get(type);
            assertTrue(result.size() == number);
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            int pageSize = 2;
            int page = 1;
            testPaging(type, pageSize, page, number, offset, includeDeletedAndHidden, publicOnly, sortKey, sortDesc);
            PropertyMap props = new PropertyMap();
            props.put(sortKey, "TestOne-09");
            this.createTestNode(type, props);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            int pageSize = 2;
            int page = 1;
            testPaging(type, pageSize, page + 1, number + 1, offset - 1, includeDeletedAndHidden, publicOnly, sortKey, sortDesc);
            System.out.println("paging test finished");
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) 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) Test(org.junit.Test)

Example 38 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class SearchAndSortingTest method test06PagingVisibility.

@Test
public void test06PagingVisibility() {
    Principal tester1 = null;
    Principal tester2 = null;
    try (final Tx tx = app.tx()) {
        // create non-admin user
        tester1 = app.create(Principal.class, "tester1");
        tester2 = app.create(Principal.class, "tester2");
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    try {
        final SecurityContext tester1Context = SecurityContext.getInstance(tester1, AccessMode.Backend);
        final SecurityContext tester2Context = SecurityContext.getInstance(tester2, AccessMode.Backend);
        final App tester1App = StructrApp.getInstance(tester1Context);
        final App tester2App = StructrApp.getInstance(tester2Context);
        final Class type = TestOne.class;
        final int number = 1000;
        final List<NodeInterface> allNodes = this.createTestNodes(type, number);
        final List<NodeInterface> tester1Nodes = new LinkedList<>();
        final List<NodeInterface> tester2Nodes = new LinkedList<>();
        final int offset = 0;
        try (final Tx tx = app.tx()) {
            int i = offset;
            for (NodeInterface node : allNodes) {
                // System.out.println("Node ID: " + node.getNodeId());
                String _name = "TestOne-" + StringUtils.leftPad(Integer.toString(i), 5, "0");
                final double rand = Math.random();
                if (rand < 0.3) {
                    node.setProperty(NodeInterface.owner, tester1);
                    tester1Nodes.add(node);
                } else if (rand < 0.6) {
                    node.setProperty(NodeInterface.owner, tester2);
                    tester2Nodes.add(node);
                }
                i++;
                node.setProperty(AbstractNode.name, _name);
            }
            tx.success();
        }
        final int tester1NodeCount = tester1Nodes.size();
        final int tester2NodeCount = tester2Nodes.size();
        try (final Tx tx = app.tx()) {
            final PropertyKey sortKey = AbstractNode.name;
            final boolean sortDesc = false;
            final int pageSize = 10;
            final int page = 22;
            final Result result = tester1App.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getResult();
            assertEquals("Invalid paging result count with non-superuser security context", tester1NodeCount, (int) result.getRawResultCount());
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            final PropertyKey sortKey = AbstractNode.name;
            final boolean sortDesc = false;
            final int pageSize = 10;
            final int page = 22;
            final Result result = tester2App.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getResult();
            assertEquals("Invalid paging result count with non-superuser security context", tester2NodeCount, (int) result.getRawResultCount());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) Result(org.structr.core.Result) TestOne(org.structr.core.entity.TestOne) Principal(org.structr.core.entity.Principal) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test)

Example 39 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class SearchAndSortingTest method test08InexactSearch.

@Test
public void test08InexactSearch() {
    try {
        final Date date = new Date();
        final TestOne test = createTestNode(TestOne.class, new NodeAttribute(TestOne.name, "TestOne"), new NodeAttribute(TestOne.aBoolean, true), new NodeAttribute(TestOne.aDate, date), new NodeAttribute(TestOne.aDouble, 1.234), new NodeAttribute(TestOne.aLong, 12345L), new NodeAttribute(TestOne.anEnum, TestOne.Status.One), new NodeAttribute(TestOne.anInt, 123));
        try (final Tx tx = app.tx()) {
            assertEquals("Invalid inexact search result for type String", test, app.nodeQuery(TestOne.class).and(TestOne.name, "TestOne", false).getFirst());
            assertEquals("Invalid inexact search result for type Boolean", test, app.nodeQuery(TestOne.class).and(TestOne.aBoolean, true, false).getFirst());
            assertEquals("Invalid inexact search result for type Date", test, app.nodeQuery(TestOne.class).and(TestOne.aDate, date, false).getFirst());
            assertEquals("Invalid inexact search result for type Double", test, app.nodeQuery(TestOne.class).and(TestOne.aDouble, 1.234, false).getFirst());
            assertEquals("Invalid inexact search result for type Long", test, app.nodeQuery(TestOne.class).and(TestOne.aLong, 12345L, false).getFirst());
            assertEquals("Invalid inexact search result for type String", test, app.nodeQuery(TestOne.class).and(TestOne.anEnum, TestOne.Status.One, false).getFirst());
            assertEquals("Invalid inexact search result for type Enum", test, app.nodeQuery(TestOne.class).and(TestOne.anInt, 123, false).getFirst());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Date(java.util.Date) Test(org.junit.Test)

Example 40 with TestOne

use of org.structr.core.entity.TestOne in project structr by structr.

the class SearchAndSortingTest method test03NegativeOffsetPaging.

@Test
public void test03NegativeOffsetPaging() {
    try {
        Class type = TestOne.class;
        int number = 8;
        final List<NodeInterface> nodes = this.createTestNodes(type, number);
        final int offset = 0;
        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()) {
            List<NodeInterface> result = app.get(type);
            assertEquals(number, result.size());
            PropertyKey sortKey = AbstractNode.name;
            boolean sortDesc = false;
            int pageSize = 2;
            int page = 1;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getAsList();
            assertEquals(2, result.size());
            assertEquals("TestOne-0", result.get(0).getProperty(AbstractNode.name));
            assertEquals("TestOne-1", result.get(1).getProperty(AbstractNode.name));
            page = -1;
            result = app.nodeQuery(type).sort(AbstractNode.name).pageSize(pageSize).page(page).getAsList();
            assertEquals(2, result.size());
            assertEquals("TestOne-6", result.get(0).getProperty(AbstractNode.name));
            assertEquals("TestOne-7", result.get(1).getProperty(AbstractNode.name));
            page = -2;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getAsList();
            assertEquals(2, result.size());
            assertEquals("TestOne-4", result.get(0).getProperty(AbstractNode.name));
            assertEquals("TestOne-5", result.get(1).getProperty(AbstractNode.name));
            page = -3;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getAsList();
            assertEquals(2, result.size());
            assertEquals("TestOne-2", result.get(0).getProperty(AbstractNode.name));
            assertEquals("TestOne-3", result.get(1).getProperty(AbstractNode.name));
            page = -4;
            result = app.nodeQuery(type).sort(sortKey).order(sortDesc).pageSize(pageSize).page(page).getAsList();
            assertEquals(2, result.size());
            assertEquals("TestOne-0", result.get(0).getProperty(AbstractNode.name));
            assertEquals("TestOne-1", result.get(1).getProperty(AbstractNode.name));
        }
    } 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) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)74 TestOne (org.structr.core.entity.TestOne)74 Tx (org.structr.core.graph.Tx)72 FrameworkException (org.structr.common.error.FrameworkException)70 StructrTest (org.structr.common.StructrTest)20 Result (org.structr.core.Result)15 Principal (org.structr.core.entity.Principal)15 TestSix (org.structr.core.entity.TestSix)15 NodeInterface (org.structr.core.graph.NodeInterface)15 Random (java.util.Random)12 ActionContext (org.structr.schema.action.ActionContext)12 LinkedList (java.util.LinkedList)10 TestFour (org.structr.core.entity.TestFour)9 PropertyMap (org.structr.core.property.PropertyMap)9 PropertyKey (org.structr.core.property.PropertyKey)8 Date (java.util.Date)7 App (org.structr.core.app.App)7 StructrApp (org.structr.core.app.StructrApp)7 NotFoundException (org.structr.api.NotFoundException)6 UnlicensedException (org.structr.common.error.UnlicensedException)6