use of org.structr.core.entity.SchemaNode in project structr by structr.
the class ScriptingTest method testSetPropertyWithDynamicNodes.
@Test
public void testSetPropertyWithDynamicNodes() {
/**
* This test creates two connected SchemaNodes and tests the script-based
* association of one instance with several others in the onCreate method.
*/
final long currentTimeMillis = System.currentTimeMillis();
Class sourceType = null;
Class targetType = null;
PropertyKey targetsProperty = null;
EnumProperty testEnumProperty = null;
PropertyKey testBooleanProperty = null;
PropertyKey testIntegerProperty = null;
PropertyKey testStringProperty = null;
PropertyKey testDoubleProperty = null;
PropertyKey testDateProperty = null;
Class testEnumType = null;
// setup phase: create schema nodes
try (final Tx tx = app.tx()) {
// create two nodes and associate them with each other
final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
final SchemaNode targetNode = createTestNode(SchemaNode.class, "Target");
final List<SchemaProperty> properties = new LinkedList<>();
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testBoolean"), new NodeAttribute(SchemaProperty.propertyType, "Boolean")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testInteger"), new NodeAttribute(SchemaProperty.propertyType, "Integer")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testString"), new NodeAttribute(SchemaProperty.propertyType, "String")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDouble"), new NodeAttribute(SchemaProperty.propertyType, "Double")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testEnum"), new NodeAttribute(SchemaProperty.propertyType, "Enum"), new NodeAttribute(SchemaProperty.format, "OPEN, CLOSED, TEST")));
properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDate"), new NodeAttribute(SchemaProperty.propertyType, "Date")));
sourceNode.setProperty(SchemaNode.schemaProperties, properties);
final List<SchemaMethod> methods = new LinkedList<>();
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "onCreate"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.targets = Structr.find('Target'); }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'OPEN'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest02"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'CLOSED'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest03"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'TEST'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest04"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'INVALID'; }")));
methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest05"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testBoolean = true; e.testInteger = 123; e.testString = 'testing..'; e.testDouble = 1.2345; e.testDate = new Date(" + currentTimeMillis + "); }")));
sourceNode.setProperty(SchemaNode.schemaMethods, methods);
final PropertyMap propertyMap = new PropertyMap();
propertyMap.put(SchemaRelationshipNode.sourceId, sourceNode.getUuid());
propertyMap.put(SchemaRelationshipNode.targetId, targetNode.getUuid());
propertyMap.put(SchemaRelationshipNode.sourceJsonName, "source");
propertyMap.put(SchemaRelationshipNode.targetJsonName, "targets");
propertyMap.put(SchemaRelationshipNode.sourceMultiplicity, "*");
propertyMap.put(SchemaRelationshipNode.targetMultiplicity, "*");
propertyMap.put(SchemaRelationshipNode.relationshipType, "HAS");
app.create(SchemaRelationshipNode.class, propertyMap);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
sourceType = config.getNodeEntityClass("Source");
targetType = config.getNodeEntityClass("Target");
targetsProperty = StructrApp.key(sourceType, "targets");
// we need to cast to EnumProperty in order to obtain the dynamic enum type
testEnumProperty = (EnumProperty) StructrApp.key(sourceType, "testEnum");
testEnumType = testEnumProperty.getEnumType();
testBooleanProperty = StructrApp.key(sourceType, "testBoolean");
testIntegerProperty = StructrApp.key(sourceType, "testInteger");
testStringProperty = StructrApp.key(sourceType, "testString");
testDoubleProperty = StructrApp.key(sourceType, "testDouble");
testDateProperty = StructrApp.key(sourceType, "testDate");
assertNotNull(sourceType);
assertNotNull(targetType);
assertNotNull(targetsProperty);
// create 5 target nodes
createTestNodes(targetType, 5);
// create source node
createTestNodes(sourceType, 5);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// check phase: source node should have all five target nodes associated with HAS
try (final Tx tx = app.tx()) {
// check all source nodes
for (final Object obj : app.nodeQuery(sourceType).getAsList()) {
assertNotNull("Invalid nodeQuery result", obj);
final GraphObject sourceNode = (GraphObject) obj;
// test contents of "targets" property
final Object targetNodesObject = sourceNode.getProperty(targetsProperty);
assertTrue("Invalid getProperty result for scripted association", targetNodesObject instanceof List);
final List list = (List) targetNodesObject;
assertEquals("Invalid getProperty result for scripted association", 5, list.size());
}
final GraphObject sourceNode = app.nodeQuery(sourceType).getFirst();
// set testEnum property to OPEN via doTest01 function call, check result
sourceNode.invokeMethod("doTest01", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[0], sourceNode.getProperty(testEnumProperty));
// set testEnum property to CLOSED via doTest02 function call, check result
sourceNode.invokeMethod("doTest02", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[1], sourceNode.getProperty(testEnumProperty));
// set testEnum property to TEST via doTest03 function call, check result
sourceNode.invokeMethod("doTest03", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
// set testEnum property to INVALID via doTest03 function call, expect previous value & error
try {
sourceNode.invokeMethod("doTest04", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
fail("Setting EnumProperty to invalid value should result in an Exception!");
} catch (FrameworkException fx) {
}
// test other property types
sourceNode.invokeMethod("doTest05", Collections.EMPTY_MAP, true);
assertEquals("Invalid setProperty result for BooleanProperty", true, sourceNode.getProperty(testBooleanProperty));
assertEquals("Invalid setProperty result for IntegerProperty", 123, sourceNode.getProperty(testIntegerProperty));
assertEquals("Invalid setProperty result for StringProperty", "testing..", sourceNode.getProperty(testStringProperty));
assertEquals("Invalid setProperty result for DoubleProperty", 1.2345, sourceNode.getProperty(testDoubleProperty));
assertEquals("Invalid setProperty result for DateProperty", new Date(currentTimeMillis), sourceNode.getProperty(testDateProperty));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaTest method testJavaSchemaMethodWithEmptySource.
@Test
public void testJavaSchemaMethodWithEmptySource() {
try (final Tx tx = app.tx()) {
final SchemaNode group = app.nodeQuery(SchemaNode.class).andName("Group").getFirst();
assertNotNull("Schema node Group should exist", group);
final String source = "";
app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, group), new NodeAttribute<>(SchemaMethod.name, "testJavaMethod"), new NodeAttribute<>(SchemaMethod.source, source), new NodeAttribute<>(SchemaMethod.codeType, "java"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaTest method test00DeleteSchemaRelationshipInView.
@Test
public void test00DeleteSchemaRelationshipInView() {
SchemaRelationshipNode rel = null;
try (final Tx tx = app.tx()) {
// create source and target node
final SchemaNode fooNode = app.create(SchemaNode.class, "Foo");
final SchemaNode barNode = app.create(SchemaNode.class, "Bar");
// create relationship
rel = app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, fooNode), new NodeAttribute<>(SchemaRelationshipNode.targetNode, barNode), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "narf"));
// create "public" view that contains the related property
app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "public"), new NodeAttribute<>(SchemaView.schemaNode, fooNode), new NodeAttribute<>(SchemaView.nonGraphProperties, "type, id, narfBars"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
app.delete(rel);
tx.success();
} catch (Throwable t) {
// deletion of relationship should not fail
logger.warn("", t);
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaTest method testBuiltinTypeFlag.
@Test
public void testBuiltinTypeFlag() {
try (final Tx tx = app.tx()) {
final JsonSchema schema = StructrSchema.createFromDatabase(app);
final JsonObjectType type = schema.addType("Test");
// add new type
StructrSchema.extendDatabaseSchema(app, schema);
tx.success();
} catch (Throwable fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// except "Test"
for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
final String name = schemaNode.getName();
final boolean flag = schemaNode.getProperty(SchemaNode.isBuiltinType);
if (name.equals("Test")) {
assertFalse("Non-builtin type Test has isBuiltinType flag set", flag);
} else {
assertTrue("Builtin type " + name + " is missing isBuiltinType flag", flag);
}
}
tx.success();
} catch (Throwable fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class SchemaTest method test04ManualSchemaRelatedPropertyNameCreation.
@Test
public void test04ManualSchemaRelatedPropertyNameCreation() {
try {
try (final Tx tx = app.tx()) {
final SchemaNode source = app.create(SchemaNode.class, "Source");
final SchemaNode target = app.create(SchemaNode.class, "Target");
app.create(SchemaRelationshipNode.class, new NodeAttribute(SchemaRelationshipNode.relationshipType, "link"), new NodeAttribute(SchemaRelationshipNode.sourceNode, source), new NodeAttribute(SchemaRelationshipNode.targetNode, target), new NodeAttribute(SchemaRelationshipNode.sourceMultiplicity, "1"), new NodeAttribute(SchemaRelationshipNode.targetMultiplicity, "*"));
tx.success();
}
checkSchemaString(StructrSchema.createFromDatabase(app).toString());
} catch (FrameworkException | URISyntaxException t) {
logger.warn("", t);
}
}
Aggregations