Search in sources :

Example 1 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class AdvancedSchemaTest method test04SchemaPropertyOrderInCustomViews.

@Test
public void test04SchemaPropertyOrderInCustomViews() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    final GenericProperty jsonName = new GenericProperty("jsonName");
    SchemaView testView = null;
    String id = null;
    try (final Tx tx = app.tx()) {
        // create test type
        final SchemaNode test = app.create(SchemaNode.class, "Test");
        // create view with sort order
        testView = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "test"), new NodeAttribute<>(SchemaView.schemaNode, test), new NodeAttribute<>(SchemaView.sortOrder, "one, two, three, four, id, type, name"), new NodeAttribute<>(SchemaView.nonGraphProperties, "id, type, name"));
        final List<SchemaView> list = new LinkedList<>();
        list.add(testView);
        // create properties
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "four"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
    final List<PropertyKey> list = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "test"));
    Assert.assertEquals("Invalid number of properties in sorted view", 7, list.size());
    Assert.assertEquals("one", list.get(0).dbName());
    Assert.assertEquals("two", list.get(1).dbName());
    Assert.assertEquals("three", list.get(2).dbName());
    Assert.assertEquals("four", list.get(3).dbName());
    Assert.assertEquals("id", list.get(4).dbName());
    Assert.assertEquals("type", list.get(5).dbName());
    Assert.assertEquals("name", list.get(6).dbName());
    try (final Tx tx = app.tx()) {
        // modify sort order
        testView.setProperty(SchemaView.sortOrder, "type, one, id, two, three, four, name");
        // create test entity
        final NodeInterface node = app.create(StructrApp.getConfiguration().getNodeEntityClass("Test"));
        id = node.getUuid();
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "test"));
    Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
    Assert.assertEquals("type", list2.get(0).dbName());
    Assert.assertEquals("one", list2.get(1).dbName());
    Assert.assertEquals("id", list2.get(2).dbName());
    Assert.assertEquals("two", list2.get(3).dbName());
    Assert.assertEquals("three", list2.get(4).dbName());
    Assert.assertEquals("four", list2.get(5).dbName());
    Assert.assertEquals("name", list2.get(6).dbName());
    // test schema resource
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(7)).body("result[0].jsonName", equalTo("type")).body("result[1].jsonName", equalTo("one")).body("result[2].jsonName", equalTo("id")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).body("result[6].jsonName", equalTo("name")).when().get("/_schema/Test/test");
    // test actual REST resource (not easy to extract and verify
    // JSON property order, that's why we're using replaceAll and
    // string comparison..
    final String[] actual = RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).when().get("/Test/test").body().asString().replaceAll("[\\s]+", "").split("[\\W]+");
    // we can only test the actual ORDER of the JSON result object by splitting it on whitespace and validating the resulting array
    assertEquals("Invalid JSON result for sorted property view", "", actual[0]);
    assertEquals("Invalid JSON result for sorted property view", "query_time", actual[1]);
    assertEquals("Invalid JSON result for sorted property view", "0", actual[2]);
    assertEquals("Invalid JSON result for sorted property view", "result_count", actual[4]);
    assertEquals("Invalid JSON result for sorted property view", "1", actual[5]);
    assertEquals("Invalid JSON result for sorted property view", "result", actual[6]);
    assertEquals("Invalid JSON result for sorted property view", "type", actual[7]);
    assertEquals("Invalid JSON result for sorted property view", "Test", actual[8]);
    assertEquals("Invalid JSON result for sorted property view", "one", actual[9]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[10]);
    assertEquals("Invalid JSON result for sorted property view", "id", actual[11]);
    assertEquals("Invalid JSON result for sorted property view", id, actual[12]);
    assertEquals("Invalid JSON result for sorted property view", "two", actual[13]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[14]);
    assertEquals("Invalid JSON result for sorted property view", "three", actual[15]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[16]);
    assertEquals("Invalid JSON result for sorted property view", "four", actual[17]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[18]);
    assertEquals("Invalid JSON result for sorted property view", "name", actual[19]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[20]);
    assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[21]);
    // try built-in function
    try {
        final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "test" });
        Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
        Assert.assertEquals("type", list3.get(0).get(jsonName));
        Assert.assertEquals("one", list3.get(1).get(jsonName));
        Assert.assertEquals("id", list3.get(2).get(jsonName));
        Assert.assertEquals("two", list3.get(3).get(jsonName));
        Assert.assertEquals("three", list3.get(4).get(jsonName));
        Assert.assertEquals("four", list3.get(5).get(jsonName));
        Assert.assertEquals("name", list3.get(6).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    // try scripting call
    try {
        final List<GraphObjectMap> list4 = (List) Scripting.evaluate(new ActionContext(securityContext), null, "${type_info('Test', 'test')}", "test");
        Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
        Assert.assertEquals("type", list4.get(0).get(jsonName));
        Assert.assertEquals("one", list4.get(1).get(jsonName));
        Assert.assertEquals("id", list4.get(2).get(jsonName));
        Assert.assertEquals("two", list4.get(3).get(jsonName));
        Assert.assertEquals("three", list4.get(4).get(jsonName));
        Assert.assertEquals("four", list4.get(5).get(jsonName));
        Assert.assertEquals("name", list4.get(6).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) SchemaNode(org.structr.core.entity.SchemaNode) TypeInfoFunction(org.structr.core.function.TypeInfoFunction) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) LinkedList(java.util.LinkedList) List(java.util.List) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 2 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class AdvancedSchemaTest method test03InheritanceOfFileAttributesToSubclassOfImage.

@Test
public void test03InheritanceOfFileAttributesToSubclassOfImage() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, fileNodeDef);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Create new schema node for dynamic class SubFile which extends File
        SchemaNode subFile = app.create(SchemaNode.class);
        final PropertyMap subFileProperties = new PropertyMap();
        subFileProperties.put(SchemaNode.name, "SubFile");
        subFileProperties.put(SchemaNode.extendsClass, "org.structr.dynamic.Image");
        subFile.setProperties(subFile.getSecurityContext(), subFileProperties);
        // Add String property "testSubFile" to new dynamic class
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testSubFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, subFile);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(count2 + 1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testSubFile"), hasEntry("declaringClass", "SubFile")))).when().get("/_schema/SubFile/ui");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 3 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class AdvancedSchemaTest method testSchemaPropertyOrderInInheritedViews.

@Test
public void testSchemaPropertyOrderInInheritedViews() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    final GenericProperty jsonName = new GenericProperty("jsonName");
    SchemaView testView = null;
    String id = null;
    try (final Tx tx = app.tx()) {
        final SchemaNode testBase = app.create(SchemaNode.class, "TestBase");
        final SchemaNode test = app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "Test"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.TestBase"));
        // create view with sort order
        testView = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.name, "test"), new NodeAttribute<>(SchemaView.schemaNode, test), new NodeAttribute<>(SchemaView.sortOrder, "one, two, three, four, id, type, name"), new NodeAttribute<>(SchemaView.nonGraphProperties, "id, type, name"));
        final List<SchemaView> list = new LinkedList<>();
        list.add(testView);
        // create properties
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, testBase), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "four"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
    final List<PropertyKey> list = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "test"));
    Assert.assertEquals("Invalid number of properties in sorted view", 7, list.size());
    Assert.assertEquals("one", list.get(0).dbName());
    Assert.assertEquals("two", list.get(1).dbName());
    Assert.assertEquals("three", list.get(2).dbName());
    Assert.assertEquals("four", list.get(3).dbName());
    Assert.assertEquals("id", list.get(4).dbName());
    Assert.assertEquals("type", list.get(5).dbName());
    Assert.assertEquals("name", list.get(6).dbName());
    try (final Tx tx = app.tx()) {
        // modify sort order
        testView.setProperty(SchemaView.sortOrder, "type, one, id, two, three, four, name");
        // create test entity
        final NodeInterface node = app.create(StructrApp.getConfiguration().getNodeEntityClass("Test"));
        id = node.getUuid();
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "test"));
    Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
    Assert.assertEquals("type", list2.get(0).dbName());
    Assert.assertEquals("one", list2.get(1).dbName());
    Assert.assertEquals("id", list2.get(2).dbName());
    Assert.assertEquals("two", list2.get(3).dbName());
    Assert.assertEquals("three", list2.get(4).dbName());
    Assert.assertEquals("four", list2.get(5).dbName());
    Assert.assertEquals("name", list2.get(6).dbName());
    // test schema resource
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(7)).body("result[0].jsonName", equalTo("type")).body("result[1].jsonName", equalTo("one")).body("result[2].jsonName", equalTo("id")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).body("result[6].jsonName", equalTo("name")).when().get("/_schema/Test/test");
    // test actual REST resource (not easy to extract and verify
    // JSON property order, that's why we're using replaceAll and
    // string comparison..
    final String[] actual = RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).when().get("/Test/test").body().asString().replaceAll("[\\s]+", "").split("[\\W]+");
    // we can only test the actual ORDER of the JSON result object by splitting it on whitespace and validating the resulting array
    assertEquals("Invalid JSON result for sorted property view", "", actual[0]);
    assertEquals("Invalid JSON result for sorted property view", "query_time", actual[1]);
    assertEquals("Invalid JSON result for sorted property view", "0", actual[2]);
    assertEquals("Invalid JSON result for sorted property view", "result_count", actual[4]);
    assertEquals("Invalid JSON result for sorted property view", "1", actual[5]);
    assertEquals("Invalid JSON result for sorted property view", "result", actual[6]);
    assertEquals("Invalid JSON result for sorted property view", "type", actual[7]);
    assertEquals("Invalid JSON result for sorted property view", "Test", actual[8]);
    assertEquals("Invalid JSON result for sorted property view", "one", actual[9]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[10]);
    assertEquals("Invalid JSON result for sorted property view", "id", actual[11]);
    assertEquals("Invalid JSON result for sorted property view", id, actual[12]);
    assertEquals("Invalid JSON result for sorted property view", "two", actual[13]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[14]);
    assertEquals("Invalid JSON result for sorted property view", "three", actual[15]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[16]);
    assertEquals("Invalid JSON result for sorted property view", "four", actual[17]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[18]);
    assertEquals("Invalid JSON result for sorted property view", "name", actual[19]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[20]);
    assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[21]);
    // try built-in function
    try {
        final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "test" });
        Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
        Assert.assertEquals("type", list3.get(0).get(jsonName));
        Assert.assertEquals("one", list3.get(1).get(jsonName));
        Assert.assertEquals("id", list3.get(2).get(jsonName));
        Assert.assertEquals("two", list3.get(3).get(jsonName));
        Assert.assertEquals("three", list3.get(4).get(jsonName));
        Assert.assertEquals("four", list3.get(5).get(jsonName));
        Assert.assertEquals("name", list3.get(6).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    // try scripting call
    try {
        final List<GraphObjectMap> list4 = (List) Scripting.evaluate(new ActionContext(securityContext), null, "${type_info('Test', 'test')}", "test");
        Assert.assertEquals("Invalid number of properties in sorted view", 7, list2.size());
        Assert.assertEquals("type", list4.get(0).get(jsonName));
        Assert.assertEquals("one", list4.get(1).get(jsonName));
        Assert.assertEquals("id", list4.get(2).get(jsonName));
        Assert.assertEquals("two", list4.get(3).get(jsonName));
        Assert.assertEquals("three", list4.get(4).get(jsonName));
        Assert.assertEquals("four", list4.get(5).get(jsonName));
        Assert.assertEquals("name", list4.get(6).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) SchemaNode(org.structr.core.entity.SchemaNode) TypeInfoFunction(org.structr.core.function.TypeInfoFunction) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) LinkedList(java.util.LinkedList) List(java.util.List) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 4 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class AdvancedSchemaTest method test02InheritanceOfFileAttributesToSubclass.

@Test
public void test02InheritanceOfFileAttributesToSubclass() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap changedProperties = new PropertyMap();
        changedProperties.put(SchemaProperty.name, "testFile");
        changedProperties.put(SchemaProperty.propertyType, "String");
        changedProperties.put(SchemaProperty.schemaNode, fileNodeDef);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), changedProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Create new schema node for dynamic class SubFile which extends File
        SchemaNode subFile = app.create(SchemaNode.class);
        final PropertyMap subFileProperties = new PropertyMap();
        subFileProperties.put(SchemaNode.name, "SubFile");
        subFileProperties.put(SchemaNode.extendsClass, "org.structr.dynamic.File");
        subFile.setProperties(subFile.getSecurityContext(), subFileProperties);
        // Add String property "testSubFile" to new dynamic class
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testSubFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, subFile);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(count1 + 1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testSubFile"), hasEntry("declaringClass", "SubFile")))).when().get("/_schema/SubFile/ui");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 5 with SchemaNode

use of org.structr.core.entity.SchemaNode in project structr by structr.

the class RenderContextTest method testVariableReplacementInDynamicTypes.

@Test
public void testVariableReplacementInDynamicTypes() {
    SchemaNode itemNode = null;
    NodeInterface parent = null;
    NodeInterface child1 = null;
    NodeInterface child2 = null;
    try (final Tx tx = app.tx()) {
        itemNode = app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"));
        final PropertyMap properties = new PropertyMap();
        properties.put(SchemaRelationshipNode.sourceId, itemNode.getUuid());
        properties.put(SchemaRelationshipNode.targetId, itemNode.getUuid());
        properties.put(SchemaRelationshipNode.relationshipType, "CHILD");
        properties.put(SchemaRelationshipNode.sourceMultiplicity, "1");
        properties.put(SchemaRelationshipNode.targetMultiplicity, "*");
        properties.put(SchemaRelationshipNode.sourceJsonName, "parentItem");
        properties.put(SchemaRelationshipNode.targetJsonName, "children");
        app.create(SchemaRelationshipNode.class, properties);
        // compile the stuff
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final Class itemClass = config.getNodeEntityClass("Item");
    final PropertyKey childrenProperty = StructrApp.key(itemClass, "children");
    // create parent/child relationship
    try (final Tx tx = app.tx()) {
        parent = app.create(itemClass);
        child1 = app.create(itemClass);
        child2 = app.create(itemClass);
        final List<NodeInterface> children = new LinkedList<>();
        children.add(child1);
        children.add(child2);
        parent.setProperties(parent.getSecurityContext(), new PropertyMap(childrenProperty, children));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // verify that parent has two children
    try (final Tx tx = app.tx()) {
        // verify that parentItem can be accessed....
        final Object value = parent.getProperty(childrenProperty);
        assertTrue(value instanceof Collection);
        final Collection coll = (Collection) value;
        assertEquals(2, coll.size());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    // check property access in template expressions
    try (final Tx tx = app.tx()) {
        assertEquals(parent.toString(), Scripting.replaceVariables(new ActionContext(securityContext), child1, "${this.parentItem}"));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) ActionContext(org.structr.schema.action.ActionContext) LinkedList(java.util.LinkedList) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) Collection(java.util.Collection) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

SchemaNode (org.structr.core.entity.SchemaNode)54 Tx (org.structr.core.graph.Tx)44 FrameworkException (org.structr.common.error.FrameworkException)41 Test (org.junit.Test)34 App (org.structr.core.app.App)18 StructrApp (org.structr.core.app.StructrApp)18 NodeAttribute (org.structr.core.graph.NodeAttribute)14 LinkedList (java.util.LinkedList)13 PropertyMap (org.structr.core.property.PropertyMap)13 NodeInterface (org.structr.core.graph.NodeInterface)12 PropertyKey (org.structr.core.property.PropertyKey)11 StructrTest (org.structr.common.StructrTest)10 SchemaMethod (org.structr.core.entity.SchemaMethod)10 SchemaRelationshipNode (org.structr.core.entity.SchemaRelationshipNode)9 StringProperty (org.structr.core.property.StringProperty)9 List (java.util.List)8 ConfigurationProvider (org.structr.schema.ConfigurationProvider)8 SchemaProperty (org.structr.core.entity.SchemaProperty)7 LinkedHashSet (java.util.LinkedHashSet)6 FrontendTest (org.structr.web.basic.FrontendTest)6