Search in sources :

Example 66 with FrameworkException

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

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

Example 68 with FrameworkException

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

the class UiTest method testAutoRenameFileWithIdenticalPathInSubFolder.

@Test
public void testAutoRenameFileWithIdenticalPathInSubFolder() {
    Settings.UniquePaths.setValue(Boolean.TRUE);
    Folder folder = null;
    File file1 = null;
    File file2 = null;
    try (final Tx tx = app.tx()) {
        folder = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), "/my/test/folder");
        assertNotNull(folder);
        assertEquals(folder.getPath(), "/my/test/folder");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        file1 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"), new NodeAttribute<>(StructrApp.key(AbstractFile.class, "parent"), folder));
        assertNotNull(file1);
        assertEquals("Testfolder should have exactly one child", 1, Iterables.count(folder.getChildren()));
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        file2 = app.create(File.class, new NodeAttribute<>(AbstractNode.name, "test.txt"), new NodeAttribute<>(StructrApp.key(AbstractFile.class, "parent"), folder));
        assertNotNull(file2);
        assertEquals("Testfolder should have exactly two children", 2, Iterables.count(folder.getChildren()));
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    assertNotEquals(file1.getName(), file2.getName());
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) AbstractFile(org.structr.web.entity.AbstractFile) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Folder(org.structr.web.entity.Folder) AbstractFile(org.structr.web.entity.AbstractFile) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 69 with FrameworkException

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

the class XPathTest method testSimpleXPath.

@Test
public void testSimpleXPath() {
    final String pageName = "page-01";
    try (final Tx tx = app.tx()) {
        Page page = Page.createNewPage(securityContext, pageName);
        assertTrue(page != null);
        assertTrue(page instanceof Page);
        DOMElement html = (DOMElement) page.createElement("html");
        DOMElement head = (DOMElement) page.createElement("head");
        DOMElement body = (DOMElement) page.createElement("body");
        DOMElement title = (DOMElement) page.createElement("title");
        DOMElement h1 = (DOMElement) page.createElement("h1");
        try {
            // add HTML element to page
            page.appendChild(html);
            // add HEAD and BODY elements to HTML
            html.appendChild(head);
            html.appendChild(body);
            // add TITLE element to HEAD
            head.appendChild(title);
            title.appendChild(page.createTextNode("Test Page"));
            // add H1 element to BODY
            body.appendChild(h1);
            h1.appendChild(page.createTextNode("Page Title"));
        } catch (DOMException dex) {
            throw new FrameworkException(422, dex.getMessage());
        }
        assertEquals(html, page.getChildNodes().item(1));
        assertEquals(head, html.getChildNodes().item(0));
        assertEquals(body, html.getChildNodes().item(1));
        // test XPath support of structr nodes..
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        // let xpath cache first..
        assertEquals("Page Title", xpath.evaluate("/html/body/h1/text()", page, XPathConstants.STRING));
        assertEquals(h1, xpath.evaluate("/html/body/h1", page, XPathConstants.NODE));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    } catch (XPathExpressionException xpeex) {
        logger.warn("", xpeex);
        fail("Unexpected exception");
    }
}
Also used : XPath(javax.xml.xpath.XPath) DOMException(org.w3c.dom.DOMException) XPathFactory(javax.xml.xpath.XPathFactory) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Page(org.structr.web.entity.dom.Page) DOMElement(org.structr.web.entity.dom.DOMElement) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 70 with FrameworkException

use of org.structr.common.error.FrameworkException 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);
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) JsonSchema(org.structr.schema.json.JsonSchema) URISyntaxException(java.net.URISyntaxException) JsonObjectType(org.structr.schema.json.JsonObjectType) 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