Search in sources :

Example 1 with CsvImportExportService

use of org.motechproject.mds.service.CsvImportExportService 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 2 with CsvImportExportService

use of org.motechproject.mds.service.CsvImportExportService in project motech by motech.

the class MdsBundleIT method verifyCsvImportIsOneTransaction.

private void verifyCsvImportIsOneTransaction() throws Exception {
    getLogger().info("Verifying that CSV Import is done in one transaction");
    CsvImportExportService csvImportExportService = ServiceRetriever.getService(bundleContext, CsvImportExportService.class);
    boolean exceptionThrown = false;
    try (InputStream in = new ClassPathResource("csv/import.csv").getInputStream()) {
        String csv = IOUtils.toString(in);
        csv += "invalid row";
        csvImportExportService.importCsv(FOO_CLASS, new StringReader(csv), "import.csv", false);
    } catch (RuntimeException e) {
        exceptionThrown = true;
    }
    assertTrue("No exception thrown during invalid CSV import", exceptionThrown);
    // verify that there are no new instances
    assertEquals(7, service.count());
}
Also used : InputStream(java.io.InputStream) CsvImportExportService(org.motechproject.mds.service.CsvImportExportService) StringReader(java.io.StringReader) ClassPathResource(org.springframework.core.io.ClassPathResource)

Aggregations

InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 CsvImportExportService (org.motechproject.mds.service.CsvImportExportService)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)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 Time (org.motechproject.commons.date.model.Time)1 CsvImportResults (org.motechproject.mds.dto.CsvImportResults)1 QueryParams (org.motechproject.mds.query.QueryParams)1 Order (org.motechproject.mds.util.Order)1