Search in sources :

Example 1 with CopyOptions

use of org.eclipse.che.api.project.shared.dto.CopyOptions in project che by eclipse.

the class ProjectServiceTest method testCopyFileWithRename.

@Test
public void testCopyFileWithRename() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b/c");
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b")).createFile("test.txt", "to be or not no be".getBytes(Charset.defaultCharset()));
    Map<String, List<String>> headers = new HashMap<>();
    headers.put(CONTENT_TYPE, singletonList(APPLICATION_JSON));
    CopyOptions descriptor = DtoFactory.getInstance().createDto(CopyOptions.class);
    descriptor.setName("copyOfTest.txt");
    descriptor.setOverWrite(false);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/copy/my_project/a/b/test.txt?to=/my_project/a/b/c", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
    assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create("http://localhost:8080/api/project/file/my_project/a/b/c/copyOfTest.txt"));
    // new
    assertNotNull(myProject.getBaseFolder().getChild("a/b/c/copyOfTest.txt"));
    // old
    assertNotNull(myProject.getBaseFolder().getChild("a/b/test.txt"));
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CopyOptions(org.eclipse.che.api.project.shared.dto.CopyOptions) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 2 with CopyOptions

use of org.eclipse.che.api.project.shared.dto.CopyOptions in project che by eclipse.

the class ProjectServiceTest method testCopyFolderWithRenameAndOverwrite.

@Test
public void testCopyFolderWithRenameAndOverwrite() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b/c");
    // File names
    String originFileName = "test.txt";
    String destinationFileName = "overwriteMe.txt";
    // File contents
    String originContent = "to be or not no be";
    String overwrittenContent = "that is the question";
    // new name for folder
    final String renamedFolder = "renamedFolder";
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b")).createFile(originFileName, originContent.getBytes(Charset.defaultCharset()));
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b/c")).createFile(destinationFileName, overwrittenContent.getBytes(Charset.defaultCharset()));
    Map<String, List<String>> headers = new HashMap<>();
    headers.put(CONTENT_TYPE, singletonList(APPLICATION_JSON));
    CopyOptions descriptor = DtoFactory.getInstance().createDto(CopyOptions.class);
    descriptor.setName(renamedFolder);
    descriptor.setOverWrite(true);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/copy/my_project/a/b?to=/my_project/a/b/c", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
    assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create(format("http://localhost:8080/api/project/children/my_project/a/b/c/%s", renamedFolder)));
    assertNotNull(myProject.getBaseFolder().getChild("a/b/test.txt"));
    assertNotNull(myProject.getBaseFolder().getChild(format("a/b/c/%s/test.txt", renamedFolder)));
    assertEquals(myProject.getBaseFolder().getChild("a/b/test.txt").getName(), myProject.getBaseFolder().getChild(format("a/b/c/%s/%s", renamedFolder, originFileName)).getName());
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CopyOptions(org.eclipse.che.api.project.shared.dto.CopyOptions) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 3 with CopyOptions

use of org.eclipse.che.api.project.shared.dto.CopyOptions in project che by eclipse.

the class ProjectServiceTest method testCopyFolderWithRename.

@Test
public void testCopyFolderWithRename() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b/c");
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b")).createFile("test.txt", "to be or not no be".getBytes(Charset.defaultCharset()));
    // new name for folder
    final String renamedFolder = "renamedFolder";
    Map<String, List<String>> headers = new HashMap<>();
    headers.put(CONTENT_TYPE, singletonList(APPLICATION_JSON));
    CopyOptions descriptor = DtoFactory.getInstance().createDto(CopyOptions.class);
    descriptor.setName(renamedFolder);
    descriptor.setOverWrite(false);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/copy/my_project/a/b?to=/my_project/a/b/c", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
    assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create(format("http://localhost:8080/api/project/children/my_project/a/b/c/%s", renamedFolder)));
    assertNotNull(myProject.getBaseFolder().getChild("a/b/test.txt"));
    assertNotNull(myProject.getBaseFolder().getChild(format("a/b/c/%s/test.txt", renamedFolder)));
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CopyOptions(org.eclipse.che.api.project.shared.dto.CopyOptions) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 4 with CopyOptions

