Search in sources :

Example 1 with Action

use of org.talend.dataprep.api.preparation.Action in project data-prep by Talend.

the class ActionParserTest method should_return_expected_actions.

@Test
public void should_return_expected_actions() throws IOException {
    String json = IOUtils.toString(ActionParserTest.class.getResourceAsStream("actions.json"), UTF_8);
    // when
    List<RunnableAction> actualActions = actionParser.parse(json);
    // then
    assertTrue(actualActions.size() == 1);
    Action actionParsed = actualActions.get(0);
    assertEquals("lookup", actionParsed.getName());
}
Also used : Action(org.talend.dataprep.api.preparation.Action) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) Test(org.junit.Test)

Example 2 with Action

use of org.talend.dataprep.api.preparation.Action in project data-prep by Talend.

the class SerializableTest method testSerializableClass.

@Test
public void testSerializableClass() throws Exception {
    LOGGER.info("{} actions for testing...", actions.length);
    int okActions = 0;
    for (ActionDefinition action : actions) {
        Map<String, String> parameters = new HashMap<>();
        if (action.acceptScope(ScopeCategory.COLUMN)) {
            parameters.put(ImplicitParameters.SCOPE.getKey(), "COLUMN");
        } else if (action.acceptScope(ScopeCategory.CELL)) {
            parameters.put(ImplicitParameters.SCOPE.getKey(), "CELL");
        } else if (action.acceptScope(ScopeCategory.LINE)) {
            parameters.put(ImplicitParameters.SCOPE.getKey(), "LINE");
        } else if (action.acceptScope(ScopeCategory.DATASET)) {
            parameters.put(ImplicitParameters.SCOPE.getKey(), "DATASET");
        }
        parameters.put(ImplicitParameters.COLUMN_ID.getKey(), "0000");
        parameters.put(ImplicitParameters.ROW_ID.getKey(), "0");
        final Action actionInstance = factory.create(action, parameters);
        try {
            try (ObjectOutputStream oos = new ObjectOutputStream(new NullOutputStream())) {
                oos.writeObject(actionInstance);
                oos.flush();
            }
            okActions++;
        } catch (IOException e) {
            LOGGER.error("Unable to serialize {}", action.getClass());
        }
    }
    LOGGER.info("{} actions for tested ({}/{} ok).", actions.length, okActions, actions.length);
}
Also used : Action(org.talend.dataprep.api.preparation.Action) HashMap(java.util.HashMap) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition) NullOutputStream(org.apache.commons.io.output.NullOutputStream) Test(org.junit.Test)

Example 3 with Action

use of org.talend.dataprep.api.preparation.Action in project data-prep by Talend.

the class PreparationAPITest method testPreparationPreviewOnPreparationWithTrimAction_TDP_5057.

/**
 * Verify a calculate time since preview after a trim step on a preparation
 * see <a href="https://jira.talendforge.org/browse/TDP-5057">TDP-5057</a>
 */
