Search in sources :

Example 6 with TransformationContext

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

the class FillWithStringIfInvalidTest method should_fill_non_valid_string.

@Test
public void should_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", "100");
    final DataSetRow row = new DataSetRow(values);
    row.setInvalid("0002");
    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("beer", 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 7 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_because_valid.

@Test
public void should_not_clear_because_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);
    final RowMetadata rowMetadata = row.getRowMetadata();
    rowMetadata.getById("0001").setType(Type.STRING.getName());
    final Map<String, Object> expectedValues = new HashMap<>();
    expectedValues.put("0000", "David Bowie");
    expectedValues.put("0001", "N");
    expectedValues.put("0002", "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) Test(org.junit.Test) AbstractMetadataBaseTest(org.talend.dataprep.transformation.actions.AbstractMetadataBaseTest)

Example 8 with TransformationContext

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

the class ClearInvalidTest method test_apply_inplace.

@Test
public void test_apply_inplace() {
    // 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());
    final Map<String, Object> expectedValues = new LinkedHashMap<>();
    expectedValues.put("__tdpInvalid", "0001");
    expectedValues.put("0000", "David Bowie");
    expectedValues.put("0001", "");
    expectedValues.put("0002", "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 9 with TransformationContext

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

the class PipelineTest method testCompileAction.

@Test
public void testCompileAction() throws Exception {
    // Given
    final RunnableAction mockAction = new RunnableAction() {

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

                @Override
                public void compile(ActionContext actionContext) {
                    actionContext.get("ExecutedCompile", p -> true);
                }

                @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
    assertFalse(actionContext.has("ExecutedCompile"));
    node.exec().receive(row, rowMetadata);
    // then
    assertTrue(actionContext.has("ExecutedCompile"));
    assertTrue(actionContext.get("ExecutedCompile"));
}
Also used : DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) 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)

Example 10 with TransformationContext

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

the class PipelineTest method testAction.

@Test
public void testAction() throws Exception {
    // Given
    final RunnableAction mockAction = new RunnableAction() {

        @Override
        public DataSetRowAction getRowAction() {
            return (r, context) -> {
                context.get("ExecutedApply", p -> true);
                return r;
            };
        }
    };
    final ActionContext actionContext = new ActionContext(new TransformationContext());
    final Node node = NodeBuilder.source().to(new ActionNode(mockAction, actionContext)).to(output).build();
    final RowMetadata rowMetadata = new RowMetadata();
    final DataSetRow row = new DataSetRow(rowMetadata);
    // when
    assertFalse(actionContext.has("ExecutedApply"));
    node.exec().receive(row, rowMetadata);
    // then
    assertTrue(actionContext.has("ExecutedApply"));
    assertTrue(actionContext.get("ExecutedApply"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) CoreMatchers(org.hamcrest.CoreMatchers) BasicLink(org.talend.dataprep.transformation.pipeline.link.BasicLink) HashMap(java.util.HashMap) Test(org.junit.Test) TransformationContext(org.talend.dataprep.transformation.api.action.context.TransformationContext) ActionContext(org.talend.dataprep.transformation.api.action.context.ActionContext) ArrayList(java.util.ArrayList) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.talend.dataprep.transformation.pipeline.node(org.talend.dataprep.transformation.pipeline.node) END_OF_STREAM(org.talend.dataprep.transformation.pipeline.Signal.END_OF_STREAM) DataSetRow(org.talend.dataprep.api.dataset.row.DataSetRow) CloneLink(org.talend.dataprep.transformation.pipeline.link.CloneLink) DataSetMetadata(org.talend.dataprep.api.dataset.DataSetMetadata) DataSetRowAction(org.talend.dataprep.transformation.api.action.DataSetRowAction) Assert(org.junit.Assert) DataSet(org.talend.dataprep.api.dataset.DataSet) NodeBuilder(org.talend.dataprep.transformation.pipeline.builder.NodeBuilder) Before(org.junit.Before) ColumnMetadata(org.talend.dataprep.api.dataset.ColumnMetadata) RowMetadata(org.talend.dataprep.api.dataset.RowMetadata) RunnableAction(org.talend.dataprep.transformation.actions.common.RunnableAction) 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