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);
}
}
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.");
}
}
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");
}
}
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());
}
}
}
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");
}
}
Aggregations