Search in sources :

Example 1 with TransformationContext

use of org.talend.dataprep.transformation.api.action.context.TransformationContext in project data-prep by Talend.

the class DateCalendarConverterTest method testCompileShouldComputeToAndFromInternalContextParameters.

@Test
public void testCompileShouldComputeToAndFromInternalContextParameters() {
    // Given
    final DateCalendarConverter calendarConverter = new DateCalendarConverter();
    final DataSetRow row = builder().with(value("toto").type(Type.STRING).name("recipe")).with(value("10/29/1996").type(Type.DATE).name("last update").statistics(getDateTestJsonAsStream("statistics_MM_dd_yyyy.json"))).with(// 
    value("tata").type(Type.STRING).name("who")).build();
    final ActionContext context = new ActionContext(new TransformationContext(), row.getRowMetadata());
    context.setActionStatus(ActionContext.ActionStatus.OK);
    final Map<String, String> parameters = new HashMap<>();
    parameters.put(ImplicitParameters.SCOPE.getKey().toLowerCase(), "column");
    parameters.put(COLUMN_ID.getKey(), "0001");
    parameters.put(FROM_CALENDAR_TYPE_PARAMETER, DateCalendarConverter.CalendarUnit.ISO.name());
    parameters.put(TO_CALENDAR_TYPE_PARAMETER, CalendarUnit.JULIAN_DAY.name());
    context.setParameters(parameters);
    // When
    calendarConverter.compile(context);
    // Then
    assertTrue(context.get(IS_FROM_CHRONOLOGY_INTERNAL_KEY));
    assertFalse(context.get(IS_TO_CHRONOLOGY_INTERNAL_KEY));
}
Also used : DateCalendarConverter(org.talend.dataprep.transformation.actions.date.DateCalendarConverter) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test)

Example 2 with TransformationContext

use of org.talend.dataprep.transformation.api.action.context.TransformationContext in project data-prep by Talend.

the class ReplaceOnValueTest method buildPatternActionContext.

private ActionContext buildPatternActionContext(String regex, String replacement, boolean replace) {
    ActionContext context = new ActionContext(new TransformationContext());
    Map<String, String> parameters = new HashMap<>();
    parameters.put(ReplaceOnValue.CELL_VALUE_PARAMETER, regex);
    parameters.put(ReplaceOnValue.REPLACE_VALUE_PARAMETER, replacement);
    parameters.put(ReplaceOnValue.REPLACE_ENTIRE_CELL_PARAMETER, String.valueOf(replace));
    context.setParameters(parameters);
    action.compile(context);
    return context;
}
Also used : HashMap(java.util.HashMap) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext)

Example 3 with TransformationContext

use of org.talend.dataprep.transformation.api.action.context.TransformationContext in project data-prep by Talend.

the class FillWithBooleanIfInvalidTest method should_not_fill_non_valid_boolean.

@Test
public void should_not_fill_non_valid_boolean() throws Exception {
    // given
    final Map<String, String> values = new HashMap<>();
    values.put("0000", "David Bowie");
    values.put("0001", "N");
    values.put("0002", "False");
    final DataSetRow row = new DataSetRow(values);
    final RowMetadata rowMetadata = row.getRowMetadata();
    rowMetadata.getById("0002").setType(Type.BOOLEAN.getName());
    Map<String, String> parameters = ActionMetadataTestUtils.parseParameters(this.getClass().getResourceAsStream("fillInvalidBooleanAction.json"));
    parameters.put(ImplicitParameters.COLUMN_ID.getKey().toLowerCase(), "0002");
    // when
    final RunnableAction runnableAction = factory.create(action, parameters);
    final ActionContext context = new ActionContext(new TransformationContext(), rowMetadata);
    context.setParameters(parameters);
    runnableAction.getRowAction().apply(row, context);
    // then
    assertEquals("False", row.get("0002"));
    assertEquals("David Bowie", row.get("0000"));
}
Also used : HashMap(java.util.HashMap) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test) AbstractMetadataBaseTest(org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)

Example 4 with TransformationContext

use of org.talend.dataprep.transformation.api.action.context.TransformationContext in project data-prep by Talend.

