Search in sources :

Example 1 with Folder

use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.

the class FolderRepository method getHierarchy.

/**
 * Get the folder hierarchy (list of its parents)
 *
 * @param folder the folder.
 * @return A {@link Iterable} of {@link Folder} containing all its parents starting from home.
 */
default List<Folder> getHierarchy(final Folder folder) {
    final List<Folder> hierarchy = new ArrayList<>();
    String nextParentId = folder.getParentId();
    while (nextParentId != null) {
        try {
            final Folder parent = this.getFolderById(nextParentId);
            hierarchy.add(0, parent);
            nextParentId = parent.getParentId();
        } catch (final TDPException e) {
            // as the user's home child
            if (e.getCode().getHttpStatus() == 403) {
                hierarchy.add(0, this.getHome());
                break;
            }
            throw e;
        }
    }
    return hierarchy;
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) ArrayList(java.util.ArrayList) Folder(org.talend.dataprep.api.folder.Folder)

Example 2 with Folder

use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.

the class FileSystemFolderRepository method renameFolder.

@Override
public Folder renameFolder(String folderId, String newName) {
    final Folder folder = getFolderById(folderId);
    if (folder == null) {
        throw new IllegalArgumentException("Cannot rename a folder that cannot be found");
    }
    FolderPath folderToMovePath = FolderPath.deserializeFromString(folder.getPath());
    if (folderToMovePath.isRoot()) {
        throw new IllegalArgumentException("Cannot rename home folder");
    }
    if (newName.contains(PATH_SEPARATOR.toString())) {
        throw new TDPException(ILLEGAL_FOLDER_NAME, build().put("name", newName));
    }
    FolderPath targetFolderPath = new FolderPath(folderToMovePath.getParent(), newName);
    Path folderPath = pathsConverter.toPath(folderToMovePath);
    Path newFolderPath = pathsConverter.toPath(targetFolderPath);
    try {
        FileUtils.moveDirectory(folderPath.toFile(), newFolderPath.toFile());
    } catch (IOException e) {
        throw new TDPException(UNABLE_TO_RENAME_FOLDER, e, build().put("path", folder.getPath()));
    }
    return getFolderById(toId(pathsConverter.toFolderPath(newFolderPath)));
}
Also used : TDPException(org.talend.dataprep.exception.TDPException) Path(java.nio.file.Path) IOException(java.io.IOException) Folder(org.talend.dataprep.api.folder.Folder)

Example 3 with Folder

use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.

the class FolderAPITest method should_rename_folder.

@Test
public void should_rename_folder() throws IOException {
    // given
    createFolder("beer", home.getId());
    final Folder beer = getFolder(home.getId(), "beer");
    final String name = "new beer";
    // when
    final Response response = // 
    given().body(// 
    name).when().put("/api/folders/{id}/name", beer.getId());
    // then
    assertThat(response.getStatusCode(), is(200));
    final Folder updatedBeer = getFolder(home.getId(), name);
    assertNotNull(updatedBeer);
}
Also used : Response(com.jayway.restassured.response.Response) Folder(org.talend.dataprep.api.folder.Folder) Test(org.junit.Test)

Example 4 with Folder

use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.

the class FolderAPITest method shouldFolderMetadataWithHierarchy.

@Test
public void shouldFolderMetadataWithHierarchy() throws Exception {
    // given
    // HOME
    // ___________|____________
    // |                       |
    // first                  second
    // ____|____                   |
    // |        |                  |
    // first child 1   first child 2     second child
    // |
    // |
    // second child child
    createFolder("first", home.getId());
    createFolder("second", home.getId());
    final Folder firstFolder = getFolder(home.getId(), "first");
    final Folder secondFolder = getFolder(home.getId(), "second");
    createFolder("first child one", firstFolder.getId());
    createFolder("first child two", firstFolder.getId());
    createFolder("second child", secondFolder.getId());
    final Folder secondChildFolder = getFolder(secondFolder.getId(), "second child");
    createFolder("second child child", secondChildFolder.getId());
    final Folder firstChildTwo = getFolder(firstFolder.getId(), "first child two");
    // when
    final Response response = // 
    given().expect().statusCode(200).log().ifError().when().get("/folders/{id}", firstChildTwo.getId());
    // then
    final FolderInfo infos = mapper.readValue(response.asString(), new TypeReference<FolderInfo>() {
    });
    final List<Folder> hierarchy = StreamSupport.stream(infos.getHierarchy().spliterator(), false).collect(toList());
    assertThat(infos.getFolder(), equalTo(firstChildTwo));
    assertThat(hierarchy, hasSize(2));
    assertThat(hierarchy.get(0).getId(), equalTo(home.getId()));
    assertThat(hierarchy.get(1).getId(), equalTo(firstFolder.getId()));
}
Also used : Response(com.jayway.restassured.response.Response) Folder(org.talend.dataprep.api.folder.Folder) FolderInfo(org.talend.dataprep.api.folder.FolderInfo) Test(org.junit.Test)

Example 5 with Folder

use of org.talend.dataprep.api.folder.Folder in project data-prep by Talend.

the class FolderAPITest method assertOnSearch.

private void assertOnSearch(final String searchQuery, final boolean strict, final int expectedSize) throws Exception {
    // when
    final Response response = // 
    given().queryParam("name", // 
    searchQuery).queryParam("strict", // 
    strict).when().get("/api/folders/search");
    final List<Folder> folders = mapper.readValue(response.asString(), new TypeReference<List<Folder>>() {
    });
    // then
    assertThat(folders, hasSize(expectedSize));
}
Also used : Response(com.jayway.restassured.response.Response) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Folder(org.talend.dataprep.api.folder.Folder)

Aggregations

Folder (org.talend.dataprep.api.folder.Folder)69 Test (org.junit.Test)36 Response (com.jayway.restassured.response.Response)25 BasePreparationTest (org.talend.dataprep.preparation.BasePreparationTest)21 ServiceBaseTest (org.talend.ServiceBaseTest)17 FolderEntry (org.talend.dataprep.api.folder.FolderEntry)16 TDPException (org.talend.dataprep.exception.TDPException)9 Collectors.toList (java.util.stream.Collectors.toList)7 List (java.util.List)6 Preparation (org.talend.dataprep.api.preparation.Preparation)6 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)5 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)5 FolderInfo (org.talend.dataprep.api.folder.FolderInfo)5 EnrichedPreparation (org.talend.dataprep.api.service.api.EnrichedPreparation)5 Stream (java.util.stream.Stream)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 PREPARATION (org.talend.dataprep.api.folder.FolderContentType.PREPARATION)4 ApiOperation (io.swagger.annotations.ApiOperation)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3