Search in sources :

Example 31 with JsonSchema

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

the class AdvancedSchemaTest method testViewProperties.

@Test
public void testViewProperties() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        final JsonType type = schema.addType("Test");
        type.addStringProperty("test", PropertyView.Public);
        type.addViewProperty(PropertyView.Public, "test");
        StructrSchema.extendDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) 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 32 with JsonSchema

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

the class SchemaTest method test06SchemaRelatedPropertyNameCreationWithoutPresets.

@Test
public void test06SchemaRelatedPropertyNameCreationWithoutPresets() {
    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);
        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)

Example 33 with JsonSchema

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

the class SchemaTest method testBuiltinTypeFlag.

@Test
public void testBuiltinTypeFlag() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        final JsonObjectType type = schema.addType("Test");
        // add new type
        StructrSchema.extendDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        // except "Test"
        for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
            final String name = schemaNode.getName();
            final boolean flag = schemaNode.getProperty(SchemaNode.isBuiltinType);
            if (name.equals("Test")) {
                assertFalse("Non-builtin type Test has isBuiltinType flag set", flag);
            } else {
                assertTrue("Builtin type " + name + " is missing isBuiltinType flag", flag);
            }
        }
        tx.success();
    } catch (Throwable fex) {
        fex.printStackTrace();
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) 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 34 with JsonSchema

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

the class CsvImportTest method testCsvFileImportDoubleQuotes.

