use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class RenderContextTest method testVariableReplacementInDynamicTypes.
@Test
public void testVariableReplacementInDynamicTypes() {
SchemaNode itemNode = null;
NodeInterface parent = null;
NodeInterface child1 = null;
NodeInterface child2 = null;
try (final Tx tx = app.tx()) {
itemNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"));
final PropertyMap properties = new PropertyMap();
properties.put(SchemaRelationshipNode.sourceId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.targetId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.relationshipType, "CHILD");
properties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
properties.put(SchemaRelationshipNode.targetMultiplicity, "*");
properties.put(SchemaRelationshipNode.sourceJsonName, "parentItem");
properties.put(SchemaRelationshipNode.targetJsonName, "children");
app.create(SchemaRelationshipNode.class, properties);
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
final PropertyKey childrenProperty = StructrApp.key(itemClass, "children");
// create parent/child relationship
try (final Tx tx = app.tx()) {
parent = app.create(itemClass);
child1 = app.create(itemClass);
child2 = app.create(itemClass);
final List<NodeInterface> children = new LinkedList<>();
children.add(child1);
children.add(child2);
parent.setProperties(parent.getSecurityContext(), new PropertyMap(childrenProperty, children));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// verify that parent has two children
try (final Tx tx = app.tx()) {
// verify that parentItem can be accessed....
final Object value = parent.getProperty(childrenProperty);
assertTrue(value instanceof Collection);
final Collection coll = (Collection) value;
assertEquals(2, coll.size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
assertEquals(parent.toString(), Scripting.replaceVariables(new ActionContext(securityContext), child1, "${this.parentItem}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class RenderContextTest method testFunctionEvaluationInDynamicTypes.
@Test
public void testFunctionEvaluationInDynamicTypes() {
NodeInterface item = null;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testMethodCalled"), "Boolean"), new NodeAttribute(new StringProperty("___testMethod"), "set(this, 'testMethodCalled', true)"));
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
// create parent/child relationship
try (final Tx tx = app.tx()) {
item = app.create(itemClass, new NodeAttribute(SchemaNode.name, "Item"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
final RenderContext renderContext = new RenderContext(securityContext);
renderContext.putDataObject("item", item);
assertEquals("Invalid combined array dot syntax result: ", "Item", Scripting.replaceVariables(renderContext, item, "${find('Item')[0].name}"));
Scripting.replaceVariables(renderContext, item, "${item.testMethod()}");
assertEquals("Invalid method evaluation result: ", "true", Scripting.replaceVariables(renderContext, item, "${item.testMethodCalled}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.core.graph.NodeAttribute 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");
}
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class SimpleTest method test001EMailAddressConstraint.
@Test
public void test001EMailAddressConstraint() {
final PropertyKey<String> eMail = StructrApp.key(User.class, "eMail");
try (final Tx tx = app.tx()) {
app.create(User.class, new NodeAttribute(User.name, "TestUser1"), new NodeAttribute(eMail, "user@structr.test"));
app.create(User.class, new NodeAttribute(User.name, "TestUser2"), new NodeAttribute(eMail, "user@structr.test"));
tx.success();
fail("Expected exception to be thrown.");
} catch (FrameworkException fex) {
assertEquals("Invalid error code", 422, fex.getStatus());
}
check();
try (final Tx tx = app.tx()) {
app.create(User.class, new NodeAttribute(User.name, "TestUser1"), new NodeAttribute(eMail, "user@structr.test"));
app.create(User.class, new NodeAttribute(User.name, "TestUser2"), new NodeAttribute(eMail, "User@Structr.test"));
tx.success();
fail("Expected exception to be thrown.");
} catch (FrameworkException fex) {
assertEquals("Invalid error code", 422, fex.getStatus());
}
check();
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class UiScriptingTest method testMultiRequestParameters.
@Test
public void testMultiRequestParameters() {
try (final Tx tx = app.tx()) {
Page page = (Page) app.create(Page.class, new NodeAttribute(Page.name, "test"), new NodeAttribute(Page.visibleToPublicUsers, true));
Template template = (Template) app.create(Template.class, new NodeAttribute(Page.visibleToPublicUsers, true));
template.setContent("${each(request.param, print(data))}");
page.appendChild(template);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body(equalTo("abc")).when().get("http://localhost:8875/test?param=a¶m=b¶m=c");
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception");
}
}
Aggregations