use of org.eclipse.che.api.project.shared.dto.CopyOptions in project che by eclipse.

the class ProjectServiceClientImpl method copy.

/** {@inheritDoc} */
@Override
public Promise<Void> copy(Path source, Path target, String newName, boolean overwrite) {
    final String url = getBaseUrl() + COPY + path(source.toString()) + "?to=" + URL.encodeQueryString(target.toString());
    final CopyOptions copyOptions = dtoFactory.createDto(CopyOptions.class);
    copyOptions.setName(newName);
    copyOptions.setOverWrite(overwrite);
    return reqFactory.createPostRequest(url, copyOptions).loader(loaderFactory.newLoader("Copying...")).send();
}
Also used : CopyOptions(org.eclipse.che.api.project.shared.dto.CopyOptions)

Example 5 with CopyOptions

use of org.eclipse.che.api.project.shared.dto.CopyOptions in project che by eclipse.

the class ProjectServiceTest method testCopyFileWithRenameAndOverwrite.

@Test
public void testCopyFileWithRenameAndOverwrite() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    myProject.getBaseFolder().createFolder("a/b/c");
    // File names
    String originFileName = "test.txt";
    String destinationFileName = "overwriteMe.txt";
    // File contents
    String originContent = "to be or not no be";
    String overwrittenContent = "that is the question";
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b")).createFile(originFileName, originContent.getBytes(Charset.defaultCharset()));
    ((FolderEntry) myProject.getBaseFolder().getChild("a/b/c")).createFile(destinationFileName, overwrittenContent.getBytes(Charset.defaultCharset()));
    Map<String, List<String>> headers = new HashMap<>();
    headers.put(CONTENT_TYPE, singletonList(APPLICATION_JSON));
    CopyOptions descriptor = DtoFactory.getInstance().createDto(CopyOptions.class);
    descriptor.setName(destinationFileName);
    descriptor.setOverWrite(true);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project/copy/my_project/a/b/" + originFileName + "?to=/my_project/a/b/c", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 201, "Error: " + response.getEntity());
    assertEquals(response.getHttpHeaders().getFirst("Location"), URI.create("http://localhost:8080/api/project/file/my_project/a/b/c/" + destinationFileName));
    // new
    assertNotNull(myProject.getBaseFolder().getChild("a/b/c/" + destinationFileName));
    // old
    assertNotNull(myProject.getBaseFolder().getChild("a/b/" + originFileName));
    Scanner inputStreamScanner = null;
    String theFirstLineFromDestinationFile;
    try {
        inputStreamScanner = new Scanner(myProject.getBaseFolder().getChild("a/b/c/" + destinationFileName).getVirtualFile().getContent());
        theFirstLineFromDestinationFile = inputStreamScanner.nextLine();
        // destination should contain original file's content
        assertEquals(theFirstLineFromDestinationFile, originContent);
    } catch (ForbiddenException | ServerException e) {
        Assert.fail(e.getMessage());
    } finally {
        if (inputStreamScanner != null) {
            inputStreamScanner.close();
        }
    }
}
Also used : Scanner(java.util.Scanner) ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ServerException(org.eclipse.che.api.core.ServerException) ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) CopyOptions(org.eclipse.che.api.project.shared.dto.CopyOptions) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Aggregations

CopyOptions (org.eclipse.che.api.project.shared.dto.CopyOptions)5 ArrayList (java.util.ArrayList)4 Collections.singletonList (java.util.Collections.singletonList)4 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4 ContainerResponse (org.everrest.core.impl.ContainerResponse)4 Test (org.testng.annotations.Test)4 Scanner (java.util.Scanner)1 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)1 ServerException (org.eclipse.che.api.core.ServerException)1