Search in sources :

Example 1 with ErrorToken

use of org.structr.common.error.ErrorToken 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 2 with ErrorToken

use of org.structr.common.error.ErrorToken in project structr by structr.

the class ValidationTest method testDatePropertyNotNullValidation.

@Test
public void testDatePropertyNotNullValidation() {
    final String keyName = "notnull";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "+Date");
    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("Date property not null constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid date validation result", 1, tokens.size());
            assertEquals("Invalid date validation result", 422, fex.getStatus());
            assertEquals("Invalid date validation result", keyName, token.getProperty());
            assertEquals("Invalid date validation result", "Test", token.getType());
            assertEquals("Invalid date 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 3 with ErrorToken

use of org.structr.common.error.ErrorToken in project structr by structr.

the class ValidationTest method testIntPropertyNotNullValidation.

@Test
public void testIntPropertyNotNullValidation() {
    final String keyName = "notnull";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "+Integer");
    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("Int property not null 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", "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 4 with ErrorToken

use of org.structr.common.error.ErrorToken in project structr by structr.

the class ValidationTest method testEmptyStringPropertyValidationWithEmptyStrings.

// ----- string property validation tests -----
@Test
public void testEmptyStringPropertyValidationWithEmptyStrings() {
    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, "")).getUuid();
                app.create(testType, new NodeAttribute<>(key, ""));
                tx.success();
                fail("Uniqueness 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 uniqueness validation result", 422, fex.getStatus());
                assertEquals("Invalid uniqueness validation result", "testUnique", token.getProperty());
                assertEquals("Invalid uniqueness validation result", "Test", token.getType());
                assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
                assertEquals("Invalid uniqueness validation result", uuid, token.getDetail());
            }
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) 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 5 with ErrorToken

use of org.structr.common.error.ErrorToken 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)

Aggregations

ErrorToken (org.structr.common.error.ErrorToken)26 Test (org.junit.Test)25 FrameworkException (org.structr.common.error.FrameworkException)25 Tx (org.structr.core.graph.Tx)25 StructrTest (org.structr.common.StructrTest)24 List (java.util.List)21 PropertyKey (org.structr.core.property.PropertyKey)20 NodeInterface (org.structr.core.graph.NodeInterface)12 StringProperty (org.structr.core.property.StringProperty)6 Date (java.util.Date)1 Random (java.util.Random)1 GraphObject (org.structr.core.GraphObject)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 Principal (org.structr.core.entity.Principal)1 SchemaNode (org.structr.core.entity.SchemaNode)1 EnumProperty (org.structr.core.property.EnumProperty)1