@Test public void should_fill_empty_string_other_column_not_date() throws Exception { // given final DataSetRow row = builder() // .with(value("David Bowie").type(Type.STRING)) // .with( value("") .type(Type.DATE) .statistics( ChangeDatePatternTest.class.getResourceAsStream( "statistics_yyyy-MM-dd.json"))) // .with(value("tagada").type(Type.DATE)) // .build(); Map<String, String> parameters = ActionMetadataTestUtils.parseParameters( // this.getClass().getResourceAsStream("fillEmptyIntegerAction.json")); // when parameters.put(FillIfEmpty.MODE_PARAMETER, FillIfEmpty.OTHER_COLUMN_MODE); parameters.put(FillIfEmpty.SELECTED_COLUMN_PARAMETER, "0002"); ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters)); // then assertEquals("tagada", row.get("0002")); assertEquals("tagada", row.get("0001")); assertEquals("David Bowie", row.get("0000")); }
@Test public void test_TDP_591() throws Exception { // given final DataSetRow row = builder() // .with(value("David Bowie").type(Type.STRING)) // .with( value("") .type(Type.DATE) .statistics( ChangeDatePatternTest.class.getResourceAsStream( "statistics_yyyy-MM-dd.json"))) // .with(value("100").type(Type.STRING)) // .build(); Map<String, String> parameters = ActionMetadataTestUtils.parseParameters( this.getClass().getResourceAsStream("fillEmptyDateAction.json")); // when ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters)); // then assertEquals("1970-01-01", row.get("0001")); assertEquals("David Bowie", row.get("0000")); }
/** @see ColumnAction#applyOnColumn(DataSetRow, ActionContext) */ @Override public void applyOnColumn(DataSetRow row, ActionContext context) { // create new column and append it after current column final RowMetadata rowMetadata = context.getRowMetadata(); final String columnId = context.getColumnId(); final ColumnMetadata column = rowMetadata.getById(columnId); final String lengthColumn = context.column(column.getName() + APPENDIX); // Set length value final String value = row.get(columnId); row.set(lengthColumn, value == null ? "0" : String.valueOf(value.length())); }
@Test public void should_not_change_other_columns() { // given final Map<String, String> values = new HashMap<>(); values.put("0001", "the beatles"); final DataSetRow row = new DataSetRow(values); // when ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters)); // then assertEquals("the beatles", row.get("0001")); }
@Test public void should_transform_upper_to_proper() { // given final Map<String, String> values = new HashMap<>(); values.put("0000", "THE BEATLES"); final DataSetRow row = new DataSetRow(values); // when ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters)); // then assertEquals("The Beatles", row.get("0000")); }
@Test public void should_not_fill_empty_date() throws Exception { // given final DataSetRow row = builder() // .with(value("David Bowie").type(Type.STRING)) // .with(value("not empty").type(Type.DATE)) // .with(value("100").type(Type.STRING)) // .build(); Map<String, String> parameters = ActionMetadataTestUtils.parseParameters( // this.getClass().getResourceAsStream("fillEmptyDateAction.json")); // when ActionTestWorkbench.test(row, actionRegistry, factory.create(action, parameters)); // then assertEquals("1/1/1970 10:00:00", row.get("0001")); assertEquals("David Bowie", row.get("0000")); }
void performColumnFilter(DataSetRow row, RowMetadata metadata) { final boolean needRefresh = rowMetadata == null || !metadata.equals(rowMetadata); List<ColumnMetadata> columns = metadata.getColumns(); if (!columns.isEmpty()) { if (filteredColumns == null || needRefresh) { filteredColumns = columns.stream().filter(filter).collect(Collectors.toList()); filteredColumnNames = filteredColumns.stream().map(ColumnMetadata::getId).collect(Collectors.toSet()); } } else { // No column in row metadata, guess all type, starting from string columns. ColumnMetadata.Builder builder = ColumnMetadata.Builder.column().type(Type.STRING); final int rowSize = row.toArray(DataSetRow.SKIP_TDP_ID).length; columns = new ArrayList<>(rowSize + 1); for (int i = 0; i < rowSize; i++) { final ColumnMetadata newColumn = builder.build(); metadata.addColumn(newColumn); columns.add(newColumn); } filteredColumns = columns; filteredColumnNames = columns.stream().map(ColumnMetadata::getId).collect(Collectors.toSet()); } rowMetadata = metadata; }
@Override public void applyOnColumn(DataSetRow row, ActionContext context) { if (context.getFilter().test(row)) { row.setDeleted(true); } }