use of org.structr.web.entity.Folder in project structr by structr.
the class DirectFileImportTest method testDirectFileImportWithAbsoluteDirectory.
@Test
public void testDirectFileImportWithAbsoluteDirectory() {
final PropertyKey<String> pathKey = StructrApp.key(File.class, "path");
Path testDir = null;
String importPath = null;
try {
testDir = Files.createTempDirectory(Paths.get(basePath), "directFileImportTestAbsoluteDirectory");
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");
importPath = testDir.toString();
} catch (IOException ioex) {
fail("Unable to create test files.");
}
// do import
final Map<String, Object> attributes = new LinkedHashMap<>();
attributes.put("source", importPath);
try (final Tx tx = app.tx()) {
app.command(DirectFileImportCommand.class).execute(attributes);
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify successful file import
try (final Tx tx = app.tx()) {
assertNotNull("Folder should have been created by import", app.nodeQuery(Folder.class).and(pathKey, "/" + testDir.getFileName().toString()).getFirst());
final File file1 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test1.txt").getFirst();
final File file2 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test2.txt").getFirst();
final File file3 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/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.");
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class DirectFileImportTest method testDirectFileImportWithRelativeDirectory.
@Test
public void testDirectFileImportWithRelativeDirectory() {
final PropertyKey<String> pathKey = StructrApp.key(File.class, "path");
final String dirName = "directFileImportTestRelativeDirectory";
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.");
}
// do import
try (final Tx tx = app.tx()) {
app.command(DirectFileImportCommand.class).execute(setupParameters(dirName, "/", "COPY", "OVERWRITE", false));
tx.success();
} catch (FrameworkException ex) {
ex.printStackTrace();
fail("Unexpected exception.");
}
// verify successful file import
try (final Tx tx = app.tx()) {
assertNotNull("Folder should have been created by import", app.nodeQuery(Folder.class).and(pathKey, "/" + testDir.getFileName().toString()).getFirst());
final File file1 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test1.txt").getFirst();
final File file2 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/test2.txt").getFirst();
final File file3 = app.nodeQuery(File.class).and(pathKey, "/" + testDir.getFileName().toString() + "/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.");
}
}
use of org.structr.web.entity.Folder 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();
}
}
}
use of org.structr.web.entity.Folder 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.");
}
}
use of org.structr.web.entity.Folder in project structr by structr.
the class ResourceAccessTest method test03ResourceAccessPUT.
@Test
public void test03ResourceAccessPUT() {
// clear resource access objects that are created by the dynamic schema
clearResourceAccess();
final String name = "testuser-01";
final String password = "testpassword-01";
ResourceAccess folderGrant = null;
User testUser = null;
Folder testFolder = null;
try (final Tx tx = app.tx()) {
testUser = createTestNodes(User.class, 1).get(0);
testFolder = createTestNodes(Folder.class, 1).get(0);
assertNotNull(testFolder);
// no resource access node at all => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
folderGrant = createResourceAccess("Folder", UiAuthenticator.FORBIDDEN);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// resource access explicitly set to FORBIDDEN => forbidden
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
// allow PUT for authenticated users => access without user/pass should be still forbidden
folderGrant.setFlag(UiAuthenticator.AUTH_USER_PUT);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(401).when().put("/folder/" + testFolder.getUuid());
// allow PUT for non-authenticated users =>
folderGrant.setProperties(folderGrant.getSecurityContext(), new PropertyMap(GraphObject.visibleToPublicUsers, true));
folderGrant.setFlag(UiAuthenticator.NON_AUTH_USER_PUT);
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
// ownerless non-public node cannot be found by anonymous user
RestAssured.given().contentType("application/json; charset=UTF-8").expect().statusCode(404).when().put("/folder/" + testFolder.getUuid());
// Prepare for next test
final PropertyMap testUserProperties = new PropertyMap();
testUserProperties.put(StructrApp.key(User.class, "name"), name);
testUserProperties.put(StructrApp.key(User.class, "password"), password);
testUser.setProperties(testUser.getSecurityContext(), testUserProperties);
// now we give the user ownership and expect a 200
testFolder.setProperties(testFolder.getSecurityContext(), new PropertyMap(AbstractNode.owner, testUser));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
logger.error(fex.toString());
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
RestAssured.given().headers("X-User", name, "X-Password", password).contentType("application/json; charset=UTF-8").expect().statusCode(200).when().put("/folder/" + testFolder.getUuid());
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
logger.error(ex.toString());
fail("Unexpected exception");
}
}
Aggregations