Search in sources :

Example 31 with FrameworkException

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

the class DirectFileImportTest method testDirectFileImportWithExistingFileCopyRename.

@Test
public void testDirectFileImportWithExistingFileCopyRename() {
    try (final Tx tx = app.tx()) {
        FileHelper.createFile(securityContext, "initial content".getBytes("utf-8"), "text/plain", File.class, "test.txt");
        tx.success();
    } catch (FrameworkException | IOException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    Path testDir = null;
    Path importPath = null;
    try {
        testDir = Files.createTempDirectory(Paths.get(basePath), "directFileImportTestFileCopyRename");
        importPath = testDir.resolve(Paths.get("test.txt"));
        createTestFile(importPath, "test file content 1");
    } catch (IOException ioex) {
        fail("Unable to create test files.");
    }
    try (final Tx tx = app.tx()) {
        app.command(DirectFileImportCommand.class).execute(setupParameters(importPath.toString(), "/", "COPY", "RENAME", false));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
    // verify successful file import
    try (final Tx tx = app.tx()) {
        final List<File> files = app.nodeQuery(File.class).getAsList();
        // import mode SKIP => no change, no additional file
        assertEquals("Two files should exist after import", 2, files.size());
        final File file1 = files.get(0);
        final File file2 = files.get(1);
        assertNotNull("Test file should have been created by import", file1);
        assertEquals("Test file name should be test.txt", "test.txt", file1.getName());
        assertNull("Test file should NOT have a parent folder", file1.getParent());
        assertEquals("Test file content does not match source", "test file content 1", getContent(file1));
        assertNotNull("Test file should have been created by import", file2);
        assertNotEquals("Existing file should be renamed", "test.txt", file2.getName());
        assertNull("Test file should NOT have a parent folder", file2.getParent());
        assertEquals("Test file content does not match source", "initial content", getContent(file2));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
    // verify source file exists after import
    assertTrue("Source file should not be deleted after import", importPath.toFile().exists());
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) DirectFileImportCommand(org.structr.web.maintenance.DirectFileImportCommand) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 32 with FrameworkException

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

the class DirectFileImportTest method testDirectFileImportWithNonexistingFileCopyRename.

@Test
public void testDirectFileImportWithNonexistingFileCopyRename() {
    Path testDir = null;
    Path importPath = null;
    try {
        testDir = Files.createTempDirectory(Paths.get(basePath), "directFileImportTestFileCopyRename");
        importPath = testDir.resolve(Paths.get("test.txt"));
        createTestFile(importPath, "test file content 1");
    } catch (IOException ioex) {
        fail("Unable to create test files.");
    }
    try (final Tx tx = app.tx()) {
        app.command(DirectFileImportCommand.class).execute(setupParameters(importPath.toString(), "/", "COPY", "RENAME", false));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
    // verify successful file import
    try (final Tx tx = app.tx()) {
        final List<File> files = app.nodeQuery(File.class).getAsList();
        // import mode SKIP => no change, no additional file
        assertEquals("Two files should exist after import", 1, files.size());
        final File file1 = files.get(0);
        assertNotNull("Test file should have been created by import", file1);
        assertEquals("Test file name should be test.txt", "test.txt", file1.getName());
        assertNull("Test file should NOT have a parent folder", file1.getParent());
        assertEquals("Test file content does not match source", "test file content 1", getContent(file1));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
    // verify source file exists after import
    assertTrue("Source file should not be deleted after import", importPath.toFile().exists());
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) DirectFileImportCommand(org.structr.web.maintenance.DirectFileImportCommand) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 33 with FrameworkException

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

the class DirectoryWatchServiceTest method testDisableWatchKeyRegistration.

@Test
public void testDisableWatchKeyRegistration() {
    Path root = null;
    java.io.File file1 = null;
    java.io.File file2 = null;
    java.io.File file3 = null;
    try {
        logger.info("Creating directory to mount..");
        // create some files and folders on disk
        root = Files.createTempDirectory("structr-mount-test");
        root.resolve("parent1/child1/grandchild1").toFile().mkdirs();
        root.resolve("parent2/child1/grandchild1").toFile().mkdirs();
        root.resolve("parent3/child1/grandchild1").toFile().mkdirs();
        logger.info("Creating files to mount..");
        file1 = root.resolve("parent1/child1/grandchild1/test1.txt").toFile();
        file2 = root.resolve("parent2/child1/grandchild1/test2.txt").toFile();
        file3 = root.resolve("parent3/child1/grandchild1/test3.txt").toFile();
        writeFile(file1, "test1 - before change");
        writeFile(file2, "test2 - before change");
        writeFile(file3, "test3 - before change");
        // mount folder
        try (final Tx tx = app.tx()) {
            logger.info("Mounting directory..");
            app.create(Folder.class, new NodeAttribute<>(Folder.name, "mounted3"), new NodeAttribute<>(StructrApp.key(Folder.class, "mountTarget"), root.toString()), new NodeAttribute<>(StructrApp.key(Folder.class, "mountWatchContents"), false));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
        // wait some time
        try {
            Thread.sleep(5000);
        } catch (Throwable t) {
        }
        // check that all files and folders exist
        try (final Tx tx = app.tx()) {
            logger.info("Checking directory..");
            final File check1 = app.nodeQuery(File.class).andName("test1.txt").getFirst();
            final File check2 = app.nodeQuery(File.class).andName("test2.txt").getFirst();
            final File check3 = app.nodeQuery(File.class).andName("test3.txt").getFirst();
            assertEquals("Invalid mount result", "/mounted3/parent1/child1/grandchild1/test1.txt", check1.getPath());
            assertEquals("Invalid mount result", "/mounted3/parent2/child1/grandchild1/test2.txt", check2.getPath());
            assertEquals("Invalid mount result", "/mounted3/parent3/child1/grandchild1/test3.txt", check3.getPath());
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
        // test external changes to files
        writeFile(file2, "test2 - AFTER change");
        // wait some time
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignore) {
        }
        // check that external changes are NOT recorded
        try (final Tx tx = app.tx()) {
            logger.info("Checking directory..");
            final File check2 = app.nodeQuery(File.class).andName("test2.txt").getFirst();
            assertFalse("Invalid checksum of externally modified file", FileHelper.getMD5Checksum(file2).equals(check2.getMd5()));
            assertEquals("Invalid content of externally modified file", "test2 - AFTER change", readFile(check2.getFileOnDisk(false)));
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
        // unmount folder
        try (final Tx tx = app.tx()) {
            logger.info("Unmounting directory..");
            final Folder mounted = app.nodeQuery(Folder.class).and(Folder.name, "mounted3").getFirst();
            mounted.setProperty(StructrApp.key(Folder.class, "mountTarget"), null);
            tx.success();
        } catch (FrameworkException fex) {
            fail("Unexpected exception.");
        }
    } catch (IOException ioex) {
        fail("Unexpected exception.");
    } finally {
        try {
            // cleanup
            Files.walkFileTree(root, new FileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    try {
                        Files.delete(file);
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    try {
                        Files.delete(dir);
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    }
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 34 with FrameworkException

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

the class DirectoryWatchServiceTest method testMountedFolderInRoot.

@Test
public void testMountedFolderInRoot() {
    final String dirName = "mountServiceTest1";
    final Path base = Paths.get(basePath);
    Path testDir = null;
    try {
        testDir = Files.createDirectory(base.resolve(dirName));
        createTestFile(testDir.resolve(Paths.get("test1.txt")), "test file content 1");
        createTestFile(testDir.resolve(Paths.get("test2.txt")), "test file content 2");
        createTestFile(testDir.resolve(Paths.get("test3.txt")), "test file content 3");
    } catch (IOException ioex) {
        fail("Unable to create test files.");
    }
    // mount directory
    try (final Tx tx = app.tx()) {
        app.create(Folder.class, new NodeAttribute<>(Folder.name, "mounted1"), new NodeAttribute<>(StructrApp.key(Folder.class, "mountWatchContents"), true), new NodeAttribute<>(StructrApp.key(Folder.class, "mountTarget"), testDir.toString()));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
    // wait for DirectoryWatchService to start and scan
    try {
        Thread.sleep(5000);
    } catch (InterruptedException ex) {
    }
    // verify mount point
    try (final Tx tx = app.tx()) {
        assertNotNull("Folder should have been created by import", app.nodeQuery(Folder.class).and(StructrApp.key(File.class, "path"), "/mounted1").getFirst());
        final File file1 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/mounted1/test1.txt").getFirst();
        final File file2 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/mounted1/test2.txt").getFirst();
        final File file3 = app.nodeQuery(File.class).and(StructrApp.key(File.class, "path"), "/mounted1/test3.txt").getFirst();
        assertNotNull("Test file should have been created by import", file1);
        assertNotNull("Test file should have been created by import", file2);
        assertNotNull("Test file should have been created by import", file3);
        assertEquals("Imported test file content does not match source", "test file content 1", getContent(file1));
        assertEquals("Imported test file content does not match source", "test file content 2", getContent(file2));
        assertEquals("Imported test file content does not match source", "test file content 3", getContent(file3));
        tx.success();
    } catch (FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception.");
    }
}
Also used : Path(java.nio.file.Path) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) IOException(java.io.IOException) Folder(org.structr.web.entity.Folder) File(org.structr.web.entity.File) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 35 with FrameworkException

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

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