use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class StructrUiTest method createTestNode.
protected <T extends NodeInterface> T createTestNode(final Class<T> type, final NodeAttribute... attrs) throws FrameworkException {
final PropertyMap props = new PropertyMap();
props.put(AbstractNode.type, type.getSimpleName());
for (final NodeAttribute attr : attrs) {
props.put(attr.getKey(), attr.getValue());
}
return app.create(type, props);
}
use of org.structr.core.graph.NodeAttribute 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.graph.NodeAttribute 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.graph.NodeAttribute 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);
}
}
use of org.structr.core.graph.NodeAttribute in project structr by structr.
the class ValidationTest method testConcurrentValidationWithInheritance.
@Test
public void testConcurrentValidationWithInheritance() {
final int count = 100;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_name"), "+String!"));
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "ItemDerived"), new NodeAttribute(SchemaNode.extendsClass, "org.structr.dynamic.Item"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class baseType = StructrApp.getConfiguration().getNodeEntityClass("Item");
final Class derivedType = StructrApp.getConfiguration().getNodeEntityClass("ItemDerived");
assertNotNull(baseType);
assertNotNull(derivedType);
final PropertyKey name = StructrApp.key(baseType, "name");
assertNotNull(name);
final Runnable tester = new Runnable() {
@Override
public void run() {
for (int i = 0; i < count; i++) {
// testing must be done in an isolated transaction
try (final Tx tx = app.tx()) {
if (Math.random() < 0.5) {
app.create(derivedType, "Item" + i);
} else {
app.create(baseType, "Item" + i);
}
tx.success();
} catch (FrameworkException ignore) {
}
}
}
};
// submit three test instances
final ExecutorService executor = Executors.newCachedThreadPool();
final Future f1 = executor.submit(tester);
final Future f2 = executor.submit(tester);
final Future f3 = executor.submit(tester);
try {
f1.get();
f2.get();
f3.get();
} catch (Throwable ex) {
}
List<GraphObject> result = null;
try (final Tx tx = app.tx()) {
result = app.nodeQuery(baseType).getAsList();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// verify that only count entities have been created.
assertEquals("Invalid concurrent validation result", count, result.size());
executor.shutdownNow();
}
Aggregations