Search in sources :

Example 46 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class StructrTest method cleanDatabase.

@Before
public void cleanDatabase() {
    try (final Tx tx = app.tx()) {
        final List<? extends NodeInterface> nodes = app.nodeQuery().getAsList();
        logger.info("Cleaning database: {} nodes", nodes.size());
        for (final NodeInterface node : nodes) {
            app.delete(node);
        }
        // delete remaining nodes without UUIDs etc.
        app.cypher("MATCH (n)-[r]-(m) DELETE n, r, m", Collections.emptyMap());
        tx.success();
    } catch (FrameworkException fex) {
        logger.error("Exception while trying to clean database: {}", fex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) Before(org.junit.Before)

Example 47 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class ScriptingTest method testNonPrimitiveReturnValue.

@Test
public void testNonPrimitiveReturnValue() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.name, "testReturnValueOfGlobalSchemaMethod"), new NodeAttribute<>(SchemaMethod.source, "{ return { name: 'test', value: 123, me: Structr.me }; }"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"))), new NodeAttribute<>(SchemaProperty.name, "returnTest"), new NodeAttribute<>(SchemaProperty.propertyType, "Function"), new NodeAttribute<>(SchemaProperty.readFunction, "{ return { name: 'test', value: 123, me: Structr.this }; }"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final ActionContext ctx = new ActionContext(securityContext, null);
        final Map map = (Map) Scripting.evaluate(ctx, null, "${{return Structr.call('testReturnValueOfGlobalSchemaMethod')}}", "test");
        final Object name = map.get("name");
        final Object value = map.get("value");
        final Object me = map.get("me");
        assertEquals("Invalid non-primitive scripting return value result, name should be of type string.", "test", name);
        assertEquals("Invalid non-primitive scripting return value result, value should be of type integer", Integer.valueOf(123), value);
        assertTrue("Invalid non-primitive scripting return value result,   me should be of type SuperUser", me instanceof SuperUser);
        tx.success();
    } catch (UnlicensedException | FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
        final NodeInterface obj = app.create(type, "test");
        final Map map = (Map) obj.getProperty(StructrApp.key(type, "returnTest"));
        final Object name = map.get("name");
        final Object value = map.get("value");
        final Object me = map.get("me");
        assertEquals("Invalid non-primitive scripting return value result, name should be of type string.", "test", name);
        assertEquals("Invalid non-primitive scripting return value result, value should be of type integer", Integer.valueOf(123), value);
        assertEquals("Invalid non-primitive scripting return value result, me should be the entity", obj, me);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) ActionContext(org.structr.schema.action.ActionContext) Map(java.util.Map) PropertyMap(org.structr.core.property.PropertyMap) SuperUser(org.structr.core.entity.SuperUser) NodeInterface(org.structr.core.graph.NodeInterface) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 48 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class SchemaTest method testJavaSchemaMethod.

@Test
public void testJavaSchemaMethod() {
    final Class groupType = StructrApp.getConfiguration().getNodeEntityClass("Group");
    NodeInterface group = null;
    try (final Tx tx = app.tx()) {
        final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName("Group").getFirst();
        assertNotNull("Schema node Group should exist", schemaNode);
        final StringBuilder source = new StringBuilder();
        source.append("final Set<String> test = new LinkedHashSet<>();\n");
        source.append("\t\ttest.add(\"one\");\n");
        source.append("\t\ttest.add(\"two\");\n");
        source.append("\t\ttest.add(\"three\");\n");
        source.append("\t\treturn test;\n\n");
        app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.name, "testJavaMethod"), new NodeAttribute<>(SchemaMethod.source, source.toString()), new NodeAttribute<>(SchemaMethod.codeType, "java"));
        group = app.create(groupType, "test");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final Object result = Actions.execute(securityContext, null, "${first(find('Group')).testJavaMethod}", "test");
        assertTrue("Result should be of type Set", result instanceof Set);
        final Set<String> set = (Set) result;
        final String[] array = set.toArray(new String[0]);
        assertEquals("Invalid Java schema method result", "one", array[0]);
        assertEquals("Invalid Java schema method result", "two", array[1]);
        assertEquals("Invalid Java schema method result", "three", array[2]);
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) Set(java.util.Set) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 49 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class ValidationTest method testIntPropertyUniquenessValidation.

// ----- int property validation tests -----
@Test
public void testIntPropertyUniquenessValidation() {
    final String keyName = "unique";
    final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Integer!");
    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, 42)).getUuid();
            app.create(testType, new NodeAttribute<>(key, 42));
            tx.success();
            fail("Int property uniqueness constraint violated!");
        } catch (FrameworkException fex) {
            final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
            final ErrorToken token = tokens.get(0);
            assertEquals("Invalid int validation result", 1, tokens.size());
            assertEquals("Invalid int validation result", 422, fex.getStatus());
            assertEquals("Invalid int validation result", keyName, token.getProperty());
            assertEquals("Invalid int validation result", "Test", token.getType());
            assertEquals("Invalid int validation result", "already_taken", token.getToken());
            assertEquals("Invalid int 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)

Example 50 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class ValidationTest method testIntPropertyRangeValidation4.

@Test
public void testIntPropertyRangeValidation4() {
    final Class<NodeInterface> testType = createTypeWithProperty("Test", "range1", "+Integer(]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);
    checkRangeSuccess(testType, range1, 5);
    try {
        checkRangeError(testType, range1, 0);
    } 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)

Aggregations

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12