Search in sources :

Example 21 with ErrorToken

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

the class ValidationTest method testLongPropertyNotNullValidation.

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

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

the class ValidationTest method testInheritedStringPropertyUniqueness.

@Test
public void testInheritedStringPropertyUniqueness() {
    try (final Tx tx = app.tx()) {
        final SchemaNode testType = app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "TestDerived"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.Test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final Class testType = StructrApp.getConfiguration().getNodeEntityClass("TestDerived");
    if (testType != null) {
        final PropertyKey key = StructrApp.key(testType, "testUnique");
        if (key != null) {
            Settings.CypherDebugLogging.setValue(true);
            try (final Tx tx = app.tx()) {
                // key must be unique, but can empty
                app.create(testType, new NodeAttribute<>(key, "unique"));
                app.create(testType, new NodeAttribute<>(key, ""));
                tx.success();
            } catch (FrameworkException fex) {
                fex.printStackTrace();
                fail("Unexpected exception.");
            }
            Settings.CypherDebugLogging.setValue(true);
            for (int i = 0; i < 5; i++) {
                try (final Tx tx = app.tx()) {
                    app.create(testType, new NodeAttribute<>(key, "unique"));
                    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", "TestDerived", token.getType());
                    assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
                }
            }
        }
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) 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 23 with ErrorToken

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

the class ValidationTest method testLongPropertyUniquenessValidation.

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

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

the class ValidationTest method testSchemaNodeNameValidation.

@Test
public void testSchemaNodeNameValidation() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, "lowercase");
        tx.success();
        fail("SchemaNode name constraint violation!");
    } 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 constraint violation error token", 422, fex.getStatus());
        assertEquals("Invalid constraint violation error token", "name", token.getProperty());
        assertEquals("Invalid constraint violation error token", "SchemaNode", token.getType());
        assertEquals("Invalid constraint violation error token", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, "7NumberAsFirstChar");
        tx.success();
        fail("SchemaNode name constraint violation!");
    } 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 constraint violation error token", 422, fex.getStatus());
        assertEquals("Invalid constraint violation error token", "name", token.getProperty());
        assertEquals("Invalid constraint violation error token", "SchemaNode", token.getType());
        assertEquals("Invalid constraint violation error token", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, "7Number");
        tx.success();
        fail("SchemaNode name constraint violation!");
    } 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 constraint violation error token", 422, fex.getStatus());
        assertEquals("Invalid constraint violation error token", "name", token.getProperty());
        assertEquals("Invalid constraint violation error token", "SchemaNode", token.getType());
        assertEquals("Invalid constraint violation error token", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, "7Number");
        tx.success();
        fail("SchemaNode name constraint violation!");
    } 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 constraint violation error token", 422, fex.getStatus());
        assertEquals("Invalid constraint violation error token", "name", token.getProperty());
        assertEquals("Invalid constraint violation error token", "SchemaNode", token.getType());
        assertEquals("Invalid constraint violation error token", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, "Valid");
        app.create(SchemaNode.class, "Valid");
        tx.success();
        fail("SchemaNode uniqueness constraint violation!");
    } 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 constraint violation error token", 422, fex.getStatus());
        assertEquals("Invalid constraint violation error token", "name", token.getProperty());
        assertEquals("Invalid constraint violation error token", "SchemaNode", token.getType());
        assertEquals("Invalid constraint violation error token", "already_taken", 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) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 25 with ErrorToken

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

the class ValidationTest method testStringPropertyUniquenessAgain.

@Test
public void testStringPropertyUniquenessAgain() {
    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) {
            final Random random = new Random();
            try (final Tx tx = app.tx()) {
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique06"), new NodeAttribute<>(key, "unique00"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique05"), new NodeAttribute<>(key, "unique01"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique04"), new NodeAttribute<>(key, "unique02"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique03"), new NodeAttribute<>(key, "unique03"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique02"), new NodeAttribute<>(key, "unique04"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique01"), new NodeAttribute<>(key, "unique05"));
                app.create(testType, new NodeAttribute<>(AbstractNode.name, "unique00"), new NodeAttribute<>(key, "unique06"));
                tx.success();
            } catch (FrameworkException fex) {
                logger.warn("", fex);
                fail("Unexpected exception.");
            }
            for (int i = 0; i < 5; i++) {
                try (final Tx tx = app.tx()) {
                    app.create(testType, new NodeAttribute<>(key, "unique0" + random.nextInt(7)));
                    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());
                }
            }
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Random(java.util.Random) 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)

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