TextImportTemplate { public List<T> execute() { List<T> results = new ArrayList<>(); for (int i = 0; i < lines.size(); i++) { String[] row = lines.get(i); if (row != null && i > 0 && isAppropriateRow(row)) { try { executeRow(i, row, results); } catch (OCSImportException ex) { LOG.error(ex.getMessage(), ex); errors.add(String.format("Row %d: %s", i + 1, ex.getMessage())); } } } return results; } TextImportTemplate(MessageService messageService, List<String[]> lines, List<String> errors,
            boolean checkForDuplicates); List<T> execute();  }
@Test public void testBasic() { List<String[]> lines = new ArrayList<>(); lines.add(new String[] { "name", "number" }); lines.add(new String[] { "1", "Kevin" }); lines.add(new String[] { "2", "Stuart" }); List<String> errors = new ArrayList<>(); TextImportTemplate<Integer, TestDTO> template = new TextImportTemplate<Integer, TestDTO>(messageService, lines, errors, false) { @Override protected TestDTO process(int rowNum, String[] row) { return importer.processRow(rowNum, row, TestDTO.class); } @Override protected boolean isAppropriateRow(String[] line) { return true; } @Override protected Integer getKeyFromRow(TestDTO t) { return null; } }; List<TestDTO> result = template.execute(); assertEquals(2, result.size()); assertEquals(0, errors.size()); }
@Test public void testException() { List<String[]> lines = new ArrayList<>(); lines.add(new String[] { "name", "number" }); lines.add(new String[] { "a", "Kevin" }); lines.add(new String[] { "2", "Stuart" }); List<String> errors = new ArrayList<>(); TextImportTemplate<Integer, TestDTO> template = new TextImportTemplate<Integer, TestDTO>(messageService, lines, errors, false) { @Override protected TestDTO process(int rowNum, String[] row) { return importer.processRow(rowNum, row, TestDTO.class); } @Override protected boolean isAppropriateRow(String[] line) { return true; } @Override protected Integer getKeyFromRow(TestDTO t) { return null; } }; List<TestDTO> result = template.execute(); assertEquals(1, result.size()); assertEquals(1, errors.size()); }
@Test public void testDuplicateCheck() { List<String[]> lines = new ArrayList<>(); lines.add(new String[] { "name", "number" }); lines.add(new String[] { "1", "Kevin" }); lines.add(new String[] { "1", "Stuart" }); List<String> errors = new ArrayList<>(); TextImportTemplate<Integer, TestDTO> template = new TextImportTemplate<Integer, TestDTO>(messageService, lines, errors, true) { @Override protected TestDTO process(int rowNum, String[] row) { return importer.processRow(rowNum, row, TestDTO.class); } @Override protected boolean isAppropriateRow(String[] line) { return true; } @Override protected Integer getKeyFromRow(TestDTO t) { return t.getFirst(); } }; List<TestDTO> result = template.execute(); assertEquals(1, result.size()); assertEquals(1, errors.size()); }