Пример #1
0
 private ServiceDescriptor getDescriptor() throws Exception {
   String path = SERVICE_URI;
   ContainerResponse response =
       launcher.service(HttpMethod.OPTIONS, path, BASE_URI, null, null, null, null);
   Assert.assertEquals(response.getStatus(), 200);
   return (ServiceDescriptor) response.getEntity();
 }
Пример #2
0
 public void testDeleteFolder() throws Exception {
   String path = SERVICE_URI + "delete/" + folderId;
   ContainerResponse response =
       launcher.service(HttpMethod.POST, path, BASE_URI, null, null, null);
   assertEquals(204, response.getStatus());
   try {
     mountPoint.getVirtualFileById(folderId);
     fail("Folder must be removed. ");
   } catch (NotFoundException e) {
   }
   try {
     mountPoint.getVirtualFileById(folderChildId);
     fail("Child file must be removed. ");
   } catch (NotFoundException e) {
   }
   try {
     mountPoint.getVirtualFile(folderPath);
     fail("Folder must be removed. ");
   } catch (NotFoundException e) {
   }
   try {
     mountPoint.getVirtualFile(folderChildPath);
     fail("Child file must be removed. ");
   } catch (NotFoundException e) {
   }
 }
Пример #3
0
  public void testUpdatePropertiesFile2() throws Exception {
    Map<String, String[]> props = new HashMap<>(3);
    props.put("a", new String[] {"to be or not to be"});
    props.put("b", new String[] {"hello world"});
    props.put("c", new String[] {"test"});
    writeProperties(filePath, props);

    String update =
        "[{\"name\":\"b\", \"value\":[\"TEST\"]}," //
            + "{\"name\":\"c\", \"value\":[\"TEST\"]}," //
            + "{\"name\":\"d\", \"value\":[\"TEST\", \"TEST\"]}]";

    String requestPath = SERVICE_URI + "item/" + fileId;
    Map<String, List<String>> h = new HashMap<>(1);
    h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
    ContainerResponse response =
        launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, update.getBytes(), null);
    assertEquals("Error: " + response.getEntity(), 200, response.getStatus());

    Map<String, String[]> expectedProperties = new HashMap<>(4);
    expectedProperties.put("a", new String[] {"to be or not to be"});
    expectedProperties.put("b", new String[] {"TEST"});
    expectedProperties.put("c", new String[] {"TEST"});
    expectedProperties.put("d", new String[] {"TEST", "TEST"});
    validateProperties(filePath, expectedProperties);

    Item item = getItem(fileId);
    assertEquals("to be or not to be", getPropertyValue(item, "a")); // not updated
    assertEquals("TEST", getPropertyValue(item, "b"));
    assertEquals("TEST", getPropertyValue(item, "c"));
    assertEquals(Arrays.asList("TEST", "TEST"), getPropertyValues(item, "d"));
  }
Пример #4
0
 public void testDeleteFileWrongId() throws Exception {
   ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
   String path = SERVICE_URI + "delete/" + fileId + "_WRONG_ID";
   ContainerResponse response =
       launcher.service(HttpMethod.POST, path, BASE_URI, null, null, writer, null);
   assertEquals(404, response.getStatus());
   log.info(new String(writer.getBody()));
 }
Пример #5
0
 public void testDeleteRootFolder() throws Exception {
   ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
   String path = SERVICE_URI + "delete/" + mountPoint.getRoot().getId();
   ContainerResponse response =
       launcher.service(HttpMethod.POST, path, BASE_URI, null, null, writer, null);
   assertEquals(403, response.getStatus());
   log.info(new String(writer.getBody()));
 }
Пример #6
0
 @Test
 public void testTransformToHtml() throws Exception {
   processor.addApplication(new Application0());
   ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
   ContainerResponse response = launcher.service("GET", "/a", "", null, null, writer, null);
   Assert.assertEquals(200, response.getStatus());
   System.out.println(new String(writer.getBody()));
 }
Пример #7
0
 public void testUploadFileAlreadyExists() throws Exception {
   String fileName = "existedFile.txt";
   VirtualFile folder = mountPoint.getVirtualFileById(uploadTestFolderId);
   folder.createFile(fileName, new ByteArrayInputStream(DEFAULT_CONTENT.getBytes()));
   ContainerResponse response = doUploadFile(fileName, DEFAULT_CONTENT, "", "", false);
   assertEquals(200, response.getStatus());
   String entity = (String) response.getEntity();
   assertTrue(entity.contains("Item with the same name exists"));
   log.info(entity);
 }
