Search in sources :

Example 11 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SchemaTest method test01Inheritance.

@Test
public void test01Inheritance() {
    // 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 JsonType contact = sourceSchema.addType("Contact").setExtends(sourceSchema.getType("Principal"));
        final JsonType customer = sourceSchema.addType("Customer").setExtends(contact);
        final String schema = sourceSchema.toString();
        final Map<String, Object> map = new GsonBuilder().create().fromJson(schema, Map.class);
        mapPathValue(map, "definitions.Contact.type", "object");
        mapPathValue(map, "definitions.Contact.$extends.0", "#/definitions/Principal");
        mapPathValue(map, "definitions.Customer.type", "object");
        mapPathValue(map, "definitions.Customer.$extends.0", "#/definitions/Contact");
        // advanced: test schema roundtrip
        compareSchemaRoundtrip(sourceSchema);
    } catch (Exception t) {
        logger.warn("", t);
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) URISyntaxException(java.net.URISyntaxException) FrameworkException(org.structr.common.error.FrameworkException) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 12 with JsonSchema

use of org.structr.schema.json.JsonSchema 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.");
    }
}
Also used : JsonReferenceType(org.structr.schema.json.JsonReferenceType) JsonProperty(org.structr.schema.json.JsonProperty) JsonSchema(org.structr.schema.json.JsonSchema) JsonReferenceProperty(org.structr.schema.json.JsonReferenceProperty) JsonObjectType(org.structr.schema.json.JsonObjectType) URISyntaxException(java.net.URISyntaxException) FrameworkException(org.structr.common.error.FrameworkException) InvalidSchemaException(org.structr.schema.json.InvalidSchemaException) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 13 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SchemaTest method test00SimpleProperties.

@Test
public void test00SimpleProperties() {
    try {
        final JsonSchema sourceSchema = StructrSchema.createFromDatabase(app);
        // a customer
        final JsonType customer = sourceSchema.addType("Customer");
        customer.addStringProperty("name", "public", "ui").setRequired(true).setUnique(true);
        customer.addStringProperty("street", "public", "ui");
        customer.addStringProperty("city", "public", "ui");
        customer.addDateProperty("birthday", "public", "ui");
        customer.addEnumProperty("status", "public", "ui").setEnums("active", "retired", "none").setDefaultValue("active");
        customer.addIntegerProperty("count", "public", "ui").setMinimum(1).setMaximum(10, true).setDefaultValue("5");
        customer.addNumberProperty("number", "public", "ui").setMinimum(2.0, true).setMaximum(5.0, true).setDefaultValue("3.0");
        customer.addLongProperty("loong", "public", "ui").setMinimum(20, true).setMaximum(50);
        customer.addBooleanProperty("isCustomer", "public", "ui");
        customer.addFunctionProperty("displayName", "public", "ui").setReadFunction("concat(this.name, '.', this.id)");
        customer.addStringProperty("description", "public", "ui").setContentType("text/plain").setFormat("multi-line");
        customer.addStringArrayProperty("stringArray", "public", "ui");
        customer.addIntegerArrayProperty("intArray", "public", "ui").setMinimum(0, true).setMaximum(100, true);
        customer.addLongArrayProperty("longArray", "public", "ui").setMinimum(1, true).setMaximum(101, true);
        customer.addDoubleArrayProperty("doubleArray", "public", "ui").setMinimum(2.0, true).setMaximum(102.0, true);
        customer.addBooleanArrayProperty("booleanArray", "public", "ui");
        final String schema = sourceSchema.toString();
        final Map<String, Object> map = new GsonBuilder().create().fromJson(schema, Map.class);
        mapPathValue(map, "definitions.Customer.type", "object");
        mapPathValue(map, "definitions.Customer.required.0", "name");
        mapPathValue(map, "definitions.Customer.properties.booleanArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.booleanArray.items.type", "boolean");
        mapPathValue(map, "definitions.Customer.properties.city.unique", null);
        mapPathValue(map, "definitions.Customer.properties.count.type", "integer");
        mapPathValue(map, "definitions.Customer.properties.count.minimum", 1.0);
        mapPathValue(map, "definitions.Customer.properties.count.maximum", 10.0);
        mapPathValue(map, "definitions.Customer.properties.count.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.type", "number");
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.maximum", 102.0);
        mapPathValue(map, "definitions.Customer.properties.doubleArray.items.minimum", 2.0);
        mapPathValue(map, "definitions.Customer.properties.number.type", "number");
        mapPathValue(map, "definitions.Customer.properties.number.minimum", 2.0);
        mapPathValue(map, "definitions.Customer.properties.number.maximum", 5.0);
        mapPathValue(map, "definitions.Customer.properties.number.exclusiveMinimum", true);
        mapPathValue(map, "definitions.Customer.properties.number.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.longArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.longArray.items.type", "long");
        mapPathValue(map, "definitions.Customer.properties.longArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.longArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.longArray.items.maximum", 101.0);
        mapPathValue(map, "definitions.Customer.properties.longArray.items.minimum", 1.0);
        mapPathValue(map, "definitions.Customer.properties.loong.type", "long");
        mapPathValue(map, "definitions.Customer.properties.loong.minimum", 20.0);
        mapPathValue(map, "definitions.Customer.properties.loong.maximum", 50.0);
        mapPathValue(map, "definitions.Customer.properties.loong.exclusiveMinimum", true);
        mapPathValue(map, "definitions.Customer.properties.intArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.intArray.items.type", "integer");
        mapPathValue(map, "definitions.Customer.properties.intArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.intArray.items.exclusiveMaximum", true);
        mapPathValue(map, "definitions.Customer.properties.intArray.items.maximum", 100.0);
        mapPathValue(map, "definitions.Customer.properties.intArray.items.minimum", 0.0);
        mapPathValue(map, "definitions.Customer.properties.isCustomer.type", "boolean");
        mapPathValue(map, "definitions.Customer.properties.description.type", "string");
        mapPathValue(map, "definitions.Customer.properties.description.contentType", "text/plain");
        mapPathValue(map, "definitions.Customer.properties.description.format", "multi-line");
        mapPathValue(map, "definitions.Customer.properties.displayName.type", "function");
        mapPathValue(map, "definitions.Customer.properties.displayName.readFunction", "concat(this.name, '.', this.id)");
        mapPathValue(map, "definitions.Customer.properties.name.type", "string");
        mapPathValue(map, "definitions.Customer.properties.name.unique", true);
        mapPathValue(map, "definitions.Customer.properties.street.type", "string");
        mapPathValue(map, "definitions.Customer.properties.status.type", "string");
        mapPathValue(map, "definitions.Customer.properties.status.enum.0", "active");
        mapPathValue(map, "definitions.Customer.properties.status.enum.1", "none");
        mapPathValue(map, "definitions.Customer.properties.status.enum.2", "retired");
        mapPathValue(map, "definitions.Customer.properties.stringArray.type", "array");
        mapPathValue(map, "definitions.Customer.properties.stringArray.items.type", "string");
        mapPathValue(map, "definitions.Customer.views.public.0", "birthday");
        mapPathValue(map, "definitions.Customer.views.public.1", "booleanArray");
        mapPathValue(map, "definitions.Customer.views.public.2", "city");
        mapPathValue(map, "definitions.Customer.views.public.3", "count");
        mapPathValue(map, "definitions.Customer.views.public.4", "description");
        mapPathValue(map, "definitions.Customer.views.public.5", "displayName");
        mapPathValue(map, "definitions.Customer.views.public.6", "doubleArray");
        mapPathValue(map, "definitions.Customer.views.public.7", "intArray");
        mapPathValue(map, "definitions.Customer.views.public.8", "isCustomer");
        mapPathValue(map, "definitions.Customer.views.public.9", "longArray");
        mapPathValue(map, "definitions.Customer.views.public.10", "loong");
        mapPathValue(map, "definitions.Customer.views.public.11", "name");
        mapPathValue(map, "definitions.Customer.views.public.12", "number");
        mapPathValue(map, "definitions.Customer.views.public.13", "status");
        mapPathValue(map, "definitions.Customer.views.public.14", "street");
        mapPathValue(map, "definitions.Customer.views.ui.0", "birthday");
        mapPathValue(map, "definitions.Customer.views.ui.1", "booleanArray");
        mapPathValue(map, "definitions.Customer.views.ui.2", "city");
        mapPathValue(map, "definitions.Customer.views.ui.3", "count");
        mapPathValue(map, "definitions.Customer.views.ui.4", "description");
        mapPathValue(map, "definitions.Customer.views.ui.5", "displayName");
        mapPathValue(map, "definitions.Customer.views.ui.6", "doubleArray");
        mapPathValue(map, "definitions.Customer.views.ui.7", "intArray");
        mapPathValue(map, "definitions.Customer.views.ui.8", "isCustomer");
        mapPathValue(map, "definitions.Customer.views.ui.9", "longArray");
        mapPathValue(map, "definitions.Customer.views.ui.10", "loong");
        mapPathValue(map, "definitions.Customer.views.ui.11", "name");
        mapPathValue(map, "definitions.Customer.views.ui.12", "number");
        mapPathValue(map, "definitions.Customer.views.ui.13", "status");
        mapPathValue(map, "definitions.Customer.views.ui.14", "street");
        // advanced: test schema roundtrip
        compareSchemaRoundtrip(sourceSchema);
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 14 with JsonSchema

use of org.structr.schema.json.JsonSchema 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"));
}
Also used : Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema) JsonObjectType(org.structr.schema.json.JsonObjectType) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 15 with JsonSchema

