Search in sources :

Example 36 with PropertyKey

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

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

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

Example 39 with PropertyKey

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

the class ValidationTest method testIntPropertyUniquenessValidation.

// ----- int property validation tests -----
@Test
public void testIntPropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Integer!");
    final PropertyKey key = StructrApp.key(testType, keyName);
    String uuid = null;
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            uuid = app.create(testType, new NodeAttribute<>(key, 42)).getUuid();
            app.create(testType, new NodeAttribute<>(key, 42));
            tx.success();
            fail("Int property uniqueness constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid int validation result", 1, tokens.size());
            assertEquals("Invalid int validation result", 422, fex.getStatus());
            assertEquals("Invalid int validation result", keyName, token.getProperty());
            assertEquals("Invalid int validation result", "Test", token.getType());
            assertEquals("Invalid int validation result", "already_taken", token.getToken());
            assertEquals("Invalid int validation result", uuid, token.getDetail());
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) ErrorToken(org.structr.common.error.ErrorToken) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 40 with PropertyKey

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

the class ValidationTest method testIntPropertyRangeValidation4.

@Test
public void testIntPropertyRangeValidation4() {
    final Class<NodeInterface> testType = createTypeWithProperty("Test", "range1", "+Integer(]0,5])");
    final PropertyKey range1 = StructrApp.key(testType, "range1");
    checkRangeSuccess(testType, range1, 1);
    checkRangeSuccess(testType, range1, 2);
    checkRangeSuccess(testType, range1, 3);
    checkRangeSuccess(testType, range1, 4);
    checkRangeSuccess(testType, range1, 5);
    try {
        checkRangeError(testType, range1, 0);
    } catch (FrameworkException fex) {
        checkException(fex, 1, 422, "Test", "range1", "must_be_in_range");
    }
    try {
        checkRangeError(testType, range1, 6);
    } catch (FrameworkException fex) {
        checkException(fex, 1, 422, "Test", "range1", "must_be_in_range");
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) 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