use of org.structr.core.entity.SchemaMethod in project structr by structr.
the class SchemaMethodsTest method test02SchemaMethodOnCustomType.
@Test
public void test02SchemaMethodOnCustomType() {
final String customTypeName = "FooFile";
final String schemaMethodName = "testFooFileMethod";
try (final Tx tx = app.tx()) {
// Add schema method "testFileMethod" to built-in File class
SchemaNode fooFileDef = app.create(SchemaNode.class, customTypeName);
final PropertyMap testFooFileMethodProperties = new PropertyMap();
testFooFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
testFooFileMethodProperties.put(SchemaMethod.source, "()");
testFooFileMethodProperties.put(SchemaMethod.schemaNode, fooFileDef);
SchemaMethod testFooFileMethod = app.create(SchemaMethod.class, testFooFileMethodProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
createAdminUser();
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post(customTypeName + "/" + schemaMethodName);
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaMethod in project structr by structr.
the class SchemaMethodsTest method test04SchemaMethodOnEntityOfCustomType.
@Test
public void test04SchemaMethodOnEntityOfCustomType() {
final String customTypeName = "FooFile";
final String schemaMethodName = "testFooFileMethod";
try (final Tx tx = app.tx()) {
// Add schema method "testFileMethod" to built-in File class
SchemaNode fooFileDef = app.create(SchemaNode.class, customTypeName);
final PropertyMap testFooFileMethodProperties = new PropertyMap();
testFooFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
testFooFileMethodProperties.put(SchemaMethod.source, "()");
testFooFileMethodProperties.put(SchemaMethod.schemaNode, fooFileDef);
SchemaMethod testFooFileMethod = app.create(SchemaMethod.class, testFooFileMethodProperties);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
createAdminUser();
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
String id = createEntityAsSuperUser(customTypeName, "{'name':'Test Foo File'}");
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post(customTypeName + "/" + id + "/" + schemaMethodName);
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaMethod 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.SchemaMethod in project structr by structr.
the class StructrGlobalSchemaMethods method createDatabaseSchema.
public void createDatabaseSchema(final App app, final JsonSchema.ImportMode importMode) throws FrameworkException {
final SecurityContext context = SecurityContext.getSuperUserInstance();
context.setDoTransactionNotifications(false);
if (JsonSchema.ImportMode.replace.equals(importMode)) {
for (final SchemaMethod method : app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, null).getAsList()) {
app.delete(method);
}
for (final Map<String, Object> entry : globalMethods) {
app.create(SchemaMethod.class, PropertyMap.inputTypeToJavaType(context, SchemaMethod.class, entry));
}
} else if (JsonSchema.ImportMode.extend.equals(importMode)) {
for (final Map<String, Object> entry : globalMethods) {
final PropertyMap schemaMethodProperties = PropertyMap.inputTypeToJavaType(context, SchemaMethod.class, entry);
for (final SchemaMethod method : app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, null).andName(schemaMethodProperties.get(SchemaMethod.name)).getAsList()) {
app.delete(method);
}
app.create(SchemaMethod.class, schemaMethodProperties);
}
}
}
Aggregations