ReorderStepsUtils { void renameCreatedColumns(List<AppendStep> appendSteps) { final List<String> createdColumns = appendSteps .stream() .flatMap(step -> step.getDiff().getCreatedColumns().stream()) .collect(Collectors.toList()); if (createdColumns.isEmpty()) { return; } final int firstIndex = Integer.parseInt(createdColumns.stream().min(String::compareTo).get()); final DecimalFormat format = new DecimalFormat("0000"); Map<String, String> rename = new HashMap<>(); IntStream.range(0, createdColumns.size()).forEach(i -> { rename.put(createdColumns.get(i), format.format(i + firstIndex)); }); appendSteps.stream().forEach(step -> { List<String> renamedCreatedColumns = step.getDiff().getCreatedColumns().stream().map(s -> { if (rename.containsKey(s)) { return rename.get(s); } else { return s; } }).collect(Collectors.toList()); step.getDiff().setCreatedColumns(renamedCreatedColumns); for (Action action : step.getActions()) { final Map<String, String> parameters = action.getParameters(); final String columnId = parameters.get(ImplicitParameters.COLUMN_ID.getKey()); if (rename.containsKey(columnId)) { parameters.put(ImplicitParameters.COLUMN_ID.getKey(), rename.get(columnId)); } } }); }    }
@Test public void renameCreatedColumns_reorderCorrectlyColumnIdsAndUpdateRefs() throws Exception { List<AppendStep> steps = new ArrayList<>(); AppendStep firstStep = createAppendStep("0002", singletonList("0004")); steps.add(firstStep); AppendStep secondStep = createAppendStep("0002", singletonList("0003")); steps.add(secondStep); AppendStep thirdStep = createAppendStep("0003", emptyList()); steps.add(thirdStep); reorderStepsUtils.renameCreatedColumns(steps); assertEquals("0003", firstStep.getDiff().getCreatedColumns().iterator().next()); assertEquals("0004", secondStep.getDiff().getCreatedColumns().iterator().next()); assertEquals("0004", thirdStep.getActions().iterator().next().getParameters().get(ImplicitParameters.COLUMN_ID.getKey())); }