Example #1
0
  @Test
  public void testCreateDirectory() throws CoreException, IOException, SAXException, JSONException {
    String directoryPath = "sample/directory/path" + System.currentTimeMillis();
    createDirectory(directoryPath);

    String dirName = "testdir";
    webConversation.setExceptionsThrownOnErrorStatus(false);

    WebRequest request =
        getPostFilesRequest(directoryPath, getNewDirJSON(dirName).toString(), dirName);
    WebResponse response = webConversation.getResponse(request);

    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    assertTrue(
        "Create directory response was OK, but the directory does not exist",
        checkDirectoryExists(directoryPath + "/" + dirName));
    assertEquals(
        "Response should contain directory metadata in JSON, but was " + response.getText(),
        "application/json",
        response.getContentType());
    JSONObject responseObject = new JSONObject(response.getText());
    assertNotNull("No directory information in response", responseObject);
    checkDirectoryMetadata(responseObject, dirName, null, null, null, null, null);

    // should be able to perform GET on location header to obtain metadata
    String location = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    request = getGetFilesRequest(location);
    response = webConversation.getResource(request);
    assertNotNull(location);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    responseObject = new JSONObject(response.getText());
    assertNotNull("No direcory information in responce", responseObject);
    checkDirectoryMetadata(responseObject, dirName, null, null, null, null, null);
  }
Example #2
0
  @Test
  public void testCreateTopLevelFile()
      throws CoreException, IOException, SAXException, JSONException {
    String directoryPath = "sample" + System.currentTimeMillis();
    createDirectory(directoryPath);
    String fileName = "testfile.txt";

    WebRequest request =
        getPostFilesRequest(directoryPath, getNewFileJSON(fileName).toString(), fileName);
    WebResponse response = webConversation.getResponse(request);

    assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
    assertTrue(
        "Create file response was OK, but the file does not exist",
        checkFileExists(directoryPath + "/" + fileName));
    assertEquals(
        "Response should contain file metadata in JSON, but was " + response.getText(),
        "application/json",
        response.getContentType());
    JSONObject responseObject = new JSONObject(response.getText());
    assertNotNull("No file information in responce", responseObject);
    checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null);

    // should be able to perform GET on location header to obtain metadata
    String location = response.getHeaderField("Location");
    request = getGetFilesRequest(location + "?parts=meta");
    response = webConversation.getResource(request);
    assertNotNull(location);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    responseObject = new JSONObject(response.getText());
    assertNotNull("No direcory information in responce", responseObject);
    checkFileMetadata(responseObject, fileName, null, null, null, null, null, null, null);
  }
Example #3
0
  @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));
  }
Example #4
0
  @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 null value in rows doesn't throw
  public void testTextNull() throws Exception {
    MockStatusAccessor statusAccessor =
        MockStatusAccessor.generateStatusAccessor(colArray1, rowArrayWithNulls);
    statSvc.registerStatusAccessor("testtbl", statusAccessor);

    WebResponse resp = getTable("testtbl", true);
    log.debug(resp.getText());

    List lines = getLines(resp);
    assertEqualTables(tableWithNulls, lines);
  }
  // test special (non-string) key in row doesn't throw
  public void testTextNonStringKey() throws Exception {
    MockStatusAccessor statusAccessor = new MockStatusAccessor();
    List cols = ListUtil.list("foo", StatusTable.ROW_SEPARATOR);
    statusAccessor.setRows(MockStatusAccessor.makeRowsFrom(cols, rowArray1), null);
    statusAccessor.setColumnDescriptors(
        MockStatusAccessor.makeColumnDescriptorsFrom(colArray1), null);

    statSvc.registerStatusAccessor("testtbl", statusAccessor);

    WebResponse resp = getTable("testtbl", true);
    log.debug(resp.getText());
  }
Example #7
0
  @Test
  public void testReadDirectory() throws CoreException, IOException, SAXException, JSONException {
    String dirName = "path" + System.currentTimeMillis();
    String directoryPath = "sample/directory/" + dirName;
    createDirectory(directoryPath);

    WebRequest request = getGetFilesRequest(directoryPath);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    JSONObject dirObject = new JSONObject(response.getText());
    checkDirectoryMetadata(dirObject, dirName, null, null, null, null, null);
  }
Example #8
0
  public void testNoInitialState() throws Exception {
    final String resourceName = "something/interesting";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName, StatefulServlet.class.getName());

    WebRequest request = new GetMethodWebRequest("http://localhost/" + resourceName);
    WebResponse response = sr.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "No session found", response.getText());
    assertEquals("Returned cookie count", 0, response.getNewCookieNames().length);
  }
Example #9
0
  @Test
  public void testReadFileContents() throws CoreException, IOException, SAXException {
    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);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    assertEquals("Invalid file content", fileContent, response.getText());
  }
