Search in sources :

Example 1 with Record2

use of org.motechproject.mds.testutil.records.Record2 in project motech by motech.

the class CsvImporterExporterTest method shouldExportInstancesFromTableAsCsv.

@Test
public void shouldExportInstancesFromTableAsCsv() {
    when(mdsLookupService.<Record2>findMany((any(String.class)), eq("lookup"), any(Map.class), any(QueryParams.class))).thenReturn(testInstances(IdMode.INCLUDE_ID));
    StringWriter writer = new StringWriter();
    List<String> headers = Arrays.asList("ID", "Creator", "Owner", "Modified By", "Creation date", "Modification date", "Value Disp", "Date disp", "dateIgnoredByRest disp", "enumField Disp", "enumListField Disp", "singleRelationship Disp", "multiRelationship Disp");
    long result = csvImporterExporter.exportCsv(ENTITY_ID, writer, "lookup", null, headers, null);
    assertEquals(INSTANCE_COUNT, result);
    assertEquals(getTestEntityRecordsAsCsv(IdMode.INCLUDE_ID), writer.toString());
}
Also used : StringWriter(java.io.StringWriter) QueryParams(org.motechproject.mds.query.QueryParams) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) Record2(org.motechproject.mds.testutil.records.Record2) Test(org.junit.Test)

Example 2 with Record2

use of org.motechproject.mds.testutil.records.Record2 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 3 with Record2

use of org.motechproject.mds.testutil.records.Record2 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 4 with Record2

use of org.motechproject.mds.testutil.records.Record2 in project motech by motech.

the class PdfCsvExporterTest method setUpTestData.

private void setUpTestData(boolean exportSecondRecord) {
    List<Record2> instances = new ArrayList<>();
    Record2 instance1 = new Record2();
    instance1.setCreationDate(NOW);
    instance1.setModificationDate(NOW.plusMinutes(10));
    instance1.setCreator("motech");
    instance1.setOwner("motech");
    instance1.setDate(NOW.toDate());
    instance1.setDateIgnoredByRest(NOW.plusHours(1).toDate());
    instance1.setValue(null);
    instances.add(instance1);
    if (exportSecondRecord) {
        Record2 instance2 = new Record2();
        instance2.setCreationDate(NOW);
        instance2.setModificationDate(NOW.plusMinutes(20));
        instance2.setCreator("motech2");
        instance2.setOwner("motech2");
        instance2.setDate(NOW.toDate());
        instance2.setDateIgnoredByRest(NOW.plusHours(2).toDate());
        instance2.setValue("SomeVal2");
        instances.add(instance2);
    }
    when(dataService.retrieveAll(null)).thenReturn(instances);
}
Also used : ArrayList(java.util.ArrayList) Record2(org.motechproject.mds.testutil.records.Record2)

Example 5 with Record2

use of org.motechproject.mds.testutil.records.Record2 in project motech by motech.

the class CsvImporterExporterTest method shouldUseImportCustomizer.

@Test
public void shouldUseImportCustomizer() {
    StringReader reader = new StringReader(getTestEntityRecordsAsCsv(IdMode.EMPTY_ID_COLUMN));
    when(csvImportCustomizer.doCreate(any(Record2.class), eq(motechDataService))).thenAnswer(new CreateAnswer());
    CsvImportResults results = csvImporterExporter.importCsv(ENTITY_ID, reader, csvImportCustomizer, CONTINUE_ON_ERROR, false);
    ArgumentCaptor<Record2> captor = ArgumentCaptor.forClass(Record2.class);
    verify(csvImportCustomizer, times(INSTANCE_COUNT)).findExistingInstance(anyMap(), eq(motechDataService));
    verify(csvImportCustomizer, times(INSTANCE_COUNT)).doCreate(captor.capture(), eq(motechDataService));
    verify(csvImportCustomizer, times(FIELD_COUNT)).findField(anyString(), anyList());
    verify(csvImportCustomizer, never()).doUpdate(captor.capture(), eq(motechDataService));
    assertNotNull(results);
    assertEquals(INSTANCE_COUNT, results.totalNumberOfImportedInstances());
}
Also used : StringReader(java.io.StringReader) Record2(org.motechproject.mds.testutil.records.Record2) CsvImportResults(org.motechproject.mds.dto.CsvImportResults) Test(org.junit.Test)

Aggregations

Record2 (org.motechproject.mds.testutil.records.Record2)6 StringReader (java.io.StringReader)3 Test (org.junit.Test)3 CsvImportResults (org.motechproject.mds.dto.CsvImportResults)3 ArrayList (java.util.ArrayList)2 StringWriter (java.io.StringWriter)1 Map (java.util.Map)1 Matchers.anyMap (org.mockito.Matchers.anyMap)1 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 QueryParams (org.motechproject.mds.query.QueryParams)1 RelatedClass (org.motechproject.mds.testutil.records.RelatedClass)1