Пример #8
0
 public void testUploadNewFile() throws Exception {
   // Passed by browser.
   String fileName = "testUploadNewFile.txt";
   // File content.
   String fileContent = "test upload file";
   ContainerResponse response = doUploadFile(fileName, fileContent, "", "", false);
   assertEquals(200, response.getStatus());
   String expectedPath = uploadTestFolderPath + '/' + fileName;
   VirtualFile file = mountPoint.getVirtualFile(expectedPath);
   assertNotNull("File was not created in expected location. ", file);
   checkFileContext(fileContent, MediaType.TEXT_PLAIN, file);
 }
Пример #9
0
 public void testDeleteFolderLockedChild() throws Exception {
   mountPoint.getVirtualFileById(folderChildId).lock(0);
   String path = SERVICE_URI + "delete/" + folderId;
   ContainerResponse response =
       launcher.service(HttpMethod.POST, path, BASE_URI, null, null, null);
   assertEquals(403, response.getStatus());
   try {
     mountPoint.getVirtualFileById(folderId);
   } catch (NotFoundException e) {
     fail("Folder must not be removed since child file is locked. ");
   }
 }
Пример #10
0
  public void testUploadFileAlreadyExistsOverwrite() throws Exception {
    String fileName = "existedFileOverwrite.txt";
    VirtualFile folder = mountPoint.getVirtualFileById(uploadTestFolderId);
    folder.createFile(fileName, new ByteArrayInputStream(DEFAULT_CONTENT.getBytes()));

    String fileContent = "test upload and overwrite existed file";
    ContainerResponse response = doUploadFile(fileName, fileContent, "", "", true);
    assertEquals(200, response.getStatus());
    String expectedPath = uploadTestFolderPath + '/' + fileName;
    VirtualFile file = mountPoint.getVirtualFile(expectedPath);
    assertNotNull("File was not created in expected location. ", file);
    checkFileContext(fileContent, MediaType.TEXT_PLAIN, file);
  }
Пример #11
0
 public void testUploadNewFileCustomizeName() throws Exception {
   // Passed by browser.
   String fileName = "testUploadNewFileCustomizeName";
   // File content.
   String fileContent = "test upload file with custom name";
   // Name of file passed in HTML form. If present it should be used instead of original file name.
   String formFileName = fileName + ".txt";
   ContainerResponse response = doUploadFile(fileName, fileContent, "", formFileName, false);
   assertEquals(200, response.getStatus());
   String expectedPath = uploadTestFolderPath + '/' + formFileName;
   VirtualFile file = mountPoint.getVirtualFile(expectedPath);
   assertNotNull("File was not created in expected location. ", file);
   checkFileContext(fileContent, MediaType.TEXT_PLAIN, file);
 }
Пример #12
0
 public void testDeleteFileLockedNoLockToken() throws Exception {
   file.lock(0);
   ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
   String path = SERVICE_URI + "delete/" + fileId;
   ContainerResponse response =
       launcher.service(HttpMethod.POST, path, BASE_URI, null, null, writer, null);
   assertEquals(403, response.getStatus());
   log.info(new String(writer.getBody()));
   try {
     mountPoint.getVirtualFileById(fileId);
   } catch (NotFoundException e) {
     fail("File must not be removed since it is locked. ");
   }
 }
Пример #13
0
 public void testDeleteFileLocked() throws Exception {
   String lockToken = file.lock(0);
   String path = SERVICE_URI + "delete/" + fileId + '?' + "lockToken=" + lockToken;
   ContainerResponse response =
       launcher.service(HttpMethod.POST, path, BASE_URI, null, null, null);
   assertEquals(204, response.getStatus());
   try {
     mountPoint.getVirtualFileById(fileId);
     fail("File must be removed. ");
   } catch (NotFoundException e) {
   }
   try {
     mountPoint.getVirtualFile(filePath);
     fail("File must be removed. ");
   } catch (NotFoundException e) {
   }
 }
