Search in sources :

Example 1 with JsonSchema

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

the class DeploymentTest method test36BuiltInTypesWithProperties.

@Test
public void test36BuiltInTypesWithProperties() {
    // setup schema
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        Assert.assertNotNull("StructrSchema must return a valid schema object", schema);
        final JsonType pageType = schema.getType("Page");
        final JsonType fileType = schema.getType("File");
        Assert.assertNotNull("Type Page must exist in every schema", pageType);
        Assert.assertNotNull("Type File must exist in every schema", fileType);
        pageType.addIntegerProperty("displayPosition");
        pageType.addStringProperty("icon");
        fileType.addIntegerProperty("test1");
        fileType.addStringProperty("test2");
        // install schema
        StructrSchema.replaceDatabaseSchema(app, schema);
        tx.success();
    } catch (FrameworkException | URISyntaxException fex) {
        fail("Unexpected exception.");
    }
    // setup
    try (final Tx tx = app.tx()) {
        final Page page = Page.createSimplePage(securityContext, "page1");
        page.setProperty(StructrApp.key(Page.class, "displayPosition"), 12);
        page.setProperty(StructrApp.key(Page.class, "icon"), "icon");
        final Folder folder = app.create(Folder.class, "files");
        folder.setProperty(StructrApp.key(Folder.class, "includeInFrontendExport"), true);
        // create test file with custom attributes
        app.create(File.class, new NodeAttribute<>(StructrApp.key(File.class, "name"), "test.txt"), new NodeAttribute<>(StructrApp.key(File.class, "parent"), folder), new NodeAttribute<>(StructrApp.key(File.class, "contentType"), "text/plain"), new NodeAttribute<>(StructrApp.key(File.class, "test1"), 123), new NodeAttribute<>(StructrApp.key(File.class, "test2"), "testString"));
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    compare(calculateHash(), true);
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) Page(org.structr.web.entity.dom.Page) URISyntaxException(java.net.URISyntaxException) Folder(org.structr.web.entity.Folder) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with JsonSchema

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

the class PropertyTest method testCustomProperty.

@Test
public void testCustomProperty() {
    // schema setup
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app, Arrays.asList("Image"));
        final Gson gson = new GsonBuilder().create();
        final Map<String, Object> str = (Map<String, Object>) gson.fromJson(schema.toString(), Map.class);
        final Map<String, Object> def = (Map<String, Object>) str.get("definitions");
        final Map<String, Object> img = (Map<String, Object>) def.get("Image");
        final Map<String, Object> pro = (Map<String, Object>) img.get("properties");
        final Map<String, Object> tn = (Map<String, Object>) pro.get("tnMid");
        assertEquals("Export of custom property should contain format string.", "300, 300, false", tn.get("format"));
        tx.success();
    } catch (FrameworkException | URISyntaxException t) {
        t.printStackTrace();
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with JsonSchema

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

the class AdvancedSchemaTest method testMixedOnCreateMethods.

@Test
public void testMixedOnCreateMethods() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        schema.getType("User").addMethod("onCreate", "log('test')", "test");
        StructrSchema.extendDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 4 with JsonSchema

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

the class AdvancedSchemaTest method testIsValidPasswordMethodOfUser.

@Test
public void testIsValidPasswordMethodOfUser() {
    try (final Tx tx = app.tx()) {
        app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        schema.getType("User").overrideMethod("isValidPassword", false, "return true;");
        StructrSchema.extendDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
    }
    RestAssured.given().filter(ResponseLoggingFilter.logResponseTo(System.out)).contentType("application/json; charset=UTF-8").headers("X-User", "admin", "X-Password", "wrong").expect().statusCode(200).when().get("/User");
}
Also used : Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 5 with JsonSchema

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

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