Search in sources :

Example 16 with ErrorToken

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

the class ValidationTest method testStringPropertyNotNull.

@Test
public void testStringPropertyNotNull() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_nonempty"), "+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, "nonempty");
        if (key != null) {
            try (final Tx tx = app.tx()) {
                app.create(testType, new NodeAttribute<>(key, null));
                tx.success();
                fail("Not empty 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", "nonempty", token.getProperty());
                assertEquals("Invalid uniqueness validation result", "Test", token.getType());
                assertEquals("Invalid uniqueness validation result", "must_not_be_empty", token.getToken());
            }
        }
    }
}
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 17 with ErrorToken

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

the class ValidationTest method testSchemaRelationshipNodeValidation.

// schema relationship node validation
@Test
public void testSchemaRelationshipNodeValidation() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, null), new NodeAttribute<>(SchemaRelationshipNode.targetNode, null));
        tx.success();
        fail("SchemaRelationshipNode constraint violation, source and target node must not be null.");
    } catch (FrameworkException fex) {
        final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
        assertEquals("Invalid SchemaRelationshipNode validation result", 3, tokens.size());
        assertEquals("Invalid SchemaRelationshipNode validation result", 422, fex.getStatus());
        final ErrorToken token1 = tokens.get(0);
        final ErrorToken token2 = tokens.get(1);
        final ErrorToken token3 = tokens.get(2);
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token1.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "relationshipType", token1.getProperty());
        assertEquals("Invalid SchemaRelationshipNode validation result", "must_not_be_empty", token1.getToken());
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token2.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "sourceNode", token2.getProperty());
        assertEquals("Invalid SchemaRelationshipNode validation result", "must_not_be_empty", token2.getToken());
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token3.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "targetNode", token3.getProperty());
        assertEquals("Invalid SchemaRelationshipNode validation result", "must_not_be_empty", token3.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "test"), new NodeAttribute<>(SchemaRelationshipNode.sourceNode, null), new NodeAttribute<>(SchemaRelationshipNode.targetNode, null));
        tx.success();
        fail("SchemaRelationshipNode constraint violation, source and target node must not be null.");
    } catch (FrameworkException fex) {
        final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
        assertEquals("Invalid SchemaRelationshipNode validation result", 2, tokens.size());
        assertEquals("Invalid SchemaRelationshipNode validation result", 422, fex.getStatus());
        final ErrorToken token1 = tokens.get(0);
        final ErrorToken token2 = tokens.get(1);
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token1.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "sourceNode", token1.getProperty());
        assertEquals("Invalid SchemaRelationshipNode validation result", "must_not_be_empty", token1.getToken());
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token2.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "targetNode", token2.getProperty());
        assertEquals("Invalid SchemaRelationshipNode validation result", "must_not_be_empty", token2.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "test"), new NodeAttribute<>(SchemaRelationshipNode.sourceNode, app.nodeQuery(SchemaNode.class).andName("Group").getFirst()), new NodeAttribute<>(SchemaRelationshipNode.targetNode, null));
        tx.success();
        fail("SchemaRelationshipNode constraint violation, target node must not be null.");
    } catch (FrameworkException fex) {
        final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
        final ErrorToken token = tokens.get(0);
        assertEquals("Invalid SchemaRelationshipNode validation result", 1, tokens.size());
        assertEquals("Invalid SchemaRelationshipNode validation result", 422, fex.getStatus());
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "targetNode", token.getProperty());
        assertEquals("Invalid SchemaRelationshipNode validation result", "must_not_be_empty", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "test"), new NodeAttribute<>(SchemaRelationshipNode.sourceNode, null), new NodeAttribute<>(SchemaRelationshipNode.targetNode, app.nodeQuery(SchemaNode.class).andName("Group").getFirst()));
        tx.success();
        fail("SchemaRelationshipNode constraint violation, source node must not be null.");
    } catch (FrameworkException fex) {
        final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
        final ErrorToken token = tokens.get(0);
        assertEquals("Invalid SchemaRelationshipNode validation result", 1, tokens.size());
        assertEquals("Invalid SchemaRelationshipNode validation result", 422, fex.getStatus());
        assertEquals("Invalid SchemaRelationshipNode validation result", "SchemaRelationshipNode", token.getType());
        assertEquals("Invalid SchemaRelationshipNode validation result", "sourceNode", token.getProperty());
        assertEquals("Invalid SchemaRelationshipNode 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) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 18 with ErrorToken

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

the class ValidationTest method testArrayPropertyNotNullValidation.

// ----- array property validation tests -----
@Test
public void testArrayPropertyNotNullValidation() {
    final String keyName = "stringArray";
    final Class testType = createTypeWithProperty("Test", keyName, "+String[]");
    final PropertyKey key = StructrApp.key(testType, keyName);
    if (key != null) {
        try (final Tx tx = app.tx()) {
            app.create(testType, new NodeAttribute<>(key, null));
            tx.success();
            fail("Array property validation failed!");
        } catch (FrameworkException fex) {
            final ErrorToken token = fex.getErrorBuffer().getErrorTokens().get(0);
            assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
            assertEquals("Invalid uniqueness validation result", keyName, token.getProperty());
            assertEquals("Invalid uniqueness validation result", "Test", token.getType());
            assertEquals("Invalid uniqueness validation result", "must_not_be_empty", token.getToken());
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ErrorToken(org.structr.common.error.ErrorToken) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 19 with ErrorToken

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

the class AccessControlTest method test00CreatePrincipal.

@Test
public void test00CreatePrincipal() {
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Principal");
    final PropertyKey<String> eMail = StructrApp.key(type, "eMail");
    Principal user1 = null;
    try (final Tx tx = app.tx()) {
        List<Principal> users = createTestNodes(type, 1);
        user1 = (Principal) users.get(0);
        user1.setProperty(AbstractNode.name, "user1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
    }
    try (final Tx tx = app.tx()) {
        List<Principal> users = createTestNodes(type, 1);
        final Principal invalidUser = (Principal) users.get(0);
        invalidUser.setProperty(Principal.name, "tester");
        invalidUser.setProperty(eMail, "invalid");
        tx.success();
        fail("Invalid e-mail address should have thrown an exception.");
    } catch (FrameworkException ex) {
        final ErrorToken token = ex.getErrorBuffer().getErrorTokens().get(0);
        assertEquals("Invalid error code", 422, ex.getStatus());
        assertEquals("Invalid error code", "Principal", token.getType());
        assertEquals("Invalid error code", "eMail", token.getProperty());
        assertEquals("Invalid error code", "must_contain_at_character", token.getToken());
        assertEquals("Invalid error code", "invalid", token.getDetail());
    }
    // Switch user context to user1
    final App user1App = StructrApp.getInstance(SecurityContext.getInstance(user1, AccessMode.Frontend));
    try (final Tx tx = user1App.tx()) {
        final Principal user2 = user1App.create(Principal.class);
        assertNotNull(user2);
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception: " + ex.toString());
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ErrorToken(org.structr.common.error.ErrorToken) Principal(org.structr.core.entity.Principal) Test(org.junit.Test)

Example 20 with ErrorToken

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

the class ValidationTest method testBooleanPropertyUniquenessValidation.

@Test
public void testBooleanPropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Boolean!");
    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, true)).getUuid();
            app.create(testType, new NodeAttribute<>(key, false));
            app.create(testType, new NodeAttribute<>(key, true));
            tx.success();
            fail("Array property 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", keyName, 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) 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