@Test
public void testCsvFileImportDoubleQuotes() {
    String newFileId = null;
    // test setup
    try (final Tx tx = app.tx()) {
        final String csvData = "\"id\";\"type\";\"name\";\"Test header with whitespace\";\"ümläüt header\"\n" + "\"0\";\"One\";\"name: one\";\"11\";\"22\"\n" + "\"1\";\"Two\";\"name: two\";\"22\";\"33\"\n" + "\"2\";\"Three\";\"name: three\";\"33\";\"44\"";
        final byte[] fileData = csvData.getBytes("utf-8");
        final File file = FileHelper.createFile(securityContext, fileData, "text/csv", File.class, "test.csv");
        // extract UUID for later use
        newFileId = file.getUuid();
        // create new type
        final JsonSchema schema = StructrSchema.createEmptySchema();
        final JsonType newType = schema.addType("Item");
        newType.addStringProperty("name");
        newType.addIntegerProperty("originId").isIndexed();
        newType.addStringProperty("typeName");
        newType.addIntegerProperty("test1");
        newType.addIntegerProperty("test2");
        StructrSchema.extendDatabaseSchema(app, schema);
        // create test user
        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));
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final Map<String, Object> params = new LinkedHashMap<>();
    final Map<String, Object> mappings = new LinkedHashMap<>();
    // import parameters
    params.put("targetType", "Item");
    params.put("quoteChar", "\"");
    params.put("delimiter", ";");
    params.put("mappings", mappings);
    // property mapping
    mappings.put("originId", "id");
    mappings.put("typeName", "type");
    mappings.put("name", "name");
    mappings.put("test1", "Test header with whitespace");
    mappings.put("test2", "ümläüt header");
    RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doCSVImport");
    // wait for result (import is async.)
    try {
        Thread.sleep(2000);
    } catch (Throwable t) {
    }
    // check imported data for correct import
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider conf = StructrApp.getConfiguration();
        final Class type = conf.getNodeEntityClass("Item");
        final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
        assertEquals("Invalid CSV import result, expected 3 items to be created from CSV import. ", 3, items.size());
        final NodeInterface one = items.get(0);
        final NodeInterface two = items.get(1);
        final NodeInterface three = items.get(2);
        assertEquals("Invalid CSV mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid CSV mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid CSV mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid CSV mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid CSV mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid CSV mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) LinkedHashMap(java.util.LinkedHashMap) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 35 with JsonSchema

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

the class XmlImportTest method testXmlFileImport.

@Test
public void testXmlFileImport() {
    final String htmlPropertyData = "<!DOCTYPE html><html><head><title>bad idea</title></head><body><h1>ORLY?</h1></body></html>";
    String newFileId = null;
    // test setup
    try (final Tx tx = app.tx()) {
        final String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" + "<items>\n" + "	<item id=\"0\" type=\"One\" name=\"name: one\">\n" + "		<test1><![CDATA[11]]></test1>\n" + "		<test2>22</test2>\n" + "	</item>\n" + "	<item id=\"1\" type=\"Two\" name=\"name: two\" >\n" + "		<test1>22</test1>\n" + "		<test2>33</test2>\n" + "	</item>\n" + "	<item id=\"2\" type=\"Three\" name=\"name: three\" >\n" + "		<test1>33</test1>\n" + "		<test2>44</test2>\n" + "		<test3><![CDATA[" + htmlPropertyData + "]]></test3>\n" + "	</item>\n" + "</items>\n";
        final byte[] fileData = xmlData.getBytes("utf-8");
        final File file = FileHelper.createFile(securityContext, fileData, "application/xml", File.class, "test.xml");
        // extract UUID for later use
        newFileId = file.getUuid();
        // create new type
        final JsonSchema schema = StructrSchema.createEmptySchema();
        final JsonType newType = schema.addType("Item");
        newType.addStringProperty("name");
        newType.addIntegerProperty("originId").isIndexed();
        newType.addStringProperty("typeName");
        newType.addIntegerProperty("test1");
        newType.addIntegerProperty("test2");
        newType.addStringProperty("test3");
        StructrSchema.extendDatabaseSchema(app, schema);
        // create test user
        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));
        tx.success();
    } catch (Throwable t) {
        t.printStackTrace();
        fail("Unexpected exception.");
    }
    final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final Map<String, Object> params = new LinkedHashMap<>();
    final Map<String, Object> itemConfig = new LinkedHashMap<>();
    final Map<String, Object> itemProperties = new LinkedHashMap<>();
    final Map<String, Object> test1Config = new LinkedHashMap<>();
    final Map<String, Object> test2Config = new LinkedHashMap<>();
    final Map<String, Object> test3Config = new LinkedHashMap<>();
    params.put("/items/item", itemConfig);
    params.put("/items/item/test1", test1Config);
    params.put("/items/item/test2", test2Config);
    params.put("/items/item/test3", test3Config);
    // import parameters
    itemConfig.put("action", "createNode");
    itemConfig.put("isRoot", true);
    itemConfig.put("type", "Item");
    itemConfig.put("properties", itemProperties);
    // property mapping
    itemProperties.put("id", "originId");
    itemProperties.put("type", "typeName");
    itemProperties.put("name", "name");
    test1Config.put("action", "setProperty");
    test1Config.put("propertyName", "test1");
    test2Config.put("action", "setProperty");
    test2Config.put("propertyName", "test2");
    test3Config.put("action", "setProperty");
    test3Config.put("propertyName", "test3");
    RestAssured.given().contentType("application/json; charset=UTF-8").header("X-User", "admin").header("X-Password", "admin").filter(RequestLoggingFilter.logRequestTo(System.out)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).body(gson.toJson(params)).expect().statusCode(200).when().post("/File/" + newFileId + "/doXMLImport");
    // wait for result (import is async.)
    try {
        Thread.sleep(300);
    } catch (Throwable t) {
    }
    // check imported data for correct import
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider conf = StructrApp.getConfiguration();
        final Class type = conf.getNodeEntityClass("Item");
        final List<NodeInterface> items = app.nodeQuery(type).sort(conf.getPropertyKeyForJSONName(type, "originId")).getAsList();
        assertEquals("Invalid XML import result, expected 3 items to be created from XML import. ", 3, items.size());
        final NodeInterface one = items.get(0);
        final NodeInterface two = items.get(1);
        final NodeInterface three = items.get(2);
        assertEquals("Invalid XML mapping result", 0, one.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid XML mapping result", 1, two.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid XML mapping result", 2, three.getProperty(conf.getPropertyKeyForJSONName(type, "originId")));
        assertEquals("Invalid XML mapping result", "One", one.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid XML mapping result", "Two", two.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid XML mapping result", "Three", three.getProperty(conf.getPropertyKeyForJSONName(type, "typeName")));
        assertEquals("Invalid XML mapping result", "name: one", one.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid XML mapping result", "name: two", two.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid XML mapping result", "name: three", three.getProperty(conf.getPropertyKeyForJSONName(type, "name")));
        assertEquals("Invalid XML mapping result", 11, one.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid XML mapping result", 22, two.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid XML mapping result", 33, three.getProperty(conf.getPropertyKeyForJSONName(type, "test1")));
        assertEquals("Invalid XML mapping result", 22, one.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid XML mapping result", 33, two.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid XML mapping result", 44, three.getProperty(conf.getPropertyKeyForJSONName(type, "test2")));
        assertEquals("Invalid XML mapping result", null, one.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
        assertEquals("Invalid XML mapping result", null, two.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
        assertEquals("Invalid XML mapping result", htmlPropertyData, three.getProperty(conf.getPropertyKeyForJSONName(type, "test3")));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) JsonSchema(org.structr.schema.json.JsonSchema) Gson(com.google.gson.Gson) LinkedHashMap(java.util.LinkedHashMap) File(org.structr.web.entity.File) NodeInterface(org.structr.core.graph.NodeInterface) 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