Search in sources :

Example 26 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportUpdatePublicAccess.

@Test
void testImportUpdatePublicAccess() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_accesses.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    DataSet dataSet = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertEquals("rw------", dataSet.getPublicAccess());
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_publicAccess_update.json").getInputStream(), RenderFormat.JSON);
    params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(metadata);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    DataSet updatedDataSet = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertEquals("r-------", updatedDataSet.getPublicAccess());
}
Also used : 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 27 with MetadataImportParams

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

the class MetadataImportServiceTest method testCorrectStatusOnImportErrors.

@Test
void testCorrectStatusOnImportErrors() throws IOException {
    createUserAndInjectSecurityContext(true);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setAtomicMode(AtomicMode.NONE);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.WARNING, report.getStatus());
}
Also used : 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 28 with MetadataImportParams

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

the class MetadataImportServiceTest method testMetadataImportWithDeletedProgramStageSection.

@Test
void testMetadataImportWithDeletedProgramStageSection() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/programstage_with_sections.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE_AND_UPDATE, metadata);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    dbmsManager.clearSession();
    ProgramStage programStage = programStageService.getProgramStage("NpsdDv6kKSO");
    assertNotNull(programStage.getProgramStageSections());
    assertNotNull(manager.get(ProgramStageSection.class, "JwcV2ZifEQf"));
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/programstage_with_removed_section.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setMetadataSyncImport(true);
    report = importService.importMetadata(params);
    report.forEachErrorReport(errorReport -> log.error("Error report:" + errorReport));
    assertEquals(Status.OK, report.getStatus());
    programStage = manager.get(ProgramStage.class, "NpsdDv6kKSO");
    assertEquals(1, programStage.getProgramStageSections().size());
    assertNull(manager.get(ProgramStageSection.class, "JwcV2ZifEQf"));
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/programstage_with_all_section_removed.json").getInputStream(), RenderFormat.JSON);
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.UPDATE);
    params.setObjects(metadata);
    params.setMetadataSyncImport(true);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    programStage = manager.get(ProgramStage.class, "NpsdDv6kKSO");
    assertEquals(true, programStage.getProgramStageSections().isEmpty());
}
Also used : ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ProgramStage(org.hisp.dhis.program.ProgramStage) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 29 with MetadataImportParams

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

the class MetadataImportServiceTest method testMetadataImportWithDeletedDataElements.

@Test
void testMetadataImportWithDeletedDataElements() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_sections_and_data_elements.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE_AND_UPDATE, metadata);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    DataSet dataset = dataSetService.getDataSet("em8Bg4LCr5k");
    assertNotNull(dataset.getSections());
    assertNotNull(dataset.getDataElements());
    assertTrue(dataset.getDataElements().stream().map(de -> de.getUid()).collect(Collectors.toList()).contains("R45hiT7RLui"));
    assertNotNull(manager.get(Section.class, "JwcV2ZifEQf"));
    assertTrue(dataset.getSections().stream().filter(s -> s.getUid().equals("JwcV2ZifEQf")).findFirst().get().getDataElements().stream().map(de -> de.getUid()).collect(Collectors.toList()).contains("R45hiT7RLui"));
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_data_element_removed.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.CREATE_AND_UPDATE, metadata);
    params.setMetadataSyncImport(false);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    dataset = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertFalse(dataset.getDataElements().stream().map(de -> de.getUid()).collect(Collectors.toList()).contains("R45hiT7RLui"));
    assertNotNull(manager.get(Section.class, "JwcV2ZifEQf"));
    assertFalse(dataset.getSections().stream().filter(s -> s.getUid().equals("JwcV2ZifEQf")).findFirst().get().getDataElements().stream().map(de -> de.getUid()).collect(Collectors.toList()).contains("R45hiT7RLui"));
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) RenderService(org.hisp.dhis.render.RenderService) Autowired(org.springframework.beans.factory.annotation.Autowired) Visualization(org.hisp.dhis.visualization.Visualization) UserAccess(org.hisp.dhis.user.sharing.UserAccess) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Status(org.hisp.dhis.feedback.Status) UserService(org.hisp.dhis.user.UserService) AccessStringHelper(org.hisp.dhis.security.acl.AccessStringHelper) UserGroup(org.hisp.dhis.user.UserGroup) Set(java.util.Set) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Section(org.hisp.dhis.dataset.Section) Test(org.junit.jupiter.api.Test) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) AclService(org.hisp.dhis.security.acl.AclService) ThematicMapType(org.hisp.dhis.mapping.ThematicMapType) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) DataSet(org.hisp.dhis.dataset.DataSet) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) ClassPathResource(org.springframework.core.io.ClassPathResource) HashMap(java.util.HashMap) EventReport(org.hisp.dhis.eventreport.EventReport) MapView(org.hisp.dhis.mapping.MapView) Program(org.hisp.dhis.program.Program) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ObjectBundleMode(org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleMode) User(org.hisp.dhis.user.User) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) MapUtils(org.apache.commons.collections4.MapUtils) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) RenderFormat(org.hisp.dhis.render.RenderFormat) IOException(java.io.IOException) Sharing(org.hisp.dhis.user.sharing.Sharing) ProgramStageService(org.hisp.dhis.program.ProgramStageService) MergeMode(org.hisp.dhis.common.MergeMode) ProgramStage(org.hisp.dhis.program.ProgramStage) Dashboard(org.hisp.dhis.dashboard.Dashboard) ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) DataSetService(org.hisp.dhis.dataset.DataSetService) Collections(java.util.Collections) DataSet(org.hisp.dhis.dataset.DataSet) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) Section(org.hisp.dhis.dataset.Section) ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 30 with MetadataImportParams

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

the class CsvMetadataImportIntegrationTest 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