Search in sources :

Example 16 with JsonSchema

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

the class SchemaTest method compareSchemaRoundtrip.

private void compareSchemaRoundtrip(final JsonSchema sourceSchema) throws FrameworkException, InvalidSchemaException, URISyntaxException {
    final String source = sourceSchema.toString();
    System.out.println("##################### source");
    System.out.println(source);
    final JsonSchema targetSchema = StructrSchema.createFromSource(sourceSchema.toString());
    final String target = targetSchema.toString();
    System.out.println("##################### target");
    System.out.println(target);
    assertEquals("Invalid schema (de)serialization roundtrip result", source, target);
    StructrSchema.replaceDatabaseSchema(app, targetSchema);
    final JsonSchema replacedSchema = StructrSchema.createFromDatabase(app);
    final String replaced = replacedSchema.toString();
    System.out.println("##################### replaced");
    System.out.println(replaced);
    assertEquals("Invalid schema replacement result", source, replaced);
}
Also used : JsonSchema(org.structr.schema.json.JsonSchema)

Example 17 with JsonSchema

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

the class StructrGraphQLTest method createTestUserType.

protected Class createTestUserType() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        final JsonType type = schema.addType("TestUser");
        type.setExtends(URI.create("#/definitions/Principal"));
        type.overrideMethod("onCreate", true, "setProperty(name, \"test\" + System.currentTimeMillis());");
        StructrSchema.replaceDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    return StructrApp.getConfiguration().getNodeEntityClass("TestUser");
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema)

Example 18 with JsonSchema

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

the class StructrRestTest method createTestUserType.

protected Class createTestUserType() {
    try (final Tx tx = app.tx()) {
        final JsonSchema schema = StructrSchema.createFromDatabase(app);
        final JsonType type = schema.addType("TestUser");
        type.setExtends(URI.create("#/definitions/Principal"));
        type.overrideMethod("onCreate", true, "setProperty(name, \"test\" + System.currentTimeMillis());");
        StructrSchema.replaceDatabaseSchema(app, schema);
        tx.success();
    } catch (Throwable fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    return StructrApp.getConfiguration().getNodeEntityClass("TestUser");
}
Also used : JsonType(org.structr.schema.json.JsonType) Tx(org.structr.core.graph.Tx) JsonSchema(org.structr.schema.json.JsonSchema)

Example 19 with JsonSchema

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

the class CsvImportTest method testCsvFileImportSingleQuotes.

@Test
public void testCsvFileImportSingleQuotes() {
    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 20 with JsonSchema

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

the class CsvImportTest method testCsvFileImportNoQuotes.

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

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