use of org.structr.core.entity.SchemaNode 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.SchemaNode 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.SchemaNode in project structr by structr.
the class AdvancedSchemaTest method test01InheritanceOfFileAttributesToImage.
@Test
public void test01InheritanceOfFileAttributesToImage() {
try (final Tx tx = app.tx()) {
createAdminUser();
ResourceAccessTest.createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
// Add String property "testFile" to built-in File class
SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
SchemaProperty testFileProperty = app.create(SchemaProperty.class);
final PropertyMap testFileProperties = new PropertyMap();
testFileProperties.put(SchemaProperty.name, "testFile");
testFileProperties.put(SchemaProperty.propertyType, "String");
testFileProperties.put(SchemaProperty.schemaNode, fileNodeDef);
testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
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).expect().statusCode(200).body("result", Matchers.hasSize(count1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).when().get("/_schema/File/ui");
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
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).expect().statusCode(200).body("result", Matchers.hasSize(count2)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).when().get("/_schema/Image/ui");
tx.success();
} catch (FrameworkException ex) {
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class AdvancedSchemaTest method test04SchemaPropertyOrderInBuiltInViews.
@Test
public void test04SchemaPropertyOrderInBuiltInViews() {
try (final Tx tx = app.tx()) {
createAdminUser();
createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
tx.success();
} catch (Exception ex) {
logger.error("", ex);
}
final GenericProperty jsonName = new GenericProperty("jsonName");
SchemaNode test = null;
String id = null;
try (final Tx tx = app.tx()) {
// create test type
test = app.create(SchemaNode.class, "Test");
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
try (final Tx tx = app.tx()) {
// create view with sort order
final List<SchemaView> list = test.getProperty(SchemaNode.schemaViews);
// create properties
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "four"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
final List<PropertyKey> list = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
Assert.assertEquals("Invalid number of properties in sorted view", 6, list.size());
Assert.assertEquals("id", list.get(0).dbName());
Assert.assertEquals("type", list.get(1).dbName());
Assert.assertEquals("one", list.get(2).dbName());
Assert.assertEquals("two", list.get(3).dbName());
Assert.assertEquals("three", list.get(4).dbName());
Assert.assertEquals("four", list.get(5).dbName());
try (final Tx tx = app.tx()) {
for (final SchemaView testView : test.getProperty(SchemaNode.schemaViews)) {
// modify sort order
testView.setProperty(SchemaView.sortOrder, "type, one, id, two, three, four, name");
}
// create test entity
final NodeInterface node = app.create(StructrApp.getConfiguration().getNodeEntityClass("Test"));
// save UUID for later
id = node.getUuid();
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
Assert.assertEquals("Invalid number of properties in sorted view", 6, list2.size());
Assert.assertEquals("id", list2.get(0).dbName());
Assert.assertEquals("type", list2.get(1).dbName());
Assert.assertEquals("one", list2.get(2).dbName());
Assert.assertEquals("two", list2.get(3).dbName());
Assert.assertEquals("three", list2.get(4).dbName());
Assert.assertEquals("four", list2.get(5).dbName());
// test schema resource
RestAssured.given().contentType("application/json; charset=UTF-8").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).expect().statusCode(200).body("result", hasSize(6)).body("result[0].jsonName", equalTo("id")).body("result[1].jsonName", equalTo("type")).body("result[2].jsonName", equalTo("one")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).when().get("/_schema/Test/public");
// test actual REST resource (not easy to extract and verify
// JSON property order, that's why we're using replaceAll and
// string comparison..
final String[] actual = 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).expect().statusCode(200).when().get("/Test").body().asString().replaceAll("[\\s]+", "").split("[\\W]+");
// we can only test the actual ORDER of the JSON result object by splitting it on whitespace and validating the resulting array
assertEquals("Invalid JSON result for sorted property view", "", actual[0]);
assertEquals("Invalid JSON result for sorted property view", "query_time", actual[1]);
assertEquals("Invalid JSON result for sorted property view", "0", actual[2]);
assertEquals("Invalid JSON result for sorted property view", "result_count", actual[4]);
assertEquals("Invalid JSON result for sorted property view", "1", actual[5]);
assertEquals("Invalid JSON result for sorted property view", "result", actual[6]);
assertEquals("Invalid JSON result for sorted property view", "id", actual[7]);
assertEquals("Invalid JSON result for sorted property view", id, actual[8]);
assertEquals("Invalid JSON result for sorted property view", "type", actual[9]);
assertEquals("Invalid JSON result for sorted property view", "Test", actual[10]);
assertEquals("Invalid JSON result for sorted property view", "one", actual[11]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[12]);
assertEquals("Invalid JSON result for sorted property view", "two", actual[13]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[14]);
assertEquals("Invalid JSON result for sorted property view", "three", actual[15]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[16]);
assertEquals("Invalid JSON result for sorted property view", "four", actual[17]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[18]);
assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[19]);
// try built-in function
try {
final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "public" });
Assert.assertEquals("Invalid number of properties in sorted view", 6, list3.size());
Assert.assertEquals("id", list3.get(0).get(jsonName));
Assert.assertEquals("type", list3.get(1).get(jsonName));
Assert.assertEquals("one", list3.get(2).get(jsonName));
Assert.assertEquals("two", list3.get(3).get(jsonName));
Assert.assertEquals("three", list3.get(4).get(jsonName));
Assert.assertEquals("four", list3.get(5).get(jsonName));
} catch (FrameworkException fex) {
fex.printStackTrace();
}
// try scripting call
try {
final List<GraphObjectMap> list4 = (List) Scripting.evaluate(new ActionContext(securityContext), null, "${type_info('Test', 'public')}", "test");
Assert.assertEquals("Invalid number of properties in sorted view", 6, list4.size());
Assert.assertEquals("id", list4.get(0).get(jsonName));
Assert.assertEquals("type", list4.get(1).get(jsonName));
Assert.assertEquals("one", list4.get(2).get(jsonName));
Assert.assertEquals("two", list4.get(3).get(jsonName));
Assert.assertEquals("three", list4.get(4).get(jsonName));
Assert.assertEquals("four", list4.get(5).get(jsonName));
} catch (FrameworkException fex) {
fex.printStackTrace();
}
}
use of org.structr.core.entity.SchemaNode in project structr by structr.
the class PropertyTest method testFunctionPropertyIndexing.
// ----- function property tests -----
/**
* This test creates a new type "Test" and links it to
* the built-in type "Group". It then creates a function
* property that references the name of the related group
* and assumes that a test entity is found by its related
* group name.
*/
@Test
public void testFunctionPropertyIndexing() {
// schema setup
try (final Tx tx = app.tx()) {
final SchemaNode group = app.nodeQuery(SchemaNode.class).andName("Group").getFirst();
final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testFunction"), "Function(this.group.name)"));
assertNotNull("Invalid schema setup result", group);
assertNotNull("Invalid schema setup result", test);
app.create(SchemaRelationshipNode.class, new NodeAttribute<>(SchemaRelationshipNode.sourceNode, test), new NodeAttribute<>(SchemaRelationshipNode.targetNode, group), new NodeAttribute<>(SchemaRelationshipNode.sourceMultiplicity, "*"), new NodeAttribute<>(SchemaRelationshipNode.targetMultiplicity, "1"), new NodeAttribute<>(SchemaRelationshipNode.sourceJsonName, "tests"), new NodeAttribute<>(SchemaRelationshipNode.targetJsonName, "group"), new NodeAttribute<>(SchemaRelationshipNode.relationshipType, "group"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// entity setup
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class testType = config.getNodeEntityClass("Test");
// create test type without link to group!
app.create(testType);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// entity setup
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class testType = config.getNodeEntityClass("Test");
final Class groupType = config.getNodeEntityClass("Group");
final GraphObject group = app.create(groupType, "testgroup");
final GraphObject test = app.nodeQuery(testType).getFirst();
// create Test with link to Group
test.setProperty(StructrApp.key(testType, "group"), group);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// test
try (final Tx tx = app.tx()) {
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class testType = config.getNodeEntityClass("Test");
final PropertyKey key = StructrApp.key(testType, "testFunction");
// fetch test node
final GraphObject testNode = app.nodeQuery(testType).getFirst();
final GraphObject result = app.nodeQuery(testType).and(key, "testgroup").getFirst();
// test indexing
assertEquals("Invalid FunctionProperty indexing result", testNode, result);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
Aggregations