Search in sources :

Example 16 with TransformationContext

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

the class FillWithStringIfInvalidTest method should_not_fill_non_valid_string.

@Test
public void should_not_fill_non_valid_string() throws Exception {
    // given
    final Map<String, String> values = new HashMap<>();
    values.put("0000", "David Bowie");
    values.put("0001", "N");
    values.put("0002", "wine");
    final DataSetRow row = new DataSetRow(values);
    final RowMetadata rowMetadata = row.getRowMetadata();
    rowMetadata.getById("0002").setType(Type.STRING.getName());
    Map<String, String> parameters = ActionMetadataTestUtils.parseParameters(this.getClass().getResourceAsStream("fillInvalidStringAction.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("wine", 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 17 with TransformationContext

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

the class ClearInvalidTest method should_not_clear_invalid_date.

@Test
public void should_not_clear_invalid_date() {
    // given
    final Map<String, String> values = new HashMap<>();
    values.put("0001", "David Bowie");
    values.put("0002", "20-09-1975");
    values.put("0003", "Something");
    final DataSetRow row = new DataSetRow(values);
    final RowMetadata rowMetadata = row.getRowMetadata();
    rowMetadata.getById("0002").setType(Type.DATE.getName());
    final Map<String, Object> expectedValues = new LinkedHashMap<>();
    expectedValues.put("0001", "David Bowie");
    expectedValues.put("0002", "20-09-1975");
    expectedValues.put("0003", "Something");
    // 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(expectedValues, row.values());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) AbstractMetadataBaseTest(org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)

Example 18 with TransformationContext

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

the class DeleteInvalidTest method should_delete_because_non_valid.

@Test
public void should_delete_because_non_valid() {
    // given
    final Map<String, String> values = new HashMap<>();
    values.put("0000", "David Bowie");
    values.put("0001", "N");
    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());
    // 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
    assertTrue(row.isDeleted());
    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 19 with TransformationContext

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

the class PipelineTest method testCompileNode.

@Test
public void testCompileNode() throws Exception {
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    final RunnableAction mockAction = new RunnableAction();
    CompileNode compileNode = new CompileNode(mockAction, actionContext);
    assertEquals(actionContext, compileNode.getActionContext());
    assertEquals(mockAction, compileNode.getAction());
}
Also used : RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) Test(org.junit.Test)

Example 20 with TransformationContext

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

the class PipelineTest method testRecompileAction.

@Test
public void testRecompileAction() throws Exception {
    // Given
    AtomicInteger compileCount = new AtomicInteger();
    final RunnableAction mockAction = new RunnableAction() {

        @Override
        public DataSetRowAction getRowAction() {
            return new DataSetRowAction() {

                @Override
                public void compile(ActionContext actionContext) {
                    compileCount.incrementAndGet();
                    actionContext.setActionStatus(ActionContext.ActionStatus.OK);
                }

                @Override
                public DataSetRow apply(DataSetRow dataSetRow, ActionContext context) {
                    return dataSetRow;
                }
            };
        }
    };
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    final Node node = NodeBuilder.source().to(new CompileNode(mockAction, actionContext)).to(output).build();
    final RowMetadata rowMetadata = new RowMetadata();
    final DataSetRow row = new DataSetRow(rowMetadata);
    // when
    assertEquals(0, compileCount.get());
    node.exec().receive(row, rowMetadata);
    // Change row metadata in middle of the transformation (to trigger
    rowMetadata.addColumn(new ColumnMetadata());
    // new compile).
    node.exec().receive(row, rowMetadata);
    // then
    assertEquals(2, compileCount.get());
}
Also used : DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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)

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