Example #10
0
  public void testStatePreservation() throws Exception {
    final String resourceName1 = "something/interesting/start";
    final String resourceName2 = "something/continue";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName1, StatefulServlet.class.getName());
    sr.registerServlet(resourceName2, StatefulServlet.class.getName());
    WebClient wc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName1);
    request.setParameter("color", "red");
    WebResponse response = wc.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "You selected red", response.getText());

    request = new GetMethodWebRequest("http://localhost/" + resourceName2);
    response = wc.getResponse(request);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "You posted red", response.getText());
    assertEquals("Returned cookie count", 0, response.getNewCookieNames().length);
  }
Example #11
0
  @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"));
  }
Example #12
0
  public void testInvocationCompletion() throws Exception {
    final String resourceName = "something/interesting";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName, StatefulServlet.class.getName());
    ServletUnitClient suc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName);
    request.setParameter("color", "red");

    InvocationContext ic = suc.newInvocation(request);
    StatefulServlet ss = (StatefulServlet) ic.getServlet();
    ss.setColor(ic.getRequest(), "blue");
    ss.writeSelectMessage("blue", ic.getResponse().getWriter());

    WebResponse response = ic.getServletResponse();
    assertEquals("requested resource", "You selected blue", response.getText());
    assertEquals("Returned cookie count", 1, response.getNewCookieNames().length);
  }
Example #13
0
  public void testText() throws Exception {
    MockStatusAccessor statusAccessor =
        MockStatusAccessor.generateStatusAccessor(colArray1, rowArray1);
    statusAccessor.setTitle("testtbl", null);
    statSvc.registerStatusAccessor("testtbl", statusAccessor);

    WebResponse resp = getTable("testtbl", true);
    assertResponseOk(resp);
    assertEquals("Content type", "text/plain", resp.getContentType());
    log.debug(resp.getText());
    List lines = getLines(resp);
    assertEquals(rowArray1.length + 3, lines.size());
    Map row0 = getRow((String) lines.get(0));
    assertEquals("2.4.6.8", row0.get("host"));

    Map row2 = getRow((String) lines.get(2));
    assertEquals("testtbl", row2.get("table"));

    assertEqualTables(table1, lines);
  }
Example #14
0
 @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));
 }
Example #15
0
  public void testInvocationContextUpdate() throws Exception {
    final String resourceName = "something/interesting";

    ServletRunner sr = new ServletRunner();
    sr.registerServlet(resourceName, StatefulServlet.class.getName());
    ServletUnitClient suc = sr.newClient();

    WebRequest request = new PostMethodWebRequest("http://localhost/" + resourceName);
    request.setParameter("color", "red");

    InvocationContext ic = suc.newInvocation(request);
    StatefulServlet ss = (StatefulServlet) ic.getServlet();
    ss.setColor(ic.getRequest(), "blue");
    suc.getResponse(ic);

    WebResponse response = suc.getResponse("http://localhost/" + resourceName);
    assertNotNull("No response received", response);
    assertEquals("content type", "text/plain", response.getContentType());
    assertEquals("requested resource", "You posted blue", response.getText());
    assertEquals("Returned cookie count", 0, response.getNewCookieNames().length);
  }
Example #16
0
  @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("/"));
    }
  }
Example #17
0
  @Test
  public void testDirectoryDepth() throws CoreException, IOException, SAXException, JSONException {
    String basePath = "sampe/directory/long" + System.currentTimeMillis();
    String longPath = basePath + "/dir1/dir2/dir3/dir4";
    createDirectory(longPath);

    WebRequest request = getGetFilesRequest(basePath);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    assertEquals(
        "Directory information with depth = 0 return children",
        0,
        getDirectoryChildren(new JSONObject(response.getText())).size());

    request = getGetFilesRequest(basePath + "?depth=1");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    List<JSONObject> depthChildren = getDirectoryChildren(new JSONObject(response.getText()));

    assertEquals(
        "Directory information with depth = 1 returned too shallow", 1, depthChildren.size());
    checkDirectoryMetadata(depthChildren.get(0), "dir1", null, null, null, null, null);

    assertEquals(
        "Directory information with depth = 1 returned too deep",
        0,
        getDirectoryChildren(depthChildren.get(0)).size());

    request = getGetFilesRequest(basePath + "?depth=2");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    depthChildren =
        getDirectoryChildren(getDirectoryChildren(new JSONObject(response.getText())).get(0));

    assertEquals(
        "Directory information with depth = 2 returned too shallow", 1, depthChildren.size());

    checkDirectoryMetadata(depthChildren.get(0), "dir2", null, null, null, null, null);

    assertEquals(
        "Directory information with depth = 2 returned too deep",
        0,
        getDirectoryChildren(depthChildren.get(0)).size());

    request = getGetFilesRequest(basePath + "?depth=3");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    depthChildren =
        getDirectoryChildren(
            getDirectoryChildren(getDirectoryChildren(new JSONObject(response.getText())).get(0))
                .get(0));

    assertEquals(
        "Directory information with depth = 3 returned too shallow", 1, depthChildren.size());

    checkDirectoryMetadata(depthChildren.get(0), "dir3", null, null, null, null, null);

    assertEquals(
        "Directory information with depth = 3 returned too deep",
        0,
        getDirectoryChildren(depthChildren.get(0)).size());
  }