Search in sources :

Example 21 with TestOne

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

the class SearchAndSortingTest method test07SortByIntWithNullValues.

@Test
public void test07SortByIntWithNullValues() {
    try {
        final List<TestOne> nodes = this.createTestNodes(TestOne.class, 10);
        try (final Tx tx = app.tx()) {
            int i = 0;
            for (NodeInterface node : nodes) {
                node.setProperty(AbstractNode.name, Long.toString(i));
                if (i < 7) {
                    node.setProperty(TestOne.anInt, i);
                }
                i++;
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            boolean sortDesc = false;
            final List<TestOne> result = app.nodeQuery(TestOne.class).sort(TestOne.anInt).order(sortDesc).getAsList();
            // check that the sorting is stable, i.e. the position of nodes
            // with equal values (and null) is not modified by sorting
            final Iterator<TestOne> nameIterator = result.iterator();
            while (nameIterator.hasNext()) {
                // nulls first
                assertEquals("Invalid sort result with mixed values (null vs. int)", "7", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "8", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "9", nameIterator.next().getProperty(TestOne.name));
                // other values after that
                assertEquals("Invalid sort result with mixed values (null vs. int)", "0", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "1", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "2", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "3", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "4", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "5", nameIterator.next().getProperty(TestOne.name));
                assertEquals("Invalid sort result with mixed values (null vs. int)", "6", nameIterator.next().getProperty(TestOne.name));
            }
            // check that the sorting is "nulls first" as documented
            final Iterator<TestOne> intIterator = result.iterator();
            while (intIterator.hasNext()) {
                // nulls first
                assertEquals("Invalid sort result with mixed values (null vs. int)", null, intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", null, intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", null, intIterator.next().getProperty(TestOne.anInt));
                // other values after that
                assertEquals("Invalid sort result with mixed values (null vs. int)", 0L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 1L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 2L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 3L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 4L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 5L, (long) intIterator.next().getProperty(TestOne.anInt));
                assertEquals("Invalid sort result with mixed values (null vs. int)", 6L, (long) intIterator.next().getProperty(TestOne.anInt));
            }
            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) TestOne(org.structr.core.entity.TestOne) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 22 with TestOne

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

the class SearchAndSortingTest method test03SeachByNamePropertyLooseLowercase.

@Test
public void test03SeachByNamePropertyLooseLowercase() {
    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.name, "testone", 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)

Example 23 with TestOne

use of org.structr.core.entity.TestOne 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 24 with TestOne

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

the class SearchAndSortingTest method test09SearchBySourceAndTargetId.

@Test
public void test09SearchBySourceAndTargetId() {
    try {
        final TestOne test1 = createTestNode(TestOne.class);
        final List<TestSix> tests = createTestNodes(TestSix.class, 5);
        try (final Tx tx = app.tx()) {
            test1.setProperty(TestOne.manyToManyTestSixs, tests);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            final List<SixOneManyToMany> result1 = app.relationshipQuery(SixOneManyToMany.class).and(SixOneManyToMany.sourceId, tests.get(0).getUuid()).getAsList();
            assertEquals("Invalid sourceId query result", 1, result1.size());
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            final List<SixOneManyToMany> result1 = app.relationshipQuery(SixOneManyToMany.class).and(SixOneManyToMany.targetId, test1.getUuid()).getAsList();
            assertEquals("Invalid targetId query result", 5, result1.size());
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SixOneManyToMany(org.structr.core.entity.SixOneManyToMany) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Example 25 with TestOne

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

the class SearchAndSortingTest method test06InexactSearchWithHyphen.

@Test
public void test06InexactSearchWithHyphen() {
    final Map<String, Integer> testResults = new LinkedHashMap<>();
    testResults.put("ABCDE12345", 1);
    testResults.put("ABCDE#1234", 1);
    testResults.put("ABCDE+2345", 1);
    testResults.put("ABCDE-2345", 1);
    testResults.put("ABCDE!2345", 1);
    testResults.put("ABCDE(2345", 1);
    testResults.put("ABCDE)2345", 1);
    testResults.put("ABCDE:2345", 1);
    testResults.put("ABCDE^2345", 1);
    testResults.put("ABCDE[2345", 1);
    testResults.put("ABCDE]2345", 1);
    testResults.put("ABCDE\"2345", 1);
    testResults.put("ABCDE{2345", 1);
    testResults.put("ABCDE}2345", 1);
    testResults.put("ABCDE~2345", 1);
    testResults.put("ABCDE*2345", 1);
    testResults.put("ABCDE?2345", 1);
    testResults.put("ABCDE|2345", 1);
    testResults.put("ABCDE&2345", 1);
    testResults.put("ABCDE;2345", 1);
    testResults.put("ABCDE/2345", 1);
    try {
        try (final Tx tx = app.tx()) {
            for (final String source : testResults.keySet()) {
                app.create(TestOne.class, source);
            }
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            for (final String source : testResults.keySet()) {
                final String query = source.substring(0, 6);
                final int count = testResults.get(source);
                final List<TestOne> result = app.nodeQuery(TestOne.class).and(AbstractNode.name, query, false).getAsList();
                assertEquals("Unexpected query result for special char query " + query, count, result.size());
            }
            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) TestOne(org.structr.core.entity.TestOne) LinkedHashMap(java.util.LinkedHashMap) 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