@Test public void shouldPostProcessRecordsAfterSuccessfulUpload() throws UnsupportedEncodingException { String csvInput = "mandatory string field , mandatoryIntField, entity 1 code, entity 2 code\n" + " Random1 , 23, code1-1, code1-2\n" + " Random2 , 25, code2-1, code2-2\n"; InputStream inputStream = new ByteArrayInputStream(csvInput.getBytes("UTF-8")); DummyRecordHandler spyRecordHandler = spy(recordHandler); csvParser.process(inputStream, dummyImportableClass, spyRecordHandler, auditFields); DummyImportable dummyImportable = (DummyImportable) spyRecordHandler.getImportedObjects().get(0); assertThat(dummyImportable.getMultipleNestedFields().getEntityCode1(), is("code1-1")); assertThat(dummyImportable.getMultipleNestedFields().getEntityCode2(), is("code1-2")); verify(spyRecordHandler).postProcess(auditFields); }
@Transactional public int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields) throws UploadException { CsvBeanReader csvBeanReader = null; String[] headers = null; try { csvBeanReader = new CsvBeanReader(modelClass, inputStream); headers = csvBeanReader.getHeaders(); csvBeanReader.validateHeaders(); Importable importedModel; while ((importedModel = csvBeanReader.readWithCellProcessors()) != null) { recordHandler.execute(importedModel, csvBeanReader.getRowNumber(), auditFields); } recordHandler.postProcess(auditFields); } catch (SuperCsvConstraintViolationException constraintException) { if (constraintException.getMessage().contains("^\\d{1,2}/\\d{1,2}/\\d{4}$")) { createHeaderException("incorrect.date.format", headers, constraintException); } createHeaderException("missing.mandatory", headers, constraintException); } catch (SuperCsvCellProcessorException processorException) { createHeaderException("incorrect.data.type", headers, processorException); } catch (SuperCsvException superCsvException) { if (csvBeanReader.length() > headers.length) { throw new UploadException("incorrect.file.format"); } createDataException("column.do.not.match", headers, superCsvException); } catch (IOException e) { throw new UploadException(e.getStackTrace().toString()); } return csvBeanReader.getRowNumber() - 1; }
CSVParser { @Transactional public int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields) throws UploadException { CsvBeanReader csvBeanReader = null; String[] headers = null; try { csvBeanReader = new CsvBeanReader(modelClass, inputStream); headers = csvBeanReader.getHeaders(); csvBeanReader.validateHeaders(); Importable importedModel; while ((importedModel = csvBeanReader.readWithCellProcessors()) != null) { recordHandler.execute(importedModel, csvBeanReader.getRowNumber(), auditFields); } recordHandler.postProcess(auditFields); } catch (SuperCsvConstraintViolationException constraintException) { if (constraintException.getMessage().contains("^\\d{1,2}/\\d{1,2}/\\d{4}$")) { createHeaderException("incorrect.date.format", headers, constraintException); } createHeaderException("missing.mandatory", headers, constraintException); } catch (SuperCsvCellProcessorException processorException) { createHeaderException("incorrect.data.type", headers, processorException); } catch (SuperCsvException superCsvException) { if (csvBeanReader.length() > headers.length) { throw new UploadException("incorrect.file.format"); } createDataException("column.do.not.match", headers, superCsvException); } catch (IOException e) { throw new UploadException(e.getStackTrace().toString()); } return csvBeanReader.getRowNumber() - 1; } }
CSVParser { @Transactional public int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields) throws UploadException { CsvBeanReader csvBeanReader = null; String[] headers = null; try { csvBeanReader = new CsvBeanReader(modelClass, inputStream); headers = csvBeanReader.getHeaders(); csvBeanReader.validateHeaders(); Importable importedModel; while ((importedModel = csvBeanReader.readWithCellProcessors()) != null) { recordHandler.execute(importedModel, csvBeanReader.getRowNumber(), auditFields); } recordHandler.postProcess(auditFields); } catch (SuperCsvConstraintViolationException constraintException) { if (constraintException.getMessage().contains("^\\d{1,2}/\\d{1,2}/\\d{4}$")) { createHeaderException("incorrect.date.format", headers, constraintException); } createHeaderException("missing.mandatory", headers, constraintException); } catch (SuperCsvCellProcessorException processorException) { createHeaderException("incorrect.data.type", headers, processorException); } catch (SuperCsvException superCsvException) { if (csvBeanReader.length() > headers.length) { throw new UploadException("incorrect.file.format"); } createDataException("column.do.not.match", headers, superCsvException); } catch (IOException e) { throw new UploadException(e.getStackTrace().toString()); } return csvBeanReader.getRowNumber() - 1; }  }
CSVParser { @Transactional public int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields) throws UploadException { CsvBeanReader csvBeanReader = null; String[] headers = null; try { csvBeanReader = new CsvBeanReader(modelClass, inputStream); headers = csvBeanReader.getHeaders(); csvBeanReader.validateHeaders(); Importable importedModel; while ((importedModel = csvBeanReader.readWithCellProcessors()) != null) { recordHandler.execute(importedModel, csvBeanReader.getRowNumber(), auditFields); } recordHandler.postProcess(auditFields); } catch (SuperCsvConstraintViolationException constraintException) { if (constraintException.getMessage().contains("^\\d{1,2}/\\d{1,2}/\\d{4}$")) { createHeaderException("incorrect.date.format", headers, constraintException); } createHeaderException("missing.mandatory", headers, constraintException); } catch (SuperCsvCellProcessorException processorException) { createHeaderException("incorrect.data.type", headers, processorException); } catch (SuperCsvException superCsvException) { if (csvBeanReader.length() > headers.length) { throw new UploadException("incorrect.file.format"); } createDataException("column.do.not.match", headers, superCsvException); } catch (IOException e) { throw new UploadException(e.getStackTrace().toString()); } return csvBeanReader.getRowNumber() - 1; }  @Transactional int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields); }
CSVParser { @Transactional public int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields) throws UploadException { CsvBeanReader csvBeanReader = null; String[] headers = null; try { csvBeanReader = new CsvBeanReader(modelClass, inputStream); headers = csvBeanReader.getHeaders(); csvBeanReader.validateHeaders(); Importable importedModel; while ((importedModel = csvBeanReader.readWithCellProcessors()) != null) { recordHandler.execute(importedModel, csvBeanReader.getRowNumber(), auditFields); } recordHandler.postProcess(auditFields); } catch (SuperCsvConstraintViolationException constraintException) { if (constraintException.getMessage().contains("^\\d{1,2}/\\d{1,2}/\\d{4}$")) { createHeaderException("incorrect.date.format", headers, constraintException); } createHeaderException("missing.mandatory", headers, constraintException); } catch (SuperCsvCellProcessorException processorException) { createHeaderException("incorrect.data.type", headers, processorException); } catch (SuperCsvException superCsvException) { if (csvBeanReader.length() > headers.length) { throw new UploadException("incorrect.file.format"); } createDataException("column.do.not.match", headers, superCsvException); } catch (IOException e) { throw new UploadException(e.getStackTrace().toString()); } return csvBeanReader.getRowNumber() - 1; }  @Transactional int process(InputStream inputStream, ModelClass modelClass, RecordHandler recordHandler, AuditFields auditFields);  }