Search in sources :

Example 36 with FrameworkException

use of org.structr.common.error.FrameworkException 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 37 with FrameworkException

use of org.structr.common.error.FrameworkException 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 38 with FrameworkException

use of org.structr.common.error.FrameworkException 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 39 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class CacheTest method testCaching.

@Test
public void testCaching() {
    try {
        Thread.sleep(2000);
    } catch (Throwable t) {
    }
    final ExecutorService service = Executors.newCachedThreadPool();
    final Queue<TestTwo> queue = new ConcurrentLinkedQueue<>();
    final AtomicBoolean doRun = new AtomicBoolean(true);
    service.submit(() -> {
        // caching layer.
        while (doRun.get()) {
            final TestTwo obj = queue.poll();
            if (obj != null) {
                try (final Tx tx = app.tx()) {
                    obj.getProperty(TestTwo.testFives);
                    tx.success();
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        }
    });
    // create an object in a transaction and hand it over
    // to a different thread right afterwards, and add
    // relationships in the original thread
    final int num = 10;
    final int count = 10;
    for (int i = 0; i < num; i++) {
        TestTwo obj = null;
        try (final Tx tx = app.tx()) {
            // create test object
            obj = app.create(TestTwo.class, "testTwo" + i);
            // add related nodes
            for (int j = 0; j < count; j++) {
                final TestFive tmp = app.create(TestFive.class, new NodeAttribute<>(TestFive.testTwo, obj));
            }
            queue.add(obj);
            tx.success();
        } catch (FrameworkException fex) {
            fex.printStackTrace();
        }
        try (final Tx tx = app.tx()) {
            final List<TestFive> testFives = obj.getProperty(TestTwo.testFives);
            final int size = testFives.size();
            if (size != count) {
                System.out.println("Leaking cache detected, collection has wrong number of elements: " + count + " vs. " + size);
            }
            // do not fail this test for now
            // assertEquals("Leaking cache detected, collection has wrong number of elements", count, size);
            tx.success();
        } catch (FrameworkException fex) {
            fex.printStackTrace();
        }
    }
    // stop worker thread
    doRun.set(false);
    service.shutdown();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestTwo(org.structr.web.entity.TestTwo) ExecutorService(java.util.concurrent.ExecutorService) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) TestFive(org.structr.web.entity.TestFive) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 40 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class RendererTest method testMarkdownRenderer.

@Test
public void testMarkdownRenderer() {
    Content content = null;
    try (final Tx tx = app.tx()) {
        createTestNode(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 Page page1 = Page.createSimplePage(securityContext, "page1");
        final Element div = (Element) page1.getElementsByTagName("div").item(0);
        content = (Content) div.getFirstChild();
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
        logger.warn("", fex);
    }
    try (final Tx tx = app.tx()) {
        // test markdown content
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/markdown");
        content.setProperty(StructrApp.key(Content.class, "content"), "# Title\n" + "This is a test\n\n" + "## Another title\n");
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
        logger.warn("", fex);
    }
    RestAssured.basePath = "/";
    // test successful basic auth
    RestAssured.given().header("X-User", "admin").header("X-Password", "admin").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).expect().statusCode(200).body("html.head.title", Matchers.equalTo("Page1")).body("html.body.h1", Matchers.equalTo("Page1")).body("html.body.div.h1.a", Matchers.equalTo("Title")).body("html.body.div.p", Matchers.equalTo("This is a test")).body("html.body.div.h2.a", Matchers.equalTo("Another title")).when().get("/html/page1");
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Content(org.structr.web.entity.dom.Content) Element(org.w3c.dom.Element) Page(org.structr.web.entity.dom.Page) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)892 Tx (org.structr.core.graph.Tx)673 Test (org.junit.Test)450 App (org.structr.core.app.App)175 StructrApp (org.structr.core.app.StructrApp)174 StructrUiTest (org.structr.web.StructrUiTest)134 NodeInterface (org.structr.core.graph.NodeInterface)121 StructrTest (org.structr.common.StructrTest)118 PropertyKey (org.structr.core.property.PropertyKey)109 PropertyMap (org.structr.core.property.PropertyMap)105 IOException (java.io.IOException)96 GraphObject (org.structr.core.GraphObject)93 TestOne (org.structr.core.entity.TestOne)92 File (org.structr.web.entity.File)85 SecurityContext (org.structr.common.SecurityContext)78 Principal (org.structr.core.entity.Principal)69 Page (org.structr.web.entity.dom.Page)69 LinkedList (java.util.LinkedList)62 Folder (org.structr.web.entity.Folder)60 NodeAttribute (org.structr.core.graph.NodeAttribute)56