Search in sources :

Example 21 with StringProperty

use of org.structr.core.property.StringProperty 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 22 with StringProperty

use of org.structr.core.property.StringProperty 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 23 with StringProperty

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

the class ValidationTest method testStringPropertyUniqueness.

@Test
public void testStringPropertyUniqueness() {
    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) {
            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) {
                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, "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", "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) 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 24 with StringProperty

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

the class ValidationTest method testConcurrentValidation.

@Test
public void testConcurrentValidation() {
    final int count = 100;
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_name"), "+String!"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Item");
    assertNotNull(type);
    final PropertyKey name = StructrApp.key(type, "name");
    assertNotNull(name);
    final Runnable tester = new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < count; i++) {
                // testing must be done in an isolated transaction
                try (final Tx tx = app.tx()) {
                    app.create(type, "Item" + i);
                    tx.success();
                } catch (FrameworkException ignore) {
                }
            }
        }
    };
    // submit three test instances
    final ExecutorService executor = Executors.newCachedThreadPool();
    final Future f1 = executor.submit(tester);
    final Future f2 = executor.submit(tester);
    final Future f3 = executor.submit(tester);
    try {
        f1.get();
        f2.get();
        f3.get();
    } catch (Throwable ex) {
    }
    List<GraphObject> result = null;
    try (final Tx tx = app.tx()) {
        result = app.nodeQuery(type).getAsList();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // verify that only count entities have been created.
    assertEquals("Invalid concurrent validation result", count, result.size());
    executor.shutdownNow();
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 25 with StringProperty

use of org.structr.core.property.StringProperty 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)

Aggregations

StringProperty (org.structr.core.property.StringProperty)53 FrameworkException (org.structr.common.error.FrameworkException)28 Tx (org.structr.core.graph.Tx)28 Test (org.junit.Test)26 PropertyKey (org.structr.core.property.PropertyKey)22 GraphObject (org.structr.core.GraphObject)14 GraphObjectMap (org.structr.core.GraphObjectMap)14 StructrTest (org.structr.common.StructrTest)11 LinkedList (java.util.LinkedList)10 NodeAttribute (org.structr.core.graph.NodeAttribute)10 SchemaNode (org.structr.core.entity.SchemaNode)9 List (java.util.List)8 AbstractNode (org.structr.core.entity.AbstractNode)7 NodeInterface (org.structr.core.graph.NodeInterface)7 StructrUiTest (org.structr.web.StructrUiTest)7 Map (java.util.Map)6 ErrorToken (org.structr.common.error.ErrorToken)6 IntProperty (org.structr.core.property.IntProperty)6 LinkedHashSet (java.util.LinkedHashSet)5 App (org.structr.core.app.App)5