Search in sources :

Example 56 with PropertyKey

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

the class ValidationTest method testLongPropertyRangeValidation5.

@Test
public void testLongPropertyRangeValidation5() {
    final Class<NodeInterface> testType = createTypeWithProperty("Test", "range1", "+Long(]0,5[)");
    final PropertyKey range1 = StructrApp.key(testType, "range1");
    checkRangeSuccess(testType, range1, 1);
    checkRangeSuccess(testType, range1, 2);
    checkRangeSuccess(testType, range1, 3);
    checkRangeSuccess(testType, range1, 4);
    try {
        checkRangeError(testType, range1, 0);
    } catch (FrameworkException fex) {
        checkException(fex, 1, 422, "Test", "range1", "must_be_in_range");
    }
    try {
        checkRangeError(testType, range1, 5);
    } catch (FrameworkException fex) {
        checkException(fex, 1, 422, "Test", "range1", "must_be_in_range");
    }
    try {
        checkRangeError(testType, range1, 6);
    } catch (FrameworkException fex) {
        checkException(fex, 1, 422, "Test", "range1", "must_be_in_range");
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 57 with PropertyKey

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

the class ValidationTest method testConcurrentValidationOnDynamicProperty.

@Test
public void testConcurrentValidationOnDynamicProperty() {
    final int count = 100;
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testXYZ"), "+String!"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Item");
    assertNotNull(type);
    final PropertyKey testXYZ = StructrApp.key(type, "testXYZ");
    assertNotNull(testXYZ);
    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, new NodeAttribute(testXYZ, "Item" + i));
                    tx.success();
                } catch (FrameworkException fex) {
                }
            }
        }
    };
    // 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 58 with PropertyKey

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

Example 59 with PropertyKey

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

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

the class ValidationTest method testBooleanPropertyNotNullValidation.

// ----- boolean property validation tests -----
@Test
public void testBooleanPropertyNotNullValidation() {
    final String keyName = "notNull";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "+Boolean");
    final PropertyKey key = StructrApp.key(testType, keyName);
    if (key != null) {
        // test failure
        try (final Tx tx = app.tx()) {
            /* Boolean properties are special. A boolean property will
				 * _always_ have a value, i.e. "null" equals "false" and
				 * will be returned as false.
				 *
				 * => a boolean property value can never be null!
				 */
            app.create(testType, new NodeAttribute<>(key, null));
            app.create(testType, new NodeAttribute<>(key, true));
            app.create(testType, new NodeAttribute<>(key, false));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected array property validation exception.");
        }
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) 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