<commit_before>// ============================================================================
// Copyright (C) 2006-2016 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// https://github.com/Talend/data-prep/blob/master/LICENSE
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================

package org.talend.dataprep.api.service;

import static com.jayway.restassured.RestAssured.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.emptyMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.OK;
import static org.talend.dataprep.api.export.ExportParameters.SourceType.FILTER;
import static org.talend.dataprep.api.folder.FolderContentType.PREPARATION;
import static org.talend.dataprep.api.service.EntityBuilder.buildAction;
import static org.talend.dataprep.api.service.EntityBuilder.buildParametersMap;
import static org.talend.dataprep.api.service.PreparationAPITestClient.appendStepsToPrep;
import static org.talend.dataprep.api.service.PreparationAPITestClient.changePreparationStepsOrder;
import static org.talend.dataprep.cache.ContentCache.TimeToLive.PERMANENT;
import static org.talend.dataprep.test.SameJSONFile.sameJSONAsFile;
import static org.talend.dataprep.transformation.format.JsonFormat.JSON;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.talend.dataprep.StandalonePreparation;
import org.talend.dataprep.api.dataset.ColumnMetadata;
import org.talend.dataprep.api.dataset.RowMetadata;
import org.talend.dataprep.api.folder.Folder;
import org.talend.dataprep.api.folder.FolderEntry;
import org.talend.dataprep.api.preparation.AppendStep;
import org.talend.dataprep.api.preparation.Preparation;
import org.talend.dataprep.api.preparation.Step;
import org.talend.dataprep.api.service.api.EnrichedPreparation;
import org.talend.dataprep.cache.ContentCache;
import org.talend.dataprep.cache.ContentCacheKey;
import org.talend.dataprep.security.Security;
import org.talend.dataprep.transformation.cache.CacheKeyGenerator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;

public class PreparationAPITest extends ApiServiceTestBase {

    @Autowired
    private Security security;

    @Autowired
    private ContentCache contentCache;

    @Autowired
    private CacheKeyGenerator cacheKeyGenerator;

    // ------------------------------------------------------------------------------------------------------------------
    // -----------------------------------------------------GETTER-------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------------------
    @Test
    public void testEmptyPreparationList() throws Exception {
        assertThat(when().get("/api/preparations").asString(), sameJSONAs("[]"));
        assertThat(when().get("/api/preparations/?format=short").asString(), sameJSONAs("[]"));
        assertThat(when().get("/api/preparations/?format=long").asString(), sameJSONAs("[]"));
    }

    @Test
    public void testPreparationsList() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        String preparationId = testClient.createPreparationFromDataset(tagadaId, "testPreparation", home.getId());

        // when : short format
        final JsonPath shortFormat = when().get("/api/preparations/?format=short").jsonPath();

        // then
        final List<String> values = shortFormat.getList("");
        assertThat(values.get(0), is(preparationId));

        // when : long format
        final JsonPath longFormat = when().get("/api/preparations/?format=long").jsonPath();

        // then
        assertThat(longFormat.getList("dataSetId").size(), is(1));
        assertThat(longFormat.getList("dataSetId").get(0), is(tagadaId));
        assertThat(longFormat.getList("author").size(), is(1));
        assertThat(longFormat.getList("author").get(0), is(security.getUserId()));
        assertThat(longFormat.getList("id").size(), is(1));
        assertThat(longFormat.getList("id").get(0), is(preparationId));
        assertThat(longFormat.getList("actions").size(), is(1));
        assertThat(((List) longFormat.getList("actions").get(0)).size(), is(0));

        // when : summary format
        final JsonPath summaryFormat = when().get("/api/preparations/?format=summary").jsonPath();

