use of org.structr.schema.json.JsonObjectType in project structr by structr.
the class NestedResourcesTest method testMultiLevelUpdateOnPost.
@Test
public void testMultiLevelUpdateOnPost() {
try {
final JsonSchema schema = StructrSchema.newInstance(URI.create("http://localhost/test/#"));
final JsonObjectType document = schema.addType("TestDocument");
final JsonObjectType version = schema.addType("TestVersion");
final JsonObjectType author = schema.addType("TestAuthor");
document.relate(version, "VERSION").setCardinality(Relation.Cardinality.OneToOne).setTargetPropertyName("hasVersion");
version.relate(author, "AUTHOR").setCardinality(Relation.Cardinality.OneToOne).setTargetPropertyName("hasAuthor");
// extend public view to make result testable via REST GET
document.addViewProperty("public", "hasVersion");
document.addViewProperty("public", "name");
version.addViewProperty("public", "hasAuthor");
version.addViewProperty("public", "name");
author.addViewProperty("public", "name");
StructrSchema.extendDatabaseSchema(app, schema);
} catch (URISyntaxException | FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
createEntityAsSuperUser("/User", "{ name: admin, password: admin, isAdmin: true }");
final String authorId = createEntityAsUser("admin", "admin", "/TestAuthor", "{ name: 'Tester' }");
final String versionId = createEntityAsUser("admin", "admin", "/TestVersion", "{ name: 'TestVersion' }");
final String documentId = createEntityAsUser("admin", "admin", "/TestDocument", "{ name: 'TestDocument', hasVersion: { id: '" + versionId + "', hasAuthor: { id: '" + authorId + "' } } } ");
// check document to have correct associations
RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseTo(System.out)).header("X-User", "admin").header("X-Password", "admin").expect().statusCode(200).body("result_count", equalTo(1)).body("result.id", equalTo(documentId)).body("result.name", equalTo("TestDocument")).body("result.hasVersion.id", equalTo(versionId)).body("result.hasVersion.name", equalTo("TestVersion")).body("result.hasVersion.hasAuthor.id", equalTo(authorId)).body("result.hasVersion.hasAuthor.name", equalTo("Tester")).when().get("/TestDocument/" + documentId);
}
use of org.structr.schema.json.JsonObjectType in project structr by structr.
the class StructrTypeDefinitions method addType.
public JsonObjectType addType(final String name) {
final JsonType type = getType(name, false);
if (type != null) {
return (JsonObjectType) type;
}
final StructrNodeTypeDefinition def = new StructrNodeTypeDefinition(root, name);
typeDefinitions.add(def);
return def;
}
use of org.structr.schema.json.JsonObjectType in project structr by structr.
the class SchemaTest method test02SimpleSymmetricReferences.
@Test
public void test02SimpleSymmetricReferences() {
// we need to wait for the schema service to be initialized here.. :(
try {
Thread.sleep(1000);
} catch (Throwable t) {
}
try {
final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
final JsonObjectType project = sourceSchema.addType("Project");
final JsonObjectType task = sourceSchema.addType("Task");
// create relation
final JsonReferenceType rel = project.relate(task, "has", Cardinality.OneToMany, "project", "tasks");
rel.setName("ProjectTasks");
final String schema = sourceSchema.toString();
System.out.println(schema);
// test map paths
final Map<String, Object> map = new GsonBuilder().create().fromJson(schema, Map.class);
mapPathValue(map, "definitions.Project.type", "object");
mapPathValue(map, "definitions.Project.properties.tasks.$link", "#/definitions/ProjectTasks");
mapPathValue(map, "definitions.Project.properties.tasks.items.$ref", "#/definitions/Task");
mapPathValue(map, "definitions.Project.properties.tasks.type", "array");
mapPathValue(map, "definitions.ProjectTasks.$source", "#/definitions/Project");
mapPathValue(map, "definitions.ProjectTasks.$target", "#/definitions/Task");
mapPathValue(map, "definitions.ProjectTasks.cardinality", "OneToMany");
mapPathValue(map, "definitions.ProjectTasks.rel", "has");
mapPathValue(map, "definitions.ProjectTasks.sourceName", "project");
mapPathValue(map, "definitions.ProjectTasks.targetName", "tasks");
mapPathValue(map, "definitions.ProjectTasks.type", "object");
mapPathValue(map, "definitions.Task.type", "object");
mapPathValue(map, "definitions.Task.properties.project.$link", "#/definitions/ProjectTasks");
mapPathValue(map, "definitions.Task.properties.project.$ref", "#/definitions/Project");
mapPathValue(map, "definitions.Task.properties.project.type", "object");
// test
compareSchemaRoundtrip(sourceSchema);
} catch (FrameworkException | InvalidSchemaException | URISyntaxException ex) {
logger.warn("", ex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.json.JsonObjectType in project structr by structr.
the class SchemaTest method test03SchemaBuilder.
@Test
public void test03SchemaBuilder() {
// we need to wait for the schema service to be initialized here.. :(
try {
Thread.sleep(1000);
} catch (Throwable t) {
}
try {
final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
final String instanceId = app.getInstanceId();
final JsonObjectType task = sourceSchema.addType("Task");
final JsonProperty title = task.addStringProperty("title", "public", "ui").setRequired(true);
final JsonProperty desc = task.addStringProperty("description", "public", "ui").setRequired(true);
task.addDateProperty("description", "public", "ui").setDatePattern("dd.MM.yyyy").setRequired(true);
// test function property
task.addFunctionProperty("displayName", "public", "ui").setReadFunction("this.name");
task.addFunctionProperty("javascript", "public", "ui").setReadFunction("{ var x = 'test'; return x; }").setContentType("application/x-structr-javascript");
// a project
final JsonObjectType project = sourceSchema.addType("Project");
project.addStringProperty("name", "public", "ui").setRequired(true);
final JsonReferenceType projectTasks = project.relate(task, "HAS", Cardinality.OneToMany, "project", "tasks");
projectTasks.setCascadingCreate(Cascade.targetToSource);
project.getViewPropertyNames("public").add("tasks");
task.getViewPropertyNames("public").add("project");
// test enums
project.addEnumProperty("status", "ui").setEnums("active", "planned", "finished");
// a worker
final JsonObjectType worker = sourceSchema.addType("Worker");
final JsonReferenceType workerTasks = worker.relate(task, "HAS", Cardinality.OneToMany, "worker", "tasks");
workerTasks.setCascadingDelete(Cascade.sourceToTarget);
// reference Worker -> Task
final JsonReferenceProperty workerProperty = workerTasks.getSourceProperty();
final JsonReferenceProperty tasksProperty = workerTasks.getTargetProperty();
tasksProperty.setName("renamedTasks");
worker.addReferenceProperty("taskNames", tasksProperty, "public", "ui").setProperties("name");
worker.addReferenceProperty("taskInfos", tasksProperty, "public", "ui").setProperties("id", "name");
worker.addReferenceProperty("taskErrors", tasksProperty, "public", "ui").setProperties("id");
task.addReferenceProperty("workerName", workerProperty, "public", "ui").setProperties("name");
task.addReferenceProperty("workerNotion", workerProperty, "public", "ui").setProperties("id");
// test date properties..
project.addDateProperty("startDate", "public", "ui");
// methods
project.addMethod("onCreate", "set(this, 'name', 'wurst')", "comment for wurst");
// test URIs
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/#", sourceSchema.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Task", task.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Task/properties/title", title.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Task/properties/description", desc.getId().toString());
assertEquals("Invalid schema URI", "https://structr.org/schema/" + instanceId + "/definitions/Worker/properties/renamedTasks", tasksProperty.getId().toString());
compareSchemaRoundtrip(sourceSchema);
} catch (Exception ex) {
ex.printStackTrace();
logger.warn("", ex);
fail("Unexpected exception.");
}
}
use of org.structr.schema.json.JsonObjectType in project structr by structr.
the class SchemaTest method testViewInheritedFromInterface.
@Test
public void testViewInheritedFromInterface() {
try (final Tx tx = app.tx()) {
final JsonSchema schema = StructrSchema.createFromDatabase(app);
final JsonObjectType type = schema.addType("Test");
// make test type inherit from Favoritable (should add views)
type.setImplements(URI.create("#/definitions/Favoritable"));
// ----- interface Favoritable -----
type.overrideMethod("setFavoriteContent", false, "");
type.overrideMethod("getFavoriteContent", false, "return \"getFavoriteContent();\";");
type.overrideMethod("getFavoriteContentType", false, "return \"getContentType();\";");
type.overrideMethod("getContext", false, "return \"getContext();\";");
// add new type
StructrSchema.extendDatabaseSchema(app, schema);
tx.success();
} catch (Throwable fex) {
fex.printStackTrace();
fail("Unexpected exception");
}
final Class testType = StructrApp.getConfiguration().getNodeEntityClass("Test");
final Set<String> views = StructrApp.getConfiguration().getPropertyViewsForType(testType);
assertTrue("Property view is not inherited correctly", views.contains("fav"));
}
Aggregations