Search in sources :

Example 46 with PropertyKey

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

the class ValidationTest method testDoublePropertyUniquenessValidation.

// ----- double property validation tests -----
@Test
public void testDoublePropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Double!");
    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, 0.123)).getUuid();
            app.create(testType, new NodeAttribute<>(key, 0.123));
            tx.success();
            fail("Double property uniqueness constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid double validation result", 1, tokens.size());
            assertEquals("Invalid double validation result", 422, fex.getStatus());
            assertEquals("Invalid double validation result", keyName, token.getProperty());
            assertEquals("Invalid double validation result", "Test", token.getType());
            assertEquals("Invalid double validation result", "already_taken", token.getToken());
            assertEquals("Invalid double 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 47 with PropertyKey

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

the class ValidationTest method testEmptyStringPropertyValidationWithNulls.

@Test
public void testEmptyStringPropertyValidationWithNulls() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final Class testType = StructrApp.getConfiguration().getNodeEntityClass("Test");
    if (testType != null) {
        final PropertyKey key = StructrApp.key(testType, "testUnique");
        if (key != null) {
            String uuid = null;
            try (final Tx tx = app.tx()) {
                // for string properties, null equals the empty string
                uuid = app.create(testType, new NodeAttribute<>(key, null)).getUuid();
                app.create(testType, new NodeAttribute<>(key, null));
                tx.success();
            } catch (FrameworkException fex) {
                fail("Invalid uniqueness constraint validation for null values!");
            }
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 48 with PropertyKey

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

the class ValidationTest method testStringPropertyRegexMatch.

@Test
public void testStringPropertyRegexMatch() {
    final String keyName = "regex";
    final Class testType = createTypeWithProperty("Test", keyName, "String([a-zA-Z0-9]+)");
    final PropertyKey key = StructrApp.key(testType, keyName);
    if (key != null) {
        try (final Tx tx = app.tx()) {
            app.create(testType, new NodeAttribute<>(key, "abcdefg_"));
            tx.success();
            fail("Regex constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid uniqueness validation result", 1, tokens.size());
            assertEquals("Invalid regex validation result", 422, fex.getStatus());
            assertEquals("Invalid regex validation result", "regex", token.getProperty());
            assertEquals("Invalid regex validation result", "Test", token.getType());
            assertEquals("Invalid regex validation result", "must_match", token.getToken());
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) ErrorToken(org.structr.common.error.ErrorToken) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 49 with PropertyKey

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

the class ValidationTest method testDoublePropertyNotNullValidation.

@Test
public void testDoublePropertyNotNullValidation() {
    final String keyName = "notnull";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "+Double");
    final PropertyKey key = StructrApp.key(testType, keyName);
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            app.create(testType, new NodeAttribute<>(key, null));
            tx.success();
            fail("Double property not null constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid double validation result", 1, tokens.size());
            assertEquals("Invalid double validation result", 422, fex.getStatus());
            assertEquals("Invalid double validation result", keyName, token.getProperty());
            assertEquals("Invalid double validation result", "Test", token.getType());
            assertEquals("Invalid double validation result", "must_not_be_empty", token.getToken());
        }
    }
}
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 50 with PropertyKey

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

the class ValidationTest method testEnumPropertyNotNullValidation.

@Test
public void testEnumPropertyNotNullValidation() {
    final String keyName = "notnull";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "+Enum(one, two, three)");
    final PropertyKey key = StructrApp.key(testType, keyName);
    // test failure
    try (final Tx tx = app.tx()) {
        app.create(testType, new NodeAttribute<>(key, null));
        tx.success();
        fail("Enum property not null constraint violated!");
    } catch (FrameworkException fex) {
        final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
        final ErrorToken token = tokens.get(0);
        assertEquals("Invalid enum validation result", 1, tokens.size());
        assertEquals("Invalid enum validation result", 422, fex.getStatus());
        assertEquals("Invalid enum validation result", keyName, token.getProperty());
        assertEquals("Invalid enum validation result", "Test", token.getType());
        assertEquals("Invalid enum validation result", "must_not_be_empty", token.getToken());
    }
}
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)

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