        // then
        assertThat(summaryFormat.getList("id").size(), is(1));
        assertThat(summaryFormat.getList("id").get(0), is(preparationId));
        assertThat(summaryFormat.getList("name").size(), is(1));
        assertThat(summaryFormat.getList("name").get(0), is("testPreparation"));
        assertThat(summaryFormat.getList("owner").size(), is(1));
        assertThat(summaryFormat.getList("lastModificationDate").size(), is(1));
        assertThat(summaryFormat.getList("allowDistributedRun").size(), is(1));
    }

    @Test
    public void testPreparationsList_withFilter() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        String preparationId = testClient.createPreparationFromDataset(tagadaId, "testPreparation", home.getId());

        // when : short format
        final JsonPath shortFormat = when().get("/api/preparations/?format=short&name={name}", "testPreparation").jsonPath();

        // then
        final List<String> values = shortFormat.getList("");
        assertThat(values.get(0), is(preparationId));
    }

    @Test
    public void testPreparationGet() throws Exception {
        // when
        final String datasetId = testClient.createDataset("dataset/dataset.csv", "great dataset", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(datasetId, "1234", home.getId());

        // then
        final JsonPath longFormat = given().get("/api/preparations/{id}/details", preparationId).jsonPath();
        assertThat(longFormat.getString("dataSetId"), is(datasetId));
        assertThat(longFormat.getString("author"), is(security.getUserId()));
        assertThat(longFormat.getString("id"), is(preparationId));
        assertThat(longFormat.getList("actions").size(), is(0));

        assertNotNull(longFormat.getMap("rowMetadata"));
        assertNotNull(longFormat.getMap("rowMetadata").get("columns"));
        ArrayList rowMetaData = (ArrayList) longFormat.getMap("rowMetadata").get("columns");
        assertThat(rowMetaData.size(), is(6));

        assertThat(longFormat.getString("allowFullRun"), is("false"));
        final List<String> steps = longFormat.getList("steps"); // make sure the "steps" node is a string array
        assertThat(steps.size(), is(1));
    }

    @Test
    public void testListCompatibleDataSets() throws Exception {
        // given
        final String dataSetId = testClient.createDataset("dataset/dataset.csv", "compatible1", "text/csv");
        final String dataSetId2 = testClient.createDataset("dataset/dataset.csv", "compatible2", "text/csv");
        final String dataSetId3 = testClient.createDataset("t-shirt_100.csv", "incompatible", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(dataSetId, "testPreparation", home.getId());

        // when
        final String compatibleDatasetList = when().get("/api/preparations/{id}/basedatasets", preparationId).asString();

        // then
        assertTrue(compatibleDatasetList.contains(dataSetId2));
        assertFalse(compatibleDatasetList.contains(dataSetId3));
    }

    @Test
    public void testListCompatibleDataSetsWhenUniqueDatasetInRepository() throws Exception {
        // given
        final String dataSetId = testClient.createDataset("dataset/dataset.csv", "unique", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(dataSetId, "testPreparation", home.getId());

        // when
        final String compatibleDatasetList = when().get("/api/preparations/{id}/basedatasets", preparationId).asString();

        // then
        assertFalse(compatibleDatasetList.contains(dataSetId));
    }

    @Test
    public void shouldCopyPreparation() throws Exception {
        // given
        Folder destination = folderRepository.addFolder(home.getId(), "/destination");
        Folder origin = folderRepository.addFolder(home.getId(), "/from");
        final String preparationId = testClient.createPreparationFromFile("dataset/dataset.csv", "super preparation", "text/csv",
                origin.getId());

        // when
        String newPreparationName = "NEW super preparation";
        final Response response = given() //
                .queryParam("destination", destination.getId()) //
                .queryParam("newName", newPreparationName) //
                .when()//
                .expect().statusCode(200).log().ifError() //
                .post("api/preparations/{id}/copy", preparationId);

        // then
        assertEquals(200, response.getStatusCode());
        String copyId = response.asString();

        // check the folder entry
        final Iterator<FolderEntry> iterator = folderRepository.entries(destination.getId(), PREPARATION).iterator();
        assertTrue(iterator.hasNext());
        final FolderEntry entry = iterator.next();
        assertEquals(entry.getContentId(), copyId);

        // check the name
        final Preparation actual = preparationRepository.get(copyId, Preparation.class);
        assertEquals(newPreparationName, actual.getName());
    }

    @Test
    public void copyPreparationShouldForwardExceptions() throws Exception {

        // when
        final Response response = given() //
                .queryParam("destination", "/destination") //
                .when()//
                .expect().statusCode(404).log().ifError() //
                .post("api/preparations/{id}/copy", "preparation_not_found");

        // then
        assertEquals(404, response.getStatusCode());
    }

    @Test
    public void shouldMovePreparation() throws Exception {
        // given
        final Folder source = folderRepository.addFolder(home.getId(), "source");
        final String id = testClient.createPreparationFromFile("dataset/dataset.csv", "great_preparation", "text/csv",
                source.getId());

        final Folder destination = folderRepository.addFolder(home.getId(), "destination");

        // when
        final Response response = given() //
                .queryParam("folder", source.getId()) //
                .queryParam("destination", destination.getId()) //
                .queryParam("newName", "NEW great preparation") //
                .when()//
                .expect().statusCode(200).log().ifError() //
                .put("api/preparations/{id}/move", id);

        // then
        assertEquals(200, response.getStatusCode());

        // check the folder entry
        final Iterator<FolderEntry> iterator = folderRepository.entries(destination.getId(), PREPARATION).iterator();
        assertTrue(iterator.hasNext());
        final FolderEntry entry = iterator.next();
        assertEquals(entry.getContentId(), id);

        // check the name
        final Preparation actual = preparationRepository.get(id, Preparation.class);
        assertEquals("NEW great preparation", actual.getName());
    }

    @Test
    public void movePreparationShouldForwardExceptions() throws Exception {

        // when
        final Response response = given() //
                .queryParam("folder", "/from") //
                .queryParam("destination", "/to") //
                .queryParam("newName", "NEW great preparation") //
                .when()//
                .expect().statusCode(404).log().ifError() //
                .put("api/preparations/{id}/move", "unknown_preparation");

        // then
        assertEquals(404, response.getStatusCode());
    }

    /**
     *
     * @see <a href="https://jira.talendforge.org/browse/TDP-3965">TDP-3965</a>
     */
    @Test
    public void ensureThatPreparationDetailsCanBeParsedAsStandalonePreparation_TDP_3965() throws Exception {
        // when
        final String preparationId = testClient.createPreparationFromFile("dataset/dataset.csv", "testPreparation", "text/csv",
                home.getId());

        // when
        testClient.applyActionFromFile(preparationId, "transformation/upper_case_firstname.json");
        InputStream inputStream = given().expect().statusCode(200).get("/api/preparations/{preparation}/details", preparationId)
                .asInputStream();

        // then
        try {
            mapper.readerFor(StandalonePreparation.class).readValue(inputStream);
        } catch (Exception e) {
            fail();
        }
    }

    // ------------------------------------------------------------------------------------------------------------------
    // ----------------------------------------------------LIFECYCLE-----------------------------------------------------
    // ------------------------------------------------------------------------------------------------------------------
    @Test
    public void testPreparationUpdate() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(tagadaId, "original_name", home.getId());

        JsonPath longFormat = when().get("/api/preparations/?format=long").jsonPath();
        assertThat(longFormat.getList("name").size(), is(1));
        assertThat(longFormat.getList("name").get(0), is("original_name"));
        assertThat(longFormat.getList("id").size(), is(1));
        assertThat(longFormat.getList("id").get(0), is(preparationId));

        Preparation preparation = new Preparation();
        preparation.setId(preparationId);
        String newName = "updated_name";
        preparation.setName(newName);

        // when
        given().contentType(ContentType.JSON).body(preparation)
                .put("/api/preparations/{id}", preparationId).asString();

        // then
        longFormat = when().get("/api/preparations/?format=long").jsonPath();
        assertThat(longFormat.getList("name").size(), is(1));
        assertThat(longFormat.getList("name").get(0), is(newName));
    }

    @Test
    public void testPreparationUpdate_shouldFail() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(tagadaId, "original_name", home.getId());
        Preparation preparation = testClient.getPreparation(preparationId);
        preparation.setRowMetadata(null);

        // when
        given().contentType(ContentType.JSON)
                .body(preparation)
                .expect().statusCode(HttpStatus.BAD_REQUEST.value()).log().ifValidationFails()
                .put("/api/preparations/{id}", preparationId);
    }
<commit_msg>Delete useless test because of a fix on update validation (TDP-4136 / b10d8f2260afd1eb805851d630e1587bd1f00437)<commit_after>// ============================================================================
// Copyright (C) 2006-2016 Talend Inc. - www.talend.com
//
// This source code is available under agreement available at
// https://github.com/Talend/data-prep/blob/master/LICENSE
//
// You should have received a copy of the agreement
// along with this program; if not, write to Talend SA
// 9 rue Pages 92150 Suresnes, France
//
// ============================================================================

package org.talend.dataprep.api.service;

import static com.jayway.restassured.RestAssured.*;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.emptyMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.springframework.http.HttpStatus.OK;
import static org.talend.dataprep.api.export.ExportParameters.SourceType.FILTER;
import static org.talend.dataprep.api.folder.FolderContentType.PREPARATION;
import static org.talend.dataprep.api.service.EntityBuilder.buildAction;
import static org.talend.dataprep.api.service.EntityBuilder.buildParametersMap;
import static org.talend.dataprep.api.service.PreparationAPITestClient.appendStepsToPrep;
import static org.talend.dataprep.api.service.PreparationAPITestClient.changePreparationStepsOrder;
import static org.talend.dataprep.cache.ContentCache.TimeToLive.PERMANENT;
import static org.talend.dataprep.test.SameJSONFile.sameJSONAsFile;
import static org.talend.dataprep.transformation.format.JsonFormat.JSON;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.talend.dataprep.StandalonePreparation;
import org.talend.dataprep.api.dataset.ColumnMetadata;
import org.talend.dataprep.api.dataset.RowMetadata;
import org.talend.dataprep.api.folder.Folder;
import org.talend.dataprep.api.folder.FolderEntry;
import org.talend.dataprep.api.preparation.AppendStep;
import org.talend.dataprep.api.preparation.Preparation;
import org.talend.dataprep.api.preparation.Step;
import org.talend.dataprep.api.service.api.EnrichedPreparation;
import org.talend.dataprep.cache.ContentCache;
import org.talend.dataprep.cache.ContentCacheKey;
import org.talend.dataprep.security.Security;
import org.talend.dataprep.transformation.cache.CacheKeyGenerator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;

public class PreparationAPITest extends ApiServiceTestBase {

    @Autowired
    private Security security;

    @Autowired
    private ContentCache contentCache;

    @Autowired
    private CacheKeyGenerator cacheKeyGenerator;

    // ------------------------------------------------------------------------------------------------------------------
    // -----------------------------------------------------GETTER-------------------------------------------------------
    // ------------------------------------------------------------------------------------------------------------------
    @Test
    public void testEmptyPreparationList() throws Exception {
        assertThat(when().get("/api/preparations").asString(), sameJSONAs("[]"));
        assertThat(when().get("/api/preparations/?format=short").asString(), sameJSONAs("[]"));
        assertThat(when().get("/api/preparations/?format=long").asString(), sameJSONAs("[]"));
    }

    @Test
    public void testPreparationsList() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        String preparationId = testClient.createPreparationFromDataset(tagadaId, "testPreparation", home.getId());

        // when : short format
        final JsonPath shortFormat = when().get("/api/preparations/?format=short").jsonPath();

        // then
        final List<String> values = shortFormat.getList("");
        assertThat(values.get(0), is(preparationId));

        // when : long format
        final JsonPath longFormat = when().get("/api/preparations/?format=long").jsonPath();

        // then
        assertThat(longFormat.getList("dataSetId").size(), is(1));
        assertThat(longFormat.getList("dataSetId").get(0), is(tagadaId));
        assertThat(longFormat.getList("author").size(), is(1));
        assertThat(longFormat.getList("author").get(0), is(security.getUserId()));
        assertThat(longFormat.getList("id").size(), is(1));
        assertThat(longFormat.getList("id").get(0), is(preparationId));
        assertThat(longFormat.getList("actions").size(), is(1));
        assertThat(((List) longFormat.getList("actions").get(0)).size(), is(0));

        // when : summary format
        final JsonPath summaryFormat = when().get("/api/preparations/?format=summary").jsonPath();

        // then
        assertThat(summaryFormat.getList("id").size(), is(1));
        assertThat(summaryFormat.getList("id").get(0), is(preparationId));
        assertThat(summaryFormat.getList("name").size(), is(1));
        assertThat(summaryFormat.getList("name").get(0), is("testPreparation"));
        assertThat(summaryFormat.getList("owner").size(), is(1));
        assertThat(summaryFormat.getList("lastModificationDate").size(), is(1));
        assertThat(summaryFormat.getList("allowDistributedRun").size(), is(1));
    }

    @Test
    public void testPreparationsList_withFilter() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        String preparationId = testClient.createPreparationFromDataset(tagadaId, "testPreparation", home.getId());

        // when : short format
        final JsonPath shortFormat = when().get("/api/preparations/?format=short&name={name}", "testPreparation").jsonPath();

        // then
        final List<String> values = shortFormat.getList("");
        assertThat(values.get(0), is(preparationId));
    }

    @Test
    public void testPreparationGet() throws Exception {
        // when
        final String datasetId = testClient.createDataset("dataset/dataset.csv", "great dataset", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(datasetId, "1234", home.getId());

        // then
        final JsonPath longFormat = given().get("/api/preparations/{id}/details", preparationId).jsonPath();
        assertThat(longFormat.getString("dataSetId"), is(datasetId));
        assertThat(longFormat.getString("author"), is(security.getUserId()));
        assertThat(longFormat.getString("id"), is(preparationId));
        assertThat(longFormat.getList("actions").size(), is(0));

        assertNotNull(longFormat.getMap("rowMetadata"));
        assertNotNull(longFormat.getMap("rowMetadata").get("columns"));
        ArrayList rowMetaData = (ArrayList) longFormat.getMap("rowMetadata").get("columns");
        assertThat(rowMetaData.size(), is(6));

        assertThat(longFormat.getString("allowFullRun"), is("false"));
        final List<String> steps = longFormat.getList("steps"); // make sure the "steps" node is a string array
        assertThat(steps.size(), is(1));
    }

    @Test
    public void testListCompatibleDataSets() throws Exception {
        // given
        final String dataSetId = testClient.createDataset("dataset/dataset.csv", "compatible1", "text/csv");
        final String dataSetId2 = testClient.createDataset("dataset/dataset.csv", "compatible2", "text/csv");
        final String dataSetId3 = testClient.createDataset("t-shirt_100.csv", "incompatible", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(dataSetId, "testPreparation", home.getId());

        // when
        final String compatibleDatasetList = when().get("/api/preparations/{id}/basedatasets", preparationId).asString();

        // then
        assertTrue(compatibleDatasetList.contains(dataSetId2));
        assertFalse(compatibleDatasetList.contains(dataSetId3));
    }

    @Test
    public void testListCompatibleDataSetsWhenUniqueDatasetInRepository() throws Exception {
        // given
        final String dataSetId = testClient.createDataset("dataset/dataset.csv", "unique", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(dataSetId, "testPreparation", home.getId());

        // when
        final String compatibleDatasetList = when().get("/api/preparations/{id}/basedatasets", preparationId).asString();

        // then
        assertFalse(compatibleDatasetList.contains(dataSetId));
    }

    @Test
    public void shouldCopyPreparation() throws Exception {
        // given
        Folder destination = folderRepository.addFolder(home.getId(), "/destination");
        Folder origin = folderRepository.addFolder(home.getId(), "/from");
        final String preparationId = testClient.createPreparationFromFile("dataset/dataset.csv", "super preparation", "text/csv",
                origin.getId());

        // when
        String newPreparationName = "NEW super preparation";
        final Response response = given() //
                .queryParam("destination", destination.getId()) //
                .queryParam("newName", newPreparationName) //
                .when()//
                .expect().statusCode(200).log().ifError() //
                .post("api/preparations/{id}/copy", preparationId);

        // then
        assertEquals(200, response.getStatusCode());
        String copyId = response.asString();

        // check the folder entry
        final Iterator<FolderEntry> iterator = folderRepository.entries(destination.getId(), PREPARATION).iterator();
        assertTrue(iterator.hasNext());
        final FolderEntry entry = iterator.next();
        assertEquals(entry.getContentId(), copyId);

        // check the name
        final Preparation actual = preparationRepository.get(copyId, Preparation.class);
        assertEquals(newPreparationName, actual.getName());
    }

    @Test
    public void copyPreparationShouldForwardExceptions() throws Exception {

        // when
        final Response response = given() //
                .queryParam("destination", "/destination") //
                .when()//
                .expect().statusCode(404).log().ifError() //
                .post("api/preparations/{id}/copy", "preparation_not_found");

        // then
        assertEquals(404, response.getStatusCode());
    }

    @Test
    public void shouldMovePreparation() throws Exception {
        // given
        final Folder source = folderRepository.addFolder(home.getId(), "source");
        final String id = testClient.createPreparationFromFile("dataset/dataset.csv", "great_preparation", "text/csv",
                source.getId());

        final Folder destination = folderRepository.addFolder(home.getId(), "destination");

        // when
        final Response response = given() //
                .queryParam("folder", source.getId()) //
                .queryParam("destination", destination.getId()) //
                .queryParam("newName", "NEW great preparation") //
                .when()//
                .expect().statusCode(200).log().ifError() //
                .put("api/preparations/{id}/move", id);

        // then
        assertEquals(200, response.getStatusCode());

        // check the folder entry
        final Iterator<FolderEntry> iterator = folderRepository.entries(destination.getId(), PREPARATION).iterator();
        assertTrue(iterator.hasNext());
        final FolderEntry entry = iterator.next();
        assertEquals(entry.getContentId(), id);

        // check the name
        final Preparation actual = preparationRepository.get(id, Preparation.class);
        assertEquals("NEW great preparation", actual.getName());
    }

    @Test
    public void movePreparationShouldForwardExceptions() throws Exception {

        // when
        final Response response = given() //
                .queryParam("folder", "/from") //
                .queryParam("destination", "/to") //
                .queryParam("newName", "NEW great preparation") //
                .when()//
                .expect().statusCode(404).log().ifError() //
                .put("api/preparations/{id}/move", "unknown_preparation");

        // then
        assertEquals(404, response.getStatusCode());
    }

    /**
     *
     * @see <a href="https://jira.talendforge.org/browse/TDP-3965">TDP-3965</a>
     */
    @Test
    public void ensureThatPreparationDetailsCanBeParsedAsStandalonePreparation_TDP_3965() throws Exception {
        // when
        final String preparationId = testClient.createPreparationFromFile("dataset/dataset.csv", "testPreparation", "text/csv",
                home.getId());

        // when
        testClient.applyActionFromFile(preparationId, "transformation/upper_case_firstname.json");
        InputStream inputStream = given().expect().statusCode(200).get("/api/preparations/{preparation}/details", preparationId)
                .asInputStream();

        // then
        try {
            mapper.readerFor(StandalonePreparation.class).readValue(inputStream);
        } catch (Exception e) {
            fail();
        }
    }

    // ------------------------------------------------------------------------------------------------------------------
    // ----------------------------------------------------LIFECYCLE-----------------------------------------------------
    // ------------------------------------------------------------------------------------------------------------------
    @Test
    public void testPreparationUpdate() throws Exception {
        // given
        String tagadaId = testClient.createDataset("dataset/dataset.csv", "tagada", "text/csv");
        final String preparationId = testClient.createPreparationFromDataset(tagadaId, "original_name", home.getId());

        JsonPath longFormat = when().get("/api/preparations/?format=long").jsonPath();
        assertThat(longFormat.getList("name").size(), is(1));
        assertThat(longFormat.getList("name").get(0), is("original_name"));
        assertThat(longFormat.getList("id").size(), is(1));
        assertThat(longFormat.getList("id").get(0), is(preparationId));

        Preparation preparation = new Preparation();
        preparation.setId(preparationId);
        String newName = "updated_name";
        preparation.setName(newName);

        // when
        given().contentType(ContentType.JSON).body(preparation).put("/api/preparations/{id}", preparationId).asString();

        // then
        longFormat = when().get("/api/preparations/?format=long").jsonPath();
        assertThat(longFormat.getList("name").size(), is(1));
        assertThat(longFormat.getList("name").get(0), is(newName));
    }

{'fraction_non_alphanumeric': 0.18073589637766277, 'fraction_numerical': 0.0069526016698942825, 'mean_word_length': 2.806646525679758, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683063', 'n_tokens_mistral': 8427, 'n_tokens_neox': 8149, 'n_words': 1430}