Search in sources :

Example 11 with ErrorToken

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

the class ValidationTest method testDatePropertyUniquenessValidation.

// ----- date property validation tests -----
@Test
public void testDatePropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Date!");
    final PropertyKey key = StructrApp.key(testType, keyName);
    final Date date = new Date();
    String uuid = null;
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            uuid = app.create(testType, new NodeAttribute<>(key, date)).getUuid();
            app.create(testType, new NodeAttribute<>(key, date));
            tx.success();
            fail("Date property uniqueness 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", "already_taken", token.getToken());
            assertEquals("Invalid date 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) Date(java.util.Date) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 12 with ErrorToken

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

the class ValidationTest method testCompoundUniqueness.

@Test
public void testCompoundUniqueness() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "TestType"), new NodeAttribute<>(new StringProperty("_key1"), "String!!"), new NodeAttribute<>(new StringProperty("_key2"), "String!!"), new NodeAttribute<>(new StringProperty("_key3"), "String!!"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("TestType");
    final PropertyKey key1 = StructrApp.key(type, "key1");
    final PropertyKey key2 = StructrApp.key(type, "key2");
    final PropertyKey key3 = StructrApp.key(type, "key3");
    final PropertyKey[] keys = new PropertyKey[] { key1, key2, key3 };
    // test success
    try (final Tx tx = app.tx()) {
        app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "two"), new NodeAttribute<>(key3, "three"));
        app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "one"), new NodeAttribute<>(key3, "three"));
        tx.success();
    } catch (FrameworkException fex) {
        fail("Invalid compound indexing validation result.");
    }
    // test success
    try (final Tx tx = app.tx()) {
        app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key3, "three"));
        app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key3, "four"));
        tx.success();
    } catch (FrameworkException fex) {
        fail("Invalid compound indexing validation result.");
    }
    String uuid = null;
    // test failure
    try (final Tx tx = app.tx()) {
        uuid = app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "two"), new NodeAttribute<>(key3, "three")).getUuid();
        app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "two"), new NodeAttribute<>(key3, "three"));
        tx.success();
        fail("Invalid compound indexing validation result.");
    } catch (FrameworkException fex) {
        System.out.println(fex.toJSON());
        final ErrorToken token = fex.getErrorBuffer().getErrorTokens().get(0);
        assertEquals("Invalid validation status code", fex.getStatus(), 422);
        assertEquals("Invalid validation error token", "already_taken", token.getToken());
        assertEquals("Invalid validation error type", "TestType", token.getType());
        assertEquals("Invalid validation error type", uuid, token.getDetail());
        assertTrue("Invalid validation error type", Arrays.equals(keys, (PropertyKey[]) token.getValue()));
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) ErrorToken(org.structr.common.error.ErrorToken) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 13 with ErrorToken

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

the class ValidationTest method testArrayPropertyUniquenessValidation.

@Test
public void testArrayPropertyUniquenessValidation() {
    final String keyName = "stringArray";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "String[]!");
    final PropertyKey key = StructrApp.key(testType, keyName);
    String uuid1 = null;
    String uuid2 = null;
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            uuid1 = app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two" })).getUuid();
            uuid2 = app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two" })).getUuid();
            tx.success();
            fail("Array property validation failed!");
        } 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", uuid1, token.getDetail());
        }
        removeInstances(testType);
        // test success
        try (final Tx tx = app.tx()) {
            app.create(testType, new NodeAttribute<>(key, new String[] { "one" }));
            app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two" }));
            app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two", "three" }));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Array property validation error!");
        }
    }
}
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 14 with ErrorToken

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

the class ValidationTest method testUUIDValidation.

@Test
public void testUUIDValidation() {
    try (final Tx tx = app.tx()) {
        // test 31 characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "1234567890123456789012345678901"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        // test 33 characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "123456789012345678901234567890123"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        // test 40 characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "1234567890123456789012345678901234567890"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        // test wrong characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "123456789012345678g0123456789012"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        // test wrong characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "!bcdefabcdefabcdefabcdefabcdefab"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        // test wrong characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "sdfkgjh34t"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        // test all allowed characters
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "abcdef1234567890ABCDEF1234567890"));
        tx.success();
    } catch (FrameworkException fex) {
        fail("UUID validation failed for valid result.");
    }
    try (final Tx tx = app.tx()) {
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, "xy-"));
        tx.success();
        fail("UUID format 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 UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token.getToken());
    }
    try (final Tx tx = app.tx()) {
        app.create(TestOne.class, new NodeAttribute<>(GraphObject.id, ""));
        tx.success();
        fail("UUID not empty constraint violated!");
    } catch (FrameworkException fex) {
        final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
        final ErrorToken token1 = tokens.get(0);
        final ErrorToken token2 = tokens.get(1);
        assertEquals("Invalid uniqueness validation result", 2, tokens.size());
        assertEquals("Invalid UUID uniqueness validation result", 422, fex.getStatus());
        assertEquals("Invalid UUID uniqueness validation result", "id", token1.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token1.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_not_be_empty", token1.getToken());
        assertEquals("Invalid UUID uniqueness validation result", "id", token2.getProperty());
        assertEquals("Invalid UUID uniqueness validation result", "TestOne", token2.getType());
        assertEquals("Invalid UUID uniqueness validation result", "must_match", token2.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 15 with ErrorToken

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

the class ValidationTest method testEnumPropertyUniquenessValidation.

// ----- enum property validation tests -----
@Test
public void testEnumPropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Enum(one, two, three)!");
    final PropertyKey key = StructrApp.key(testType, keyName);
    String uuid = null;
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            final Object value = ((EnumProperty) key).getEnumType().getEnumConstants()[0];
            uuid = app.create(testType, new NodeAttribute<>(key, value)).getUuid();
            app.create(testType, new NodeAttribute<>(key, value));
            tx.success();
            fail("Enum property uniqueness 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", "already_taken", token.getToken());
            assertEquals("Invalid enum validation result", uuid, token.getDetail());
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) EnumProperty(org.structr.core.property.EnumProperty) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) 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