the class FillWithBooleanIfInvalidTest method should_fill_non_valid_boolean.

@Test
public void should_fill_non_valid_boolean() throws Exception {
    // given
    final Map<String, String> values = new HashMap<>();
    values.put("0000", "David Bowie");
    values.put("0001", "N");
    // invalid boolean
    values.put("0002", "100");
    final DataSetRow row = new DataSetRow(values);
    row.setInvalid("0002");
    final RowMetadata rowMetadata = row.getRowMetadata();
    rowMetadata.getById("0002").setType(Type.BOOLEAN.getName());
    Map<String, String> parameters = ActionMetadataTestUtils.parseParameters(this.getClass().getResourceAsStream("fillInvalidBooleanAction.json"));
    parameters.put(ImplicitParameters.COLUMN_ID.getKey().toLowerCase(), "0002");
    // when
    final RunnableAction runnableAction = factory.create(action, parameters);
    final ActionContext context = new ActionContext(new TransformationContext(), rowMetadata);
    context.setParameters(parameters);
    runnableAction.getRowAction().apply(row, context);
    // then
    assertEquals("True", row.get("0002"));
    assertEquals("David Bowie", row.get("0000"));
}
Also used : HashMap(java.util.HashMap) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test) AbstractMetadataBaseTest(org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)

Example 5 with TransformationContext

use of org.talend.dataprep.transformation.api.action.context.TransformationContext in project data-prep by Talend.

the class FillWithStringIfInvalidTest method should_fill_empty_string_other_column.

@Test
public void should_fill_empty_string_other_column() throws Exception {
    // given
    final Map<String, String> values = new HashMap<>();
    values.put("0000", "David Bowie");
    values.put("0001", "DC");
    values.put("0002", "Something");
    final DataSetRow row = new DataSetRow(values);
    row.setInvalid("0001");
    final RowMetadata rowMetadata = row.getRowMetadata();
    rowMetadata.getById("0001").setType(Type.STRING.getName());
    rowMetadata.getById("0002").setType(Type.STRING.getName());
    Map<String, String> parameters = // 
    ActionMetadataTestUtils.parseParameters(this.getClass().getResourceAsStream("fillInvalidStringAction.json"));
    // when
    parameters.put(FillIfEmpty.MODE_PARAMETER, FillIfEmpty.OTHER_COLUMN_MODE);
    parameters.put(FillIfEmpty.SELECTED_COLUMN_PARAMETER, "0002");
    final RunnableAction runnableAction = factory.create(action, parameters);
    final ActionContext context = new ActionContext(new TransformationContext(), rowMetadata);
    context.setParameters(parameters);
    runnableAction.getRowAction().apply(row, context);
    // then
    Assert.assertEquals("Something", row.get("0001"));
    Assert.assertEquals("David Bowie", row.get("0000"));
}
Also used : HashMap(java.util.HashMap) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) Test(org.junit.Test) AbstractMetadataBaseTest(org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)

Aggregations

TransformationContext (org.talend.dataprep.transformation.api.action.context.TransformationContext)27 ActionContext (org.talend.dataprep.transformation.api.action.context.ActionContext)26 RunnableAction (org.talend.dataprep.transformation.actions.common.RunnableAction)22 Test (org.junit.Test)20 RowMetadata (org.talend.dataprep.api.dataset.RowMetadata)18 DataSetRow (org.talend.dataprep.api.dataset.row.DataSetRow)16 HashMap (java.util.HashMap)14 AbstractMetadataBaseTest (org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)11 DataSetRowAction (org.talend.dataprep.transformation.api.action.DataSetRowAction)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ColumnMetadata (org.talend.dataprep.api.dataset.ColumnMetadata)4 LinkedHashMap (java.util.LinkedHashMap)3 Before (org.junit.Before)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CoreMatchers (org.hamcrest.CoreMatchers)2 CoreMatchers.is (org.hamcrest.CoreMatchers.is)2 Assert (org.junit.Assert)2 DataSet (org.talend.dataprep.api.dataset.DataSet)2 DataSetMetadata (org.talend.dataprep.api.dataset.DataSetMetadata)2