use of org.structr.schema.action.ActionContext in project structr by structr.
the class AdvancedSchemaTest method test04SchemaPropertyOrderInCustomViews.
@Test
public void test04SchemaPropertyOrderInCustomViews() {
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");
SchemaView testView = null;
String id = null;
try (final Tx tx = app.tx()) {
// create test type
final SchemaNode test = app.create(SchemaNode.class, "Test");
// create view with sort order
testView = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "test"), new NodeAttribute<>(SchemaView.schemaNode, test), new NodeAttribute<>(SchemaView.sortOrder, "one, two, three, four, id, type, name"), new NodeAttribute<>(SchemaView.nonGraphProperties, "id, type, name"));
final List<SchemaView> list = new LinkedList<>();
list.add(testView);
// 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, "test"));
Assert.assertEquals("Invalid number of properties in sorted view", 7, list.size());
Assert.assertEquals("one", list.get(0).dbName());
Assert.assertEquals("two", list.get(1).dbName());
Assert.assertEquals("three", list.get(2).dbName());
Assert.assertEquals("four", list.get(3).dbName());
Assert.assertEquals("id", list.get(4).dbName());
Assert.assertEquals("type", list.get(5).dbName());
Assert.assertEquals("name", list.get(6).dbName());
try (final Tx tx = app.tx()) {
// 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"));
id = node.getUuid();
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "test"));
Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
Assert.assertEquals("type", list2.get(0).dbName());
Assert.assertEquals("one", list2.get(1).dbName());
Assert.assertEquals("id", 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());
Assert.assertEquals("name", list2.get(6).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(7)).body("result[0].jsonName", equalTo("type")).body("result[1].jsonName", equalTo("one")).body("result[2].jsonName", equalTo("id")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).body("result[6].jsonName", equalTo("name")).when().get("/_schema/Test/test");
// 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/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", "type", actual[7]);
assertEquals("Invalid JSON result for sorted property view", "Test", actual[8]);
assertEquals("Invalid JSON result for sorted property view", "one", actual[9]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[10]);
assertEquals("Invalid JSON result for sorted property view", "id", actual[11]);
assertEquals("Invalid JSON result for sorted property view", id, 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", "name", actual[19]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[20]);
assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[21]);
// try built-in function
try {
final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "test" });
Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
Assert.assertEquals("type", list3.get(0).get(jsonName));
Assert.assertEquals("one", list3.get(1).get(jsonName));
Assert.assertEquals("id", 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));
Assert.assertEquals("name", list3.get(6).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', 'test')}", "test");
Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
Assert.assertEquals("type", list4.get(0).get(jsonName));
Assert.assertEquals("one", list4.get(1).get(jsonName));
Assert.assertEquals("id", 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));
Assert.assertEquals("name", list4.get(6).get(jsonName));
} catch (FrameworkException fex) {
fex.printStackTrace();
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class AdvancedSchemaTest method testSchemaPropertyOrderInInheritedViews.
@Test
public void testSchemaPropertyOrderInInheritedViews() {
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");
SchemaView testView = null;
String id = null;
try (final Tx tx = app.tx()) {
final SchemaNode testBase = app.create(SchemaNode.class, "TestBase");
final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.TestBase"));
// create view with sort order
testView = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "test"), new NodeAttribute<>(SchemaView.schemaNode, test), new NodeAttribute<>(SchemaView.sortOrder, "one, two, three, four, id, type, name"), new NodeAttribute<>(SchemaView.nonGraphProperties, "id, type, name"));
final List<SchemaView> list = new LinkedList<>();
list.add(testView);
// create properties
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), 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, "test"));
Assert.assertEquals("Invalid number of properties in sorted view", 7, list.size());
Assert.assertEquals("one", list.get(0).dbName());
Assert.assertEquals("two", list.get(1).dbName());
Assert.assertEquals("three", list.get(2).dbName());
Assert.assertEquals("four", list.get(3).dbName());
Assert.assertEquals("id", list.get(4).dbName());
Assert.assertEquals("type", list.get(5).dbName());
Assert.assertEquals("name", list.get(6).dbName());
try (final Tx tx = app.tx()) {
// 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"));
id = node.getUuid();
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "test"));
Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
Assert.assertEquals("type", list2.get(0).dbName());
Assert.assertEquals("one", list2.get(1).dbName());
Assert.assertEquals("id", 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());
Assert.assertEquals("name", list2.get(6).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(7)).body("result[0].jsonName", equalTo("type")).body("result[1].jsonName", equalTo("one")).body("result[2].jsonName", equalTo("id")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).body("result[6].jsonName", equalTo("name")).when().get("/_schema/Test/test");
// 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/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", "type", actual[7]);
assertEquals("Invalid JSON result for sorted property view", "Test", actual[8]);
assertEquals("Invalid JSON result for sorted property view", "one", actual[9]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[10]);
assertEquals("Invalid JSON result for sorted property view", "id", actual[11]);
assertEquals("Invalid JSON result for sorted property view", id, 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", "name", actual[19]);
assertEquals("Invalid JSON result for sorted property view", "null", actual[20]);
assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[21]);
// try built-in function
try {
final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "test" });
Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
Assert.assertEquals("type", list3.get(0).get(jsonName));
Assert.assertEquals("one", list3.get(1).get(jsonName));
Assert.assertEquals("id", 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));
Assert.assertEquals("name", list3.get(6).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', 'test')}", "test");
Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
Assert.assertEquals("type", list4.get(0).get(jsonName));
Assert.assertEquals("one", list4.get(1).get(jsonName));
Assert.assertEquals("id", 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));
Assert.assertEquals("name", list4.get(6).get(jsonName));
} catch (FrameworkException fex) {
fex.printStackTrace();
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class RenderContextTest method testAnyAllAndNoneFunctions1.
@Test
public void testAnyAllAndNoneFunctions1() {
final ActionContext ctx = new ActionContext(securityContext, null);
Principal user = null;
TestOne test = null;
try (final Tx tx = app.tx()) {
user = app.create(User.class, "user1");
test = app.create(TestOne.class, "test1");
app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group1"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
final Group group2 = app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group2"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
app.create(Group.class, new NodeAttribute<>(StructrApp.key(AbstractNode.class, "name"), "group3"), new NodeAttribute<>(StructrApp.key(Group.class, "members"), Arrays.asList(new Principal[] { user })));
test.setProperty(AbstractNode.owner, group2);
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
ctx.setConstant("user", user);
ctx.setConstant("test", test);
assertEquals("Invalid any() result", "true", Scripting.replaceVariables(ctx, null, "${any(user.groups, is_allowed(data, test, 'read'))}"));
assertEquals("Invalid all() result", "false", Scripting.replaceVariables(ctx, null, "${all(user.groups, is_allowed(data, test, 'read'))}"));
assertEquals("Invalid none() result", "false", Scripting.replaceVariables(ctx, null, "${none(user.groups, is_allowed(data, test, 'read'))}"));
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class RenderContextTest method testVariableReplacementInDynamicTypes.
@Test
public void testVariableReplacementInDynamicTypes() {
SchemaNode itemNode = null;
NodeInterface parent = null;
NodeInterface child1 = null;
NodeInterface child2 = null;
try (final Tx tx = app.tx()) {
itemNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"));
final PropertyMap properties = new PropertyMap();
properties.put(SchemaRelationshipNode.sourceId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.targetId, itemNode.getUuid());
properties.put(SchemaRelationshipNode.relationshipType, "CHILD");
properties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
properties.put(SchemaRelationshipNode.targetMultiplicity, "*");
properties.put(SchemaRelationshipNode.sourceJsonName, "parentItem");
properties.put(SchemaRelationshipNode.targetJsonName, "children");
app.create(SchemaRelationshipNode.class, properties);
// compile the stuff
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final Class itemClass = config.getNodeEntityClass("Item");
final PropertyKey childrenProperty = StructrApp.key(itemClass, "children");
// create parent/child relationship
try (final Tx tx = app.tx()) {
parent = app.create(itemClass);
child1 = app.create(itemClass);
child2 = app.create(itemClass);
final List<NodeInterface> children = new LinkedList<>();
children.add(child1);
children.add(child2);
parent.setProperties(parent.getSecurityContext(), new PropertyMap(childrenProperty, children));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// verify that parent has two children
try (final Tx tx = app.tx()) {
// verify that parentItem can be accessed....
final Object value = parent.getProperty(childrenProperty);
assertTrue(value instanceof Collection);
final Collection coll = (Collection) value;
assertEquals(2, coll.size());
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
// check property access in template expressions
try (final Tx tx = app.tx()) {
assertEquals(parent.toString(), Scripting.replaceVariables(new ActionContext(securityContext), child1, "${this.parentItem}"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception");
}
}
use of org.structr.schema.action.ActionContext in project structr by structr.
the class MessageSubscriber method onMessage.
static RestMethodResult onMessage(MessageSubscriber thisSubscriber, final String topic, final String message, SecurityContext securityContext) throws FrameworkException {
if (!StringUtils.isEmpty(thisSubscriber.getCallback())) {
String script = "${" + thisSubscriber.getCallback() + "}";
Map<String, Object> params = new HashMap<>();
params.put("topic", topic);
params.put("message", message);
ActionContext ac = new ActionContext(securityContext, params);
Scripting.replaceVariables(ac, thisSubscriber, script);
}
return new RestMethodResult(200);
}
Aggregations