Search in sources :

Example 66 with Tx

use of org.structr.core.graph.Tx in project structr by structr.

the class UiScriptingTest method testRestQueryRepeater.

@Test
public void testRestQueryRepeater() {
    String uuid = null;
    try (final Tx tx = app.tx()) {
        final Page page = Page.createSimplePage(securityContext, "test");
        final Div div = (Div) page.getElementsByTagName("div").item(0);
        final Content content = (Content) div.getFirstChild();
        // setup scripting repeater
        content.setProperty(StructrApp.key(Content.class, "restQuery"), "/Page/${current.id}");
        content.setProperty(StructrApp.key(Content.class, "dataKey"), "test");
        content.setProperty(StructrApp.key(Content.class, "content"), "${test.id}");
        // store UUID for later use
        uuid = page.getUuid();
        // create admin user
        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));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    RestAssured.basePath = "/";
    // test successful basic auth
    RestAssured.given().headers("X-User", "admin", "X-Password", "admin").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body("html.head.title", Matchers.equalTo("Test")).body("html.body.h1", Matchers.equalTo("Test")).body("html.body.div", Matchers.equalTo(uuid)).when().get("/html/test/" + uuid);
}
Also used : Div(org.structr.web.entity.html.Div) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Content(org.structr.web.entity.dom.Content) Page(org.structr.web.entity.dom.Page) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 67 with Tx

use of org.structr.core.graph.Tx in project structr by structr.

the class UiScriptingTest method testSingleRequestParameters.

@Test
public void testSingleRequestParameters() {
    try (final Tx tx = app.tx()) {
        Page page = (Page) app.create(Page.class, new NodeAttribute(Page.name, "test"), new NodeAttribute(Page.visibleToPublicUsers, true));
        Template template = (Template) app.create(Template.class, new NodeAttribute(Page.visibleToPublicUsers, true));
        template.setContent("${request.param}");
        page.appendChild(template);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(401)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(403)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).expect().statusCode(200).body(equalTo("a")).when().get("http://localhost:8875/test?param=a");
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Page(org.structr.web.entity.dom.Page) Template(org.structr.web.entity.dom.Template) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 68 with Tx

use of org.structr.core.graph.Tx in project structr by structr.

the class UiScriptingTest method testGroupFunctions.

@Test
public void testGroupFunctions() {
    Group group = null;
    User tester = null;
    try (final Tx tx = app.tx()) {
        // create test user
        tester = createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "test"));
        // create test group
        group = createTestNode(Group.class, new NodeAttribute<>(StructrApp.key(Group.class, "name"), "test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final RenderContext renderContext = new RenderContext(securityContext, new RequestMockUp(), new ResponseMockUp(), RenderContext.EditMode.NONE);
    try (final Tx tx = app.tx()) {
        // check that the user is not in the group at first
        assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
        // check that is_in_group returns the correct result
        assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
        // add user to group
        Scripting.evaluate(renderContext, null, "${add_to_group(first(find('Group')), first(find('User')))}", "test");
        // check that the user is in the group after the call to add_to_group
        final List<Principal> members = group.getMembers();
        assertTrue("User should be in the test group now", members.contains(tester));
        // check that is_in_group returns the correct result
        assertEquals("Function is_in_group should return true.", true, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
        // remove user from group
        Scripting.evaluate(renderContext, null, "${remove_from_group(first(find('Group')), first(find('User')))}", "test");
        // check that the user is not in the group any more after the call to remove_from_group
        assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
        // check that is_in_group returns the correct result
        assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : Group(org.structr.core.entity.Group) NodeAttribute(org.structr.core.graph.NodeAttribute) RenderContext(org.structr.web.common.RenderContext) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 69 with Tx

use of org.structr.core.graph.Tx in project structr by structr.

the class UiTest method testExtensionBasedMimeTypeDetection.

@Test
public void testExtensionBasedMimeTypeDetection() {
    final Map<String, Map<String, byte[]>> testMap = new LinkedHashMap<>();
    testMap.put("text/html", toMap(new Pair("test.html", "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Test</h1></body></html>".getBytes()), new Pair("test.htm", "<!DOCTYPE html>".getBytes())));
    testMap.put("text/plain", toMap(new Pair("test.txt", "Hello world!".getBytes())));
    testMap.put("text/css", toMap(new Pair("test.css", "body { background-color: #ffffff; }".getBytes())));
    testMap.put("application/javascript", toMap(new Pair("test.js", "function() { alert('Test'); }".getBytes())));
    testMap.put("application/zip", toMap(new Pair("test.zip", "".getBytes())));
    testMap.put("image/jpeg", toMap(new Pair("test.jpg", "".getBytes()), new Pair("test.jpeg", "".getBytes())));
    testMap.put("image/png", toMap(new Pair("test.png", "".getBytes())));
    try (final Tx tx = app.tx()) {
        for (final Entry<String, Map<String, byte[]>> entry : testMap.entrySet()) {
            final String mimeType = entry.getKey();
            for (final Entry<String, byte[]> fileEntry : entry.getValue().entrySet()) {
                final String fileName = fileEntry.getKey();
                final byte[] content = fileEntry.getValue();
                try {
                    final File file = FileHelper.createFile(securityContext, content, null, File.class, fileName);
                    assertEquals("MIME type detection failed", mimeType, file.getContentType());
                } catch (IOException ioex) {
                    logger.warn("", ioex);
                    fail("Unexpected exception");
                }
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 70 with Tx

use of org.structr.core.graph.Tx in project structr by structr.

the class UiTest method testCreateFolder.

@Test
public void testCreateFolder() {
    Folder folder1 = null;
    try (final Tx tx = app.tx()) {
        folder1 = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/folder1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        File file1 = (File) app.create(File.class, "file1");
        assertNotNull(file1);
        assertEquals(file1.getPath(), "/file1");
        file1.setProperty(StructrApp.key(File.class, "parent"), folder1);
        assertEquals(file1.getPath(), "/folder1/file1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        Image image1 = (Image) app.create(Image.class, "image1");
        assertNotNull(image1);
        assertEquals(image1.getPath(), "/image1");
        image1.setProperty(StructrApp.key(File.class, "parent"), folder1);
        assertEquals(image1.getPath(), "/folder1/image1");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        assertEquals(2, Iterables.toList(folder1.getFiles()).size());
        assertEquals(1, Iterables.toList(folder1.getImages()).size());
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) Image(org.structr.web.entity.Image) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

Tx (org.structr.core.graph.Tx)892 FrameworkException (org.structr.common.error.FrameworkException)684 Test (org.junit.Test)461 App (org.structr.core.app.App)201 StructrApp (org.structr.core.app.StructrApp)201 StructrUiTest (org.structr.web.StructrUiTest)139 NodeInterface (org.structr.core.graph.NodeInterface)117 StructrTest (org.structr.common.StructrTest)108 IOException (java.io.IOException)105 PropertyMap (org.structr.core.property.PropertyMap)102 LinkedList (java.util.LinkedList)99 TestOne (org.structr.core.entity.TestOne)98 File (org.structr.web.entity.File)83 Page (org.structr.web.entity.dom.Page)71 Principal (org.structr.core.entity.Principal)65 Folder (org.structr.web.entity.Folder)65 PropertyKey (org.structr.core.property.PropertyKey)62 NodeAttribute (org.structr.core.graph.NodeAttribute)57 SchemaNode (org.structr.core.entity.SchemaNode)45 AbstractFile (org.structr.web.entity.AbstractFile)44