Search in sources :

Example 1 with CsvImportResults

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());
    }
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) StringReader(java.io.StringReader) Record2(org.motechproject.mds.testutil.records.Record2) CsvImportResults(org.motechproject.mds.dto.CsvImportResults)

Example 2 with CsvImportResults

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);
}
Also used : StringReader(java.io.StringReader) Record2(org.motechproject.mds.testutil.records.Record2) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) Test(org.junit.Test)

Example 3 with CsvImportResults

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();
}
Also used : DefaultCsvImportCustomizer(org.motechproject.mds.service.DefaultCsvImportCustomizer) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) Test(org.junit.Test)

Example 4 with CsvImportResults

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);
}
Also used : Order(org.motechproject.mds.util.Order) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Period(org.joda.time.Period) Time(org.motechproject.commons.date.model.Time) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) LocalDate(org.joda.time.LocalDate) ClassPathResource(org.springframework.core.io.ClassPathResource) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) CsvImportExportService(org.motechproject.mds.service.CsvImportExportService) QueryParams(org.motechproject.mds.query.QueryParams) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 5 with CsvImportResults

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;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) CsvImportResults(org.motechproject.mds.dto.CsvImportResults)

Aggregations

CsvImportResults (org.motechproject.mds.dto.CsvImportResults)10 Test (org.junit.Test)5 StringReader (java.io.StringReader)4 Record2 (org.motechproject.mds.testutil.records.Record2)3 ArrayList (java.util.ArrayList)2 EntityDto (org.motechproject.mds.dto.EntityDto)2 DefaultCsvImportCustomizer (org.motechproject.mds.service.DefaultCsvImportCustomizer)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 LocalDateTime (java.time.LocalDateTime)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 DateTime (org.joda.time.DateTime)1 LocalDate (org.joda.time.LocalDate)1 Period (org.joda.time.Period)1