Search in sources :

Example 66 with ImportReport

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

the class MetadataImportServiceTest method testImportMapCreateAndUpdate.

@Test
void testImportMapCreateAndUpdate() throws IOException {
    java.util.Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/map_new.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());
    List<org.hisp.dhis.mapping.Map> maps = manager.getAll(org.hisp.dhis.mapping.Map.class);
    assertEquals(1, maps.size());
    assertEquals("test1", maps.get(0).getName());
    assertEquals(1, maps.get(0).getMapViews().size());
    org.hisp.dhis.mapping.Map map = manager.get(org.hisp.dhis.mapping.Map.class, "LTNgXfzTFTv");
    assertNotNull(map);
    assertEquals(1, map.getMapViews().size());
    MapView mapView = map.getMapViews().get(0);
    assertNotNull(mapView);
    assertEquals("#ddeeff", mapView.getNoDataColor());
    assertEquals("#aabbcc", mapView.getOrganisationUnitColor());
    assertEquals(ThematicMapType.CHOROPLETH, mapView.getThematicMapType());
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/map_update.json").getInputStream(), RenderFormat.JSON);
    params = new MetadataImportParams();
    params.setImportMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
    params.setObjects(metadata);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    map = manager.get(org.hisp.dhis.mapping.Map.class, "LTNgXfzTFTv");
    assertNotNull(map);
    assertEquals(1, map.getMapViews().size());
    mapView = map.getMapViews().get(0);
    assertNotNull(mapView);
    assertEquals("#ddeeff", mapView.getNoDataColor());
    assertEquals("#aabbcc", mapView.getOrganisationUnitColor());
    assertEquals(ThematicMapType.CHOROPLETH, mapView.getThematicMapType());
}
Also used : ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) MapView(org.hisp.dhis.mapping.MapView) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 67 with ImportReport

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

the class MetadataImportServiceTest method testImportProgramWithProgramStageSections.

@Test
void testImportProgramWithProgramStageSections() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/program_noreg_sections.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    Program program = manager.get(Program.class, "s5uvS0Q7jnX");
    assertNotNull(program);
    assertEquals(1, program.getProgramStages().size());
    ProgramStage programStage = program.getProgramStages().iterator().next();
    assertNotNull(programStage.getProgram());
    assertEquals(3, programStage.getProgramStageDataElements().size());
    programStage.getProgramStageDataElements().forEach(psde -> {
        assertNotNull(psde.getSkipAnalytics());
        assertFalse(psde.getSkipAnalytics());
    });
    Set<ProgramStageSection> programStageSections = programStage.getProgramStageSections();
    assertNotNull(programStageSections);
    assertEquals(2, programStageSections.size());
    ProgramStageSection programStageSection = programStageSections.iterator().next();
    assertNotNull(programStageSection.getProgramStage());
}
Also used : ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) Program(org.hisp.dhis.program.Program) 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 68 with ImportReport

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

the class MetadataImportServiceTest method testImportNewObjectWithSkipTranslationIsTrue.

@Test
void testImportNewObjectWithSkipTranslationIsTrue() throws IOException {
    User user = createUser("A", "ALL");
    manager.save(user);
    injectSecurityContext(user);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_accesses_skipSharing.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setSkipTranslation(true);
    params.setUser(user);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    DataSet dataSet = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertNotNull(dataSet.getSharing().getUserGroups());
    // Payload has translations but skipTranslation = true
    assertEquals(0, dataSet.getTranslations().size());
}
Also used : User(org.hisp.dhis.user.User) 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 69 with ImportReport

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

the class MetadataImportServiceTest method testImportWithSkipSharingIsTrueAndWritePermission.

/**
 * User have READ-WRITE access to Dashboard object User try to update
 * Dashboard with: skipSharing=true, and payload doesn't include sharing
 * data. Expected: import successfully
 */
@Test
void testImportWithSkipSharingIsTrueAndWritePermission() {
    User userA = createUser('A');
    userService.addUser(userA);
    injectSecurityContext(userA);
    Dashboard dashboard = new Dashboard();
    dashboard.setName("DashboardA");
    Sharing sharing = new Sharing();
    sharing.setPublicAccess(AccessStringHelper.DEFAULT);
    sharing.addUserAccess(new UserAccess(userA, AccessStringHelper.READ_WRITE));
    dashboard.setSharing(sharing);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = new HashMap<>();
    metadata.put(Dashboard.class, Collections.singletonList(dashboard));
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setSkipSharing(false);
    // Create Dashboard
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    // Check all sharing data
    IdentifiableObject savedDashboard = manager.get(Dashboard.class, dashboard.getUid());
    assertTrue(aclService.canWrite(userA, savedDashboard));
    assertTrue(aclService.canRead(userA, savedDashboard));
    // Update Dashboard with skipSharing=true and no sharing data in payload
    dashboard.setSharing(null);
    metadata.put(Dashboard.class, Collections.singletonList(dashboard));
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setSkipSharing(true);
    params.setUser(userA);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
}
Also used : User(org.hisp.dhis.user.User) Sharing(org.hisp.dhis.user.sharing.Sharing) UserAccess(org.hisp.dhis.user.sharing.UserAccess) HashMap(java.util.HashMap) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Dashboard(org.hisp.dhis.dashboard.Dashboard) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 70 with ImportReport

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

the class MetadataImportServiceTest method testImportProgramWithProgramStageAndSharing.

/**
 * Payload includes Program and ProgramStage with sharing settings.
 * <p>
 * Expected: after created, both Program and ProgramStage are saved
 * correctly together with sharing settings.
 */
@Test
void testImportProgramWithProgramStageAndSharing() throws IOException {
    User user = createUser("A", "ALL");
    manager.save(user);
    injectSecurityContext(user);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/program_programStage_with_sharing.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setSkipSharing(false);
    params.setUser(user);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    ProgramStage programStage = programStageService.getProgramStage("oORy3Rg9hLE");
    assertEquals(1, programStage.getSharing().getUserGroups().size());
    Program program = manager.get("QIHW6CBdLsP");
    assertEquals(1, program.getSharing().getUserGroups().size());
}
Also used : User(org.hisp.dhis.user.User) Program(org.hisp.dhis.program.Program) 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)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)87 Test (org.junit.jupiter.api.Test)52 List (java.util.List)39 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)37 ClassPathResource (org.springframework.core.io.ClassPathResource)37 User (org.hisp.dhis.user.User)34 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)33 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)32 DhisSpringTest (org.hisp.dhis.DhisSpringTest)12 DataSet (org.hisp.dhis.dataset.DataSet)12 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)11 IOException (java.io.IOException)10 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 CsvImportOptions (org.hisp.dhis.dxf2.csv.CsvImportOptions)9 InputStream (java.io.InputStream)8 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)8 UserService (org.hisp.dhis.user.UserService)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8