use of org.structr.schema.json.JsonSchema in project structr by structr.

the class SchemaTest method test05SchemaRelatedPropertyNameCreationWithPresets.

@Test
public void test05SchemaRelatedPropertyNameCreationWithPresets() {
    try {
        // create test case
        final JsonSchema schema = StructrSchema.newInstance(URI.create(app.getInstanceId()));
        final JsonObjectType source = schema.addType("Source");
        final JsonObjectType target = schema.addType("Target");
        source.relate(target, "link", Relation.Cardinality.OneToMany, "sourceLink", "linkTargets");
        checkSchemaString(schema.toString());
    } catch (FrameworkException t) {
        logger.warn("", t);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) JsonObjectType(org.structr.schema.json.JsonObjectType) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

JsonSchema (org.structr.schema.json.JsonSchema)36 Tx (org.structr.core.graph.Tx)24 Test (org.junit.Test)22 FrameworkException (org.structr.common.error.FrameworkException)19 URISyntaxException (java.net.URISyntaxException)13 JsonType (org.structr.schema.json.JsonType)13 GsonBuilder (com.google.gson.GsonBuilder)9 JsonObjectType (org.structr.schema.json.JsonObjectType)9 StructrTest (org.structr.common.StructrTest)8 InvalidSchemaException (org.structr.schema.json.InvalidSchemaException)8 Gson (com.google.gson.Gson)6 App (org.structr.core.app.App)6 StructrApp (org.structr.core.app.StructrApp)6 NodeInterface (org.structr.core.graph.NodeInterface)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 File (org.structr.web.entity.File)6 IOException (java.io.IOException)5 LinkedHashMap (java.util.LinkedHashMap)5 File (java.io.File)4 JsonReferenceType (org.structr.schema.json.JsonReferenceType)4