@Test public void testCopyFileOverwrite() throws Exception { String directoryPath = "/testCopyFile/directory/path" + System.currentTimeMillis(); String sourcePath = directoryPath + "/source.txt"; String destName = "destination.txt"; String destPath = directoryPath + "/" + destName; createDirectory(directoryPath); createFile(sourcePath, "This is the contents"); createFile(destPath, "Original file"); // with no-overwrite, copy should fail JSONObject requestObject = new JSONObject(); addSourceLocation(requestObject, sourcePath); WebRequest request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt"); request.setHeaderField("X-Create-Options", "copy,no-overwrite"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_PRECON_FAILED, response.getResponseCode()); // now omit no-overwrite and copy should succeed and return 200 instead of 201 request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt"); request.setHeaderField("X-Create-Options", "copy"); response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject responseObject = new JSONObject(response.getText()); checkFileMetadata(responseObject, destName, null, null, null, null, null, null, null); assertTrue(checkFileExists(sourcePath)); assertTrue(checkFileExists(destPath)); }
@Test public void testReadDirectoryChildren() throws CoreException, IOException, SAXException, JSONException { String dirName = "path" + System.currentTimeMillis(); String directoryPath = "sample/directory/" + dirName; createDirectory(directoryPath); String subDirectory = "subdirectory"; createDirectory(directoryPath + "/" + subDirectory); String subFile = "subfile.txt"; createFile(directoryPath + "/" + subFile, "Sample file"); WebRequest request = getGetFilesRequest(directoryPath + "?depth=1"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); List<JSONObject> children = getDirectoryChildren(new JSONObject(response.getText())); assertEquals("Wrong number of directory children", 2, children.size()); for (JSONObject child : children) { if (child.getBoolean("Directory")) { checkDirectoryMetadata(child, subDirectory, null, null, null, null, null); } else { checkFileMetadata(child, subFile, null, null, null, null, null, null, null); } } }
@Test public void testCopyFileInvalidSource() throws Exception { String directoryPath = "/testCopyFile/directory/path" + System.currentTimeMillis(); createDirectory(directoryPath); JSONObject requestObject = new JSONObject(); requestObject.put("Location", "/this/does/not/exist/at/all"); WebRequest request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt"); request.setHeaderField("X-Create-Options", "copy"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode()); JSONObject responseObject = new JSONObject(response.getText()); assertEquals("Error", responseObject.get("Severity")); }
@Test public void testCopyFileNoOverwrite() throws Exception { String directoryPath = "/testCopyFile/directory/path" + System.currentTimeMillis(); String sourcePath = directoryPath + "/source.txt"; String destName = "destination.txt"; String destPath = directoryPath + "/" + destName; createDirectory(directoryPath); createFile(sourcePath, "This is the contents"); JSONObject requestObject = new JSONObject(); addSourceLocation(requestObject, sourcePath); WebRequest request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt"); request.setHeaderField("X-Create-Options", "copy"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode()); JSONObject responseObject = new JSONObject(response.getText()); checkFileMetadata(responseObject, destName, null, null, null, null, null, null, null); assertTrue(checkFileExists(sourcePath)); assertTrue(checkFileExists(destPath)); }
@Test public void testReadFileMetadata() throws Exception { String directoryPath = "sample/directory/path" + System.currentTimeMillis(); createDirectory(directoryPath); String fileName = "sampleFile" + System.currentTimeMillis() + ".txt"; String fileContent = "Sample File Cotnent " + System.currentTimeMillis(); createFile(directoryPath + "/" + fileName, fileContent); WebRequest request = getGetFilesRequest(directoryPath + "/" + fileName + "?parts=meta"); WebResponse response = webConversation.getResponse(request); assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode()); JSONObject result = new JSONObject(response.getText()); assertEquals(fileName, result.optString(ProtocolConstants.KEY_NAME)); JSONArray parents = result.optJSONArray(ProtocolConstants.KEY_PARENTS); assertNotNull(parents); assertEquals(3, parents.length()); IPath parentPath = new Path(directoryPath); // immediate parent JSONObject parent = parents.getJSONObject(0); assertEquals(parentPath.segment(2), parent.getString(ProtocolConstants.KEY_NAME)); // grandparent parent = parents.getJSONObject(1); assertEquals(parentPath.segment(1), parent.getString(ProtocolConstants.KEY_NAME)); // ensure all parent locations end with trailing slash for (int i = 0; i < parents.length(); i++) { parent = parents.getJSONObject(i); String location = parent.getString(ProtocolConstants.KEY_LOCATION); assertTrue(location.endsWith("/")); location = parent.getString(ProtocolConstants.KEY_CHILDREN_LOCATION); URI childrenLocation = new URI(location); assertTrue(childrenLocation.getPath().endsWith("/")); } }
private void addSourceLocation(JSONObject requestObject, String sourcePath) throws JSONException { requestObject.put( "Location", new Path(FileSystemTest.FILE_SERVLET_LOCATION).append(sourcePath)); }