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);
}
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");
}
}
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();
}
}
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");
}
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);
}
Aggregations