Search in sources :

Example 51 with PropertyKey

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

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

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

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

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

Aggregations

PropertyKey (org.structr.core.property.PropertyKey)177 FrameworkException (org.structr.common.error.FrameworkException)108 Test (org.junit.Test)69 NodeInterface (org.structr.core.graph.NodeInterface)62 Tx (org.structr.core.graph.Tx)61 GraphObject (org.structr.core.GraphObject)59 StructrTest (org.structr.common.StructrTest)39 PropertyMap (org.structr.core.property.PropertyMap)37 List (java.util.List)31 Result (org.structr.core.Result)28 ConfigurationProvider (org.structr.schema.ConfigurationProvider)27 SecurityContext (org.structr.common.SecurityContext)26 LinkedList (java.util.LinkedList)22 StringProperty (org.structr.core.property.StringProperty)22 ErrorToken (org.structr.common.error.ErrorToken)20 Map (java.util.Map)18 PropertyConverter (org.structr.core.converter.PropertyConverter)18 NodeAttribute (org.structr.core.graph.NodeAttribute)17 App (org.structr.core.app.App)16 StructrApp (org.structr.core.app.StructrApp)16