@Test
public void testPreparationPreviewOnPreparationWithTrimAction_TDP_5057() throws IOException {
    // Create a dataset from csv
    final String datasetId = testClient.createDataset("preview/best_sad_songs_of_all_time.csv", "testPreview");
    // Create a preparation
    String preparationId = testClient.createPreparationFromDataset(datasetId, "testPrep", home.getId());
    // apply trim action on the 8nd column to make this column date valid
    Map<String, String> trimParameters = new HashMap<>();
    trimParameters.put("create_new_column", "false");
    trimParameters.put("padding_character", "whitespace");
    trimParameters.put("scope", "column");
    trimParameters.put("column_id", "0008");
    trimParameters.put("column_name", "Added At");
    trimParameters.put("row_id", "null");
    testClient.applyAction(preparationId, Trim.TRIM_ACTION_NAME, trimParameters);
    // check column is date valid after trim action
    InputStream inputStream = testClient.getPreparation(preparationId).asInputStream();
    mapper.getDeserializationConfig().without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    RowMetadata preparationContent = mapper.readValue(inputStream, Data.class).metadata;
    List<PatternFrequency> patternFrequencies = preparationContent.getColumns().get(8).getStatistics().getPatternFrequencies();
    assertTrue(patternFrequencies.stream().map(// 
    PatternFrequency::getPattern).anyMatch("yyyy-MM-dd"::equals));
    // create a preview of calculate time since action
    PreviewAddParameters previewAddParameters = new PreviewAddParameters();
    previewAddParameters.setDatasetId(datasetId);
    previewAddParameters.setPreparationId(preparationId);
    previewAddParameters.setTdpIds(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
    Action calculateTimeUntilAction = new Action();
    calculateTimeUntilAction.setName(ComputeTimeSince.TIME_SINCE_ACTION_NAME);
    MixedContentMap actionParameters = new MixedContentMap();
    actionParameters.put("create_new_column", "true");
    actionParameters.put("time_unit", "HOURS");
    actionParameters.put("since_when", "now_server_side");
    actionParameters.put("scope", "column");
    actionParameters.put("column_id", "0008");
    actionParameters.put("column_name", "Added At");
    calculateTimeUntilAction.setParameters(actionParameters);
    previewAddParameters.setActions(Collections.singletonList(calculateTimeUntilAction));
    JsonPath jsonPath = given().contentType(// 
    ContentType.JSON).body(// 
    previewAddParameters).expect().statusCode(200).log().ifError().when().post(// 
    "/api/preparations/preview/add").jsonPath();
    // check non empty value for the new column
    assertEquals(// 
    "new preview column should contains values according to calculate time since action", // 
    0, jsonPath.getList("records.0009").stream().map(String::valueOf).filter(StringUtils::isBlank).count());
}
Also used : Action(org.talend.dataprep.api.preparation.Action) EntityBuilder.buildAction(org.talend.dataprep.api.service.EntityBuilder.buildAction) HashMap(java.util.HashMap) InputStream(java.io.InputStream) JsonPath(com.jayway.restassured.path.json.JsonPath) PreviewAddParameters(org.talend.dataprep.api.service.api.PreviewAddParameters) PatternFrequency(org.talend.dataprep.api.dataset.statistics.PatternFrequency) StringUtils(org.apache.commons.lang.StringUtils) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) MixedContentMap(org.talend.dataprep.api.preparation.MixedContentMap) Test(org.junit.Test)

Example 4 with Action

use of org.talend.dataprep.api.preparation.Action in project data-prep by Talend.

the class APIClientTest method applyAction.

/**
 * Add a step to a preparation.
 *
 * @param preparationId the preparation id.
 * @param actionName action name
 * @param parameters action parameters
 * @throws IOException sh*t happens.
 */
public void applyAction(final String preparationId, final String actionName, Map<String, String> parameters) throws IOException {
    org.talend.dataprep.api.preparation.Actions actions = new org.talend.dataprep.api.preparation.Actions();
    Action action = new Action();
    action.setName(actionName);
    action.setParameters(MixedContentMap.convert(parameters));
    actions.setActions(Collections.singletonList(action));
    given().contentType(JSON.withCharset(UTF_8)).content(// 
    actions).when().post("/api/preparations/{id}/actions", // 
    preparationId).then().statusCode(is(200));
}
Also used : Action(org.talend.dataprep.api.preparation.Action)

Example 5 with Action

use of org.talend.dataprep.api.preparation.Action in project data-prep by Talend.

the class ActionsStaticProfiler method getActionMetadataByAction.

/**
 * Get the actions metadata by actions
 */
private Map<Action, ActionDefinition> getActionMetadataByAction(final List<RunnableAction> actions) {
    final Map<Action, ActionDefinition> actionToMetadata = new HashMap<>(actions.size());
    for (final Action action : actions) {
        final ActionDefinition actionMetadata = // 
        actionRegistry.get(action.getName()).adapt(ScopeCategory.from(action.getParameters().get(ImplicitParameters.SCOPE.getKey())));
        actionToMetadata.put(action, actionMetadata);
    }
    return actionToMetadata;
}
Also used : Action(org.talend.dataprep.api.preparation.Action) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) ActionDefinition(org.talend.dataprep.api.action.ActionDefinition)

Aggregations

Action (org.talend.dataprep.api.preparation.Action)27 Test (org.junit.Test)14 PreparationActions (org.talend.dataprep.api.preparation.PreparationActions)12 ArrayList (java.util.ArrayList)9 List (java.util.List)7 PreparationRepository (org.talend.dataprep.preparation.store.PreparationRepository)7 Preparation (org.talend.dataprep.api.preparation.Preparation)6 Step (org.talend.dataprep.api.preparation.Step)6 PersistentStep (org.talend.dataprep.preparation.store.PersistentStep)6 IOException (java.io.IOException)4 Collections (java.util.Collections)4 Stream (java.util.stream.Stream)4 ActionDefinition (org.talend.dataprep.api.action.ActionDefinition)4 RunnableAction (org.talend.dataprep.transformation.actions.common.RunnableAction)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 StringUtils (org.apache.commons.lang.StringUtils)3 InjectMocks (org.mockito.InjectMocks)3 Mock (org.mockito.Mock)3