package org.talend.dataprep.api.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;
import org.apache.commons.io.FileUtils;
import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.TestPropertySource;
import org.talend.dataprep.api.folder.Folder;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class FolderAPITest extends ApiServiceTestBase {

    Logger logger = LoggerFactory.getLogger(getClass());


    @Before
    public void cleanupFolder() throws Exception {
        FileUtils.deleteDirectory(Paths.get("target/test/store/folders").toFile());
    }

    @Test
    public void add_then_remove() throws Exception {

        // create foo folder under root
        Response response = RestAssured.given() //
                .queryParam("path", "foo")
                .when() //
                .get("/api/folders/add");

        Folder created = response.as(Folder.class);

        logger.info("created: {}", created);

        // create beer under foo
        response = RestAssured.given() //
                .queryParam("path", "beer") //
                .queryParam("parentPath", "foo") //
                .when() //
                .get("/api/folders/add");

        created = response.as(Folder.class);

        logger.info("created: {}", created);


        // create wine under foo
        response = RestAssured.given() //
                .queryParam("path", "wine") //
                .queryParam("parentPath", "foo") //
                .when() //
                .get("/api/folders/add");

        created = response.as(Folder.class);

        logger.info("created: {}", created);

        response = RestAssured.given() //
                .when() //
                .get("/api/folders/childs");

        logger.info("response: {}", response.asString());

        response = RestAssured.given() //
                .queryParam("path", "foo") //
                .when() //
                .get("/api/folders/childs");


        ObjectMapper objectMapper = new ObjectMapper();


        List<Folder> folders = objectMapper.readValue(response.asString(), new TypeReference<List<Folder>>(){});

        List<Folder> expected = Lists.newArrayList(Folder.Builder.folder().path("foo/beer/").build(), //
                Folder.Builder.folder().path("foo/wine/").build());

        Assertions.assertThat(folders).isNotNull().isNotEmpty().hasSize(2) //
                .usingElementComparatorOnFields("path").containsAll(expected);


        RestAssured.given() //
                .queryParam("path", "foo/wine") //
                .when() //
                .delete("/api/folders");


        response = RestAssured.given() //
                .queryParam("path", "foo") //
                .when() //
                .get("/api/folders/childs");


        folders = objectMapper.readValue(response.asString(), new TypeReference<List<Folder>>(){});

        expected = Lists.newArrayList(Folder.Builder.folder().path("foo/beer/").build());

        Assertions.assertThat(folders).isNotNull().isNotEmpty().hasSize(1) //
                .usingElementComparatorOnFields("path").containsAll(expected);


    }

}

@@ -3561,21 +3561,445 @@
 xpected);%0A%0A%0A
+        RestAssured.given() //%0A                .queryParam(%22path%22, %22foo%22) //%0A                .when() //%0A                .delete(%22/api/folders%22);%0A%0A        response = RestAssured.given() //%0A                .when() //%0A                .get(%22/api/folders/childs%22);%0A%0A        folders = objectMapper.readValue(response.asString(), new TypeReference%3CList%3CFolder%3E%3E()%7B%7D);%0A%0A        Assertions.assertThat(folders).isNotNull().isEmpty();%0A
     %7D%0A%0A%7D%0A

improve testing