Search in sources :

Example 16 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class AttributeValueImportTest method testSaveAttributeValueAfterUpdateAttribute.

@Test
void testSaveAttributeValueAfterUpdateAttribute() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> attributes = renderService.fromMetadata(new ClassPathResource("attribute/attribute.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(attributes);
    final ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> dataSets = renderService.fromMetadata(new ClassPathResource("attribute/dataSet.json").getInputStream(), RenderFormat.JSON);
    params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(dataSets);
    final ImportReport importReport = importService.importMetadata(params);
    assertEquals(Status.OK, importReport.getStatus());
    manager.flush();
    DataSet dataSet = manager.get(DataSet.class, "sPnR8BCInMV");
    assertEquals("true", dataSet.getAttributeValue("PtyV6lLcmol").getValue());
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> attributesUpdate = renderService.fromMetadata(new ClassPathResource("attribute/attribute_update.json").getInputStream(), RenderFormat.JSON);
    params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(attributesUpdate);
    final ImportReport importReport1 = importService.importMetadata(params);
    assertEquals(Status.OK, importReport1.getStatus());
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> dataSetUpdate = renderService.fromMetadata(new ClassPathResource("attribute/dataSet_update.json").getInputStream(), RenderFormat.JSON);
    params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(dataSetUpdate);
    final ImportReport importReport2 = importService.importMetadata(params);
    assertEquals(Status.OK, importReport2.getStatus());
    DataSet updatedDataSet = manager.get(DataSet.class, "sPnR8BCInMV");
    assertEquals("false", updatedDataSet.getAttributeValue("PtyV6lLcmol").getValue());
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) DataSet(org.hisp.dhis.dataset.DataSet) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 17 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class MetadataSyncImportHandlerTest method testShouldThrowExceptionWhenParsingClassListMap.

@Test
void testShouldThrowExceptionWhenParsingClassListMap() throws IOException {
    syncParams.setImportParams(new MetadataImportParams());
    syncParams.setVersion(metadataVersion);
    importReport.setStatus(Status.OK);
    when(renderService.fromMetadata(any(InputStream.class), eq(RenderFormat.JSON))).thenThrow(new IOException());
    assertThrows(MetadataSyncServiceException.class, () -> metadataSyncImportHandler.importMetadata(syncParams, expectedMetadataSnapshot), "Exception occurred while trying to do JSON conversion while parsing class list map");
    verify(metadataImportService, never()).importMetadata(syncParams.getImportParams());
    verify(metadataVersionDelegate, never()).addNewMetadataVersion(metadataVersion);
    verify(metadataVersionDelegate, never()).addNewMetadataVersion(metadataVersion);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Example 18 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class MetadataSyncImportHandlerTest method testShouldThrowExceptionWhenClassListMapIsNull.

@Test
void testShouldThrowExceptionWhenClassListMapIsNull() throws IOException {
    syncParams.setImportParams(new MetadataImportParams());
    syncParams.setVersion(metadataVersion);
    importReport.setStatus(Status.OK);
    when(renderService.fromMetadata(any(InputStream.class), eq(RenderFormat.JSON))).thenReturn(null);
    assertThrows(MetadataSyncServiceException.class, () -> metadataSyncImportHandler.importMetadata(syncParams, expectedMetadataSnapshot), "ClassListMap can't be null");
    verify(metadataImportService, never()).importMetadata(syncParams.getImportParams());
    verify(metadataVersionDelegate, never()).addNewMetadataVersion(metadataVersion);
    verify(metadataVersionDelegate, never()).addNewMetadataVersion(metadataVersion);
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) InputStream(java.io.InputStream) Test(org.junit.jupiter.api.Test)

Example 19 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class MetadataSyncImportHandlerTest method testShouldImportMetadata.

@Test
void testShouldImportMetadata() {
    syncParams.setImportParams(new MetadataImportParams());
    syncParams.setVersion(metadataVersion);
    MetadataSyncSummary metadataSyncSummary = new MetadataSyncSummary();
    importReport.setStatus(Status.OK);
    when(metadataImportService.importMetadata(syncParams.getImportParams())).thenReturn(importReport);
    metadataSyncSummary.setImportReport(importReport);
    metadataSyncSummary.setMetadataVersion(metadataVersion);
    doNothing().when(metadataVersionDelegate).addNewMetadataVersion(metadataVersion);
    MetadataSyncSummary actualMetadataSyncSummary = metadataSyncImportHandler.importMetadata(syncParams, expectedMetadataSnapshot);
    verify(metadataVersionDelegate).addNewMetadataVersion(metadataVersion);
    assertEquals(metadataSyncSummary.getImportReport(), actualMetadataSyncSummary.getImportReport());
    assertEquals(metadataSyncSummary.getImportSummary(), actualMetadataSyncSummary.getImportSummary());
    assertEquals(metadataSyncSummary.getMetadataVersion(), actualMetadataSyncSummary.getMetadataVersion());
    assertEquals(metadataSyncSummary.getMetadataVersion().getType(), actualMetadataSyncSummary.getMetadataVersion().getType());
    assertEquals(metadataSyncSummary.getImportReport().getStatus(), actualMetadataSyncSummary.getImportReport().getStatus());
}
Also used : MetadataImportParams(org.hisp.dhis.dxf2.metadata.MetadataImportParams) Test(org.junit.jupiter.api.Test)

Example 20 with MetadataImportParams

use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.

the class CsvMetadataImportTest method runImport.

private ImportReport runImport(String csvFile, CsvImportClass importClass, Consumer<Metadata> preCondition, Consumer<MetadataImportParams> modifier) throws IOException {
    InputStream input = new ClassPathResource(csvFile).getInputStream();
    Metadata metadata = csvImportService.fromCsv(input, new CsvImportOptions().setImportClass(importClass).setFirstRowIsHeader(true));
    if (preCondition != null) {
        preCondition.accept(metadata);
    }
    MetadataImportParams params = new MetadataImportParams();
    params.addMetadata(schemaService.getMetadataSchemas(), metadata);
    if (modifier != null) {
        modifier.accept(params);
    }
    return importService.importMetadata(params);
}
Also used : CsvImportOptions(org.hisp.dhis.dxf2.csv.CsvImportOptions) InputStream(java.io.InputStream) ClassPathResource(org.springframework.core.io.ClassPathResource)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)58 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)49 List (java.util.List)38 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)38 Test (org.junit.jupiter.api.Test)38 ClassPathResource (org.springframework.core.io.ClassPathResource)31 User (org.hisp.dhis.user.User)30 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)29 DataSet (org.hisp.dhis.dataset.DataSet)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)9 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)8 WebMessage (org.hisp.dhis.dxf2.webmessage.WebMessage)8 HashMap (java.util.HashMap)6 UserGroup (org.hisp.dhis.user.UserGroup)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5