Search in sources :

Example 6 with NodeInterface

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

the class RenderContextTest method testScriptEvaluation.

@Test
public void testScriptEvaluation() {
    Settings.CypherDebugLogging.setValue(true);
    try (final Tx tx = app.tx()) {
        // create a Project type
        final SchemaNode projectNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Project"));
        // create a Task type with a string property "task"
        final SchemaNode taskNode = createTestNode(SchemaNode.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task"), new NodeAttribute<>(new StringProperty("_task"), "String"));
        // create a schema relationship between them
        createTestNode(SchemaRelationshipNode.class, new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceNode"), projectNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetNode"), taskNode), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "relationshipType"), "has"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceMultiplicity"), "1"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetMultiplicity"), "*"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "sourceJsonName"), "project"), new NodeAttribute<>(StructrApp.key(SchemaRelationshipNode.class, "targetJsonName"), "tasks"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        // obtain class objects to create instances of the above types
        final Class projectType = StructrApp.getConfiguration().getNodeEntityClass("Project");
        final Class taskType = StructrApp.getConfiguration().getNodeEntityClass("Task");
        final PropertyKey taskKey = StructrApp.key(taskType, "task");
        final PropertyKey tasksKey = StructrApp.key(projectType, "tasks");
        final List<NodeInterface> tasks = new LinkedList<>();
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 1"), new NodeAttribute<>(taskKey, "Task 1")));
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 2"), new NodeAttribute<>(taskKey, "Task 2")));
        tasks.add(app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "Task 3"), new NodeAttribute<>(taskKey, "Task 3")));
        // create a project and a task
        final NodeInterface project = app.create(projectType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "project"), new NodeAttribute<>(tasksKey, tasks));
        // create an additional test task without a project
        final NodeInterface testTask = app.create(taskType, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "test task"), new NodeAttribute<>(taskKey, "test task"));
        final RenderContext renderContext = new RenderContext(securityContext);
        renderContext.putDataObject("project", project);
        renderContext.putDataObject("task", testTask);
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${foo.page}"));
        assertEquals("Invalid scripting evaluation result", testTask.getUuid(), Scripting.replaceVariables(renderContext, null, "${task}"));
        assertEquals("Invalid scripting evaluation result", "test task", Scripting.replaceVariables(renderContext, null, "${task.task}"));
        assertEquals("Invalid scripting evaluation result", tasks.toString(), Scripting.replaceVariables(renderContext, null, "${project.tasks}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(0).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[0]}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(1).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[1]}"));
        assertEquals("Invalid scripting evaluation result", tasks.get(2).getUuid(), Scripting.replaceVariables(renderContext, null, "${project.tasks[2]}"));
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3]}"));
        assertEquals("Invalid scripting evaluation result", "Task 1", Scripting.replaceVariables(renderContext, null, "${project.tasks[0].task}"));
        assertEquals("Invalid scripting evaluation result", "Task 2", Scripting.replaceVariables(renderContext, null, "${project.tasks[1].task}"));
        assertEquals("Invalid scripting evaluation result", "Task 3", Scripting.replaceVariables(renderContext, null, "${project.tasks[2].task}"));
        assertEquals("Invalid scripting evaluation result", "", Scripting.replaceVariables(renderContext, null, "${project.tasks[3].task}"));
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 7 with NodeInterface

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

the class StructrLDAPClientModuleTest method createTestRelationships.

protected <T extends Relation> List<T> createTestRelationships(final Class<T> relType, final int number) throws FrameworkException {
    List<GenericNode> nodes = createTestNodes(GenericNode.class, 2);
    final NodeInterface startNode = nodes.get(0);
    final NodeInterface endNode = nodes.get(1);
    try (final Tx tx = app.tx()) {
        List<T> rels = new LinkedList<>();
        for (int i = 0; i < number; i++) {
            rels.add((T) app.create(startNode, endNode, relType));
        }
        tx.success();
        return rels;
    }
}
Also used : Tx(org.structr.core.graph.Tx) GenericNode(org.structr.core.entity.GenericNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 8 with NodeInterface

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

the class StructrMessagingEngineModuleTest 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)

Example 9 with NodeInterface

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

the class ODFTest method testODS.

@Test
public void testODS() {
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("ODSExporter");
    File template = null;
    String uuid = null;
    assertNotNull("Type ODSExporter should exist", type);
    try (final Tx tx = app.tx()) {
        app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        // read test file from sources
        try (final InputStream is = ODFTest.class.getResourceAsStream("/test.odt")) {
            template = FileHelper.createFile(securityContext, is, "", File.class, "template");
        }
        assertNotNull("Test file must exist", template);
        final NodeInterface node = app.create(type, new NodeAttribute<>(StructrApp.key(ODFExporter.class, "name"), "test.ods"), new NodeAttribute<>(StructrApp.key(ODFExporter.class, "documentTemplate"), template));
        uuid = node.getUuid();
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
    }
    // use RestAssured to call exported methods on file
    RestAssured.given().filter(ResponseLoggingFilter.logResponseTo(System.out)).header("X-User", "admin").header("X-Password", "admin").expect().statusCode(200).when().post("/ODSExporter/" + uuid + "/createDocumentFromTemplate");
    // use RestAssured to call exported methods on file
    RestAssured.given().filter(ResponseLoggingFilter.logResponseTo(System.out)).header("X-User", "admin").header("X-Password", "admin").expect().statusCode(200).body("result_count", equalTo(2)).body("result", hasSize(2)).body("result[0].name", equalTo("template")).body("result[1].name", equalTo("test.ods_template")).when().get("/File");
    // use RestAssured to call exported methods on file
    RestAssured.given().filter(ResponseLoggingFilter.logResponseTo(System.out)).header("X-User", "admin").header("X-Password", "admin").expect().statusCode(200).body("result[0].type", equalTo("ODSExporter")).body("result[0].documentTemplate.type", equalTo("File")).body("result[0].documentTemplate.name", equalTo("template")).body("result[0].documentTemplate.path", equalTo("/template")).body("result[0].resultDocument.type", equalTo("File")).body("result[0].resultDocument.name", equalTo("test.ods_template")).body("result[0].resultDocument.path", equalTo("/test.ods_template")).when().get("/ODFExporter");
}
Also used : Tx(org.structr.core.graph.Tx) InputStream(java.io.InputStream) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 10 with NodeInterface

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

the class StructrODFModuleTest method cleanDatabase.

@After
@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) After(org.junit.After)

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