Пример #14
0
  public void testUpdatePropertiesFile() throws Exception {
    String update = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";

    String requestPath = SERVICE_URI + "item/" + fileId;
    Map<String, List<String>> h = new HashMap<>(1);
    h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
    ContainerResponse response =
        launcher.service(HttpMethod.POST, requestPath, BASE_URI, h, update.getBytes(), null);
    assertEquals("Error: " + response.getEntity(), 200, response.getStatus());

    Map<String, String[]> expectedProperties = new HashMap<>(1);
    expectedProperties.put("MyProperty", new String[] {"MyValue"});
    validateProperties(filePath, expectedProperties);

    Item item = getItem(fileId);
    assertEquals("MyValue", getPropertyValue(item, "MyProperty"));
  }
Пример #15
0
  public void testUpdateFileNoPermissions() throws Exception {
    String properties = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";

    String requestPath = SERVICE_URI + "item/" + protectedFileId;
    Map<String, List<String>> h = new HashMap<>(1);
    h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
    ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
    ContainerResponse response =
        launcher.service(
            HttpMethod.POST, requestPath, BASE_URI, h, properties.getBytes(), writer, null);
    assertEquals(403, response.getStatus());
    log.info(new String(writer.getBody()));

    assertNull("Properties must not be updated.", readProperties(protectedFilePath));

    Item item = getItem(protectedFileId);
    assertNull(getPropertyValue(item, "MyProperty"));
  }
Пример #16
0
  private void publicationTest(boolean singleton, ResourceId resourceId) throws Exception {
    String script = //
        "@javax.ws.rs.Path(\"a\")" //
            + "class GroovyResource {" //
            + "@javax.ws.rs.GET @javax.ws.rs.Path(\"{who}\")" //
            + "def m0(@javax.ws.rs.PathParam(\"who\") String who) { return (\"hello \" + who)}" //
            + "}";

    int initSize = resources.getSize();
    Assert.assertEquals(0, groovyPublisher.resources.size());

    if (singleton) {
      groovyPublisher.publishSingleton(script, resourceId, null, null, null);
    } else {
      groovyPublisher.publishPerRequest(script, resourceId, null, null, null);
    }

    Assert.assertEquals(initSize + 1, resources.getSize());
    Assert.assertEquals(1, groovyPublisher.resources.size());
    Assert.assertTrue(groovyPublisher.isPublished(resourceId));

    String cs =
        resources
            .getMatchedResource("/a", new ArrayList<String>())
            .getObjectModel()
            .getObjectClass()
            .getProtectionDomain()
            .getCodeSource()
            .getLocation()
            .toString();
    Assert.assertEquals("file:/groovy/script/jaxrs", cs);

    ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
    ContainerResponse resp = launcher.service("GET", "/a/groovy", "", null, null, writer, null);
    Assert.assertEquals(200, resp.getStatus());
    Assert.assertEquals("hello groovy", new String(writer.getBody()));

    groovyPublisher.unpublishResource(resourceId);

    Assert.assertEquals(initSize, resources.getSize());
    Assert.assertEquals(0, groovyPublisher.resources.size());
  }
Пример #17
0
  public void testUpdateFileHavePermissions() throws Exception {
    String properties = "[{\"name\":\"MyProperty\", \"value\":[\"MyValue\"]}]";

    String requestPath = SERVICE_URI + "item/" + protectedFileId;
    Map<String, List<String>> h = new HashMap<>(1);
    h.put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
    ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
    // File is protected and default principal 'andrew' has not write permission.
    // Replace default principal by principal who has write permission.
    EnvironmentContext.getCurrent()
        .setUser(
            new UserImpl("andrew", "andrew", null, Arrays.asList("workspace/developer"), false));
    ContainerResponse response =
        launcher.service(
            HttpMethod.POST, requestPath, BASE_URI, h, properties.getBytes(), writer, null);
    assertEquals("Error: " + response.getEntity(), 200, response.getStatus());

    Map<String, String[]> expectedProperties = new HashMap<>(1);
    expectedProperties.put("MyProperty", new String[] {"MyValue"});
    validateProperties(protectedFilePath, expectedProperties);

    Item item = getItem(protectedFileId);
    assertEquals("MyValue", getPropertyValue(item, "MyProperty"));
  }