use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImporterExporterTest method testImport.
private void testImport(IdMode idMode, boolean clearData) {
StringReader reader = new StringReader(getTestEntityRecordsAsCsv(idMode));
// if id provided, prepare entities that will be updated
if (idMode == IdMode.INCLUDE_ID) {
when(motechDataService.update(any(Record2.class))).thenAnswer(new Answer<Record2>() {
@Override
public Record2 answer(InvocationOnMock invocation) throws Throwable {
return (Record2) invocation.getArguments()[0];
}
});
for (long i = 0; i < INSTANCE_COUNT; i++) {
when(motechDataService.findById(i)).thenReturn(new Record2());
}
} else {
when(motechDataService.create(any(Record2.class))).thenAnswer(new CreateAnswer());
}
CsvImportResults results = csvImporterExporter.importCsv(ENTITY_ID, reader, CONTINUE_ON_ERROR, clearData);
ArgumentCaptor<Record2> captor = ArgumentCaptor.forClass(Record2.class);
if (idMode == IdMode.INCLUDE_ID) {
verify(motechDataService, times(INSTANCE_COUNT)).update(captor.capture());
} else {
verify(motechDataService, times(INSTANCE_COUNT)).create(captor.capture());
}
if (clearData) {
verify(motechDataService).deleteAll();
}
assertNotNull(results);
assertEquals(INSTANCE_COUNT, results.totalNumberOfImportedInstances());
assertEquals(testInstances(idMode), captor.getAllValues());
assertEquals(ENTITY_CLASSNAME, results.getEntityClassName());
assertEquals(ENTITY_NAME, results.getEntityName());
assertEquals(ENTITY_MODULE, results.getEntityModule());
assertEquals(ENTITY_NAMESPACE, results.getEntityNamespace());
assertEquals(0, results.getRowErrors().size());
if (idMode == IdMode.INCLUDE_ID) {
assertEquals(INSTANCE_COUNT, results.updatedInstanceCount());
assertEquals(0, results.newInstanceCount());
assertTrue(results.getNewInstanceIDs().isEmpty());
assertEquals(listFromRangeInclusive(0, 19), results.getUpdatedInstanceIDs());
} else {
assertEquals(INSTANCE_COUNT, results.newInstanceCount());
assertEquals(0, results.updatedInstanceCount());
assertTrue(results.getUpdatedInstanceIDs().isEmpty());
assertEquals(listFromRangeInclusive(0, 19), results.getNewInstanceIDs());
}
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImporterExporterTest method testImportWithInvalidRows.
@Test
public void testImportWithInvalidRows() {
CsvImportResults results;
// This will provide csv import with 3 rows with invalid enum fields
StringReader reader = new StringReader(getTestEntityRecordsAsCsv(IdMode.INVALID));
// First import call with continueOnError flag on
results = csvImporterExporter.importCsv(ENTITY_ID, reader, true, false);
// Check how many times create was called, how many objects were created and how many errors were caught
// Expecting 17 creates and 3 errors since we got 3 invalid rows in a set of 20 passed as import input
ArgumentCaptor<Record2> captor = ArgumentCaptor.forClass(Record2.class);
verify(motechDataService, times(17)).create(captor.capture());
assertNotNull(results);
assertEquals(17, results.totalNumberOfImportedInstances());
assertEquals(3, results.getRowErrors().size());
// Now call import with continueOnError flag off
StringReader reader2 = new StringReader(getTestEntityRecordsAsCsv(IdMode.INVALID));
boolean thrown = false;
try {
csvImporterExporter.importCsv(ENTITY_ID, reader2, false, false);
} catch (RuntimeException e) {
thrown = true;
}
// First invalid row encountered should stop whole import process and throw an exception
assertTrue(thrown);
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImportExportServiceTest method shouldImportInstancesById.
@Test
public void shouldImportInstancesById() {
CsvImportResults importResults = new CsvImportResults(entityDto, NEW_IDS, UPDATED_IDS, null);
when(csvImporterExporter.importCsv(eq(ENTITY_ID), eq(reader), any(DefaultCsvImportCustomizer.class), eq(false), eq(false))).thenReturn(importResults);
csvImportExportService.importCsv(ENTITY_ID, reader, FILE_NAME, false, false);
verify(csvImporterExporter).importCsv(eq(ENTITY_ID), eq(reader), any(DefaultCsvImportCustomizer.class), eq(false), eq(false));
verifyImportSuccessEvent();
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class MdsBundleIT method verifyCsvImport.
private void verifyCsvImport() throws Exception {
getLogger().info("Verifying CSV Import");
CsvImportExportService csvImportExportService = ServiceRetriever.getService(bundleContext, CsvImportExportService.class);
try (InputStream in = new ClassPathResource("csv/import.csv").getInputStream()) {
Reader reader = new InputStreamReader(in);
CsvImportResults results = csvImportExportService.importCsv(FOO_CLASS, reader, "import.csv", false);
assertNotNull(results);
assertEquals(2, results.totalNumberOfImportedInstances());
assertEquals(2, results.newInstanceCount());
assertEquals(0, results.updatedInstanceCount());
}
assertEquals(7, service.count());
// get the imported instances through a lookup
QueryParams queryParams = new QueryParams(new Order("someTime", Order.Direction.DESC));
List list = (List) MethodUtils.invokeExactMethod(service, "matchesOperator", new Object[] { "fromCsv", queryParams });
assertNotNull(list);
assertEquals(2, list.size());
assertInstance(list.get(0), false, "fromCsv2", "Capital CSV", Collections.emptyList(), null, new LocalDate(2012, 10, 14), null, new Period(2, 0, 0, 0, 0, 0, 0, 0), null, new DateTime(2014, 12, 2, 16, 13, 40, 0, DateTimeZone.UTC).toDate(), null, new Time(20, 20), null, null, null, null);
assertInstance(list.get(1), true, "fromCsv1", "Capital CSV", new ArrayList(asList("one", "two")), new DateTime(2014, 12, 2, 13, 10, 40, 0, DateTimeZone.UTC).withZone(DateTimeZone.getDefault()), new LocalDate(2012, 10, 15), null, new Period(1, 0, 0, 0, 0, 0, 0, 0), null, new DateTime(2014, 12, 2, 13, 13, 40, 0, DateTimeZone.UTC).toDate(), null, new Time(10, 30), null, null, null, null);
}
use of org.motechproject.mds.dto.CsvImportResults in project motech by motech.
the class CsvImportExportServiceImpl method importCsv.
@Override
public CsvImportResults importCsv(long entityId, Reader reader, String fileName, CsvImportCustomizer importCustomizer, boolean continueOnError, boolean clearData) {
LOGGER.debug("Importing instances of entity with ID: {}", entityId);
CsvImportResults importResults;
try {
importResults = csvImporterExporter.importCsv(entityId, reader, importCustomizer, continueOnError, clearData);
} catch (RuntimeException e) {
EntityDto entity = entityService.getEntity(entityId);
sendImportFailureEvent(entity, fileName, e);
throw e;
}
sendImportSuccessEvent(importResults, fileName);
return importResults;
}
Aggregations