Search in sources :

Example 56 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportWithSkipSharingIsTrueAndNoPermission.

/**
 * User only have READ access to Dashboard object User try to update
 * Dashboard with: skipSharing=true, and payload doesn't include sharing
 * data. Expected: import error
 */
@Test
void testImportWithSkipSharingIsTrueAndNoPermission() {
    clearSecurityContext();
    User userA = createUser("A");
    userService.addUser(userA);
    Dashboard dashboard = new Dashboard();
    dashboard.setName("DashboardA");
    Sharing sharing = new Sharing();
    sharing.addUserAccess(new UserAccess(userA, AccessStringHelper.READ));
    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 sharing data
    IdentifiableObject savedDashboard = manager.get(Dashboard.class, dashboard.getUid());
    boolean condition = aclService.canWrite(userA, savedDashboard);
    assertFalse(condition);
    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.ERROR, 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 57 with MetadataImportParams

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

the class MetadataImportServiceTest method testCorrectStatusOnImportErrorsATOMIC.

@Test
void testCorrectStatusOnImportErrorsATOMIC() 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);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.ERROR, 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 58 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportWithSkipSharingIsTrue.

@Test
void testImportWithSkipSharingIsTrue() 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.setSkipSharing(false);
    params.setUser(user);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    DataSet dataSet = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertNotNull(dataSet.getSharing().getUserGroups());
    assertEquals(1, dataSet.getSharing().getUserGroups().size());
    assertEquals("fvz8d3u6jFd", dataSet.getSharing().getUserGroups().values().iterator().next().getId());
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_accesses_update_skipSharing.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setSkipSharing(true);
    params.setUser(user);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    DataSet dataSetUpdated = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertNotNull(dataSetUpdated.getSharing().getUserGroups());
    assertEquals(1, dataSetUpdated.getSharing().getUserGroups().size());
    assertNotNull(dataSetUpdated.getSharing().getUserGroups().get("fvz8d3u6jFd"));
}
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 59 with MetadataImportParams

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

the class MetadataImportServiceTest method testMetadataSyncWithDeletedDataSetSection.

@Test
void testMetadataSyncWithDeletedDataSetSection() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_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();
    DataSet dataset = dataSetService.getDataSet("em8Bg4LCr5k");
    assertNotNull(dataset.getSections());
    assertNotNull(manager.get(Section.class, "JwcV2ZifEQf"));
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_removed_section.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setMetadataSyncImport(true);
    dbmsManager.clearSession();
    report = importService.importMetadata(params);
    report.forEachErrorReport(errorReport -> log.error("Error report:" + errorReport));
    assertEquals(Status.OK, report.getStatus());
    dataset = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertEquals(1, dataset.getSections().size());
    assertNull(manager.get(Section.class, "JwcV2ZifEQf"));
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_all_section_removed.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.CREATE_AND_UPDATE, metadata);
    params.setMetadataSyncImport(true);
    dbmsManager.clearSession();
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    dataset = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertTrue(dataset.getSections().isEmpty());
}
Also used : 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 60 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportMultiPropertyUniqueness.

@Test
void testImportMultiPropertyUniqueness() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/metadata_multi_property_uniqueness.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    ImportReport importReport = importService.importMetadata(params);
    assertTrue(importReport.hasErrorReport(errorReport -> errorReport.getErrorCode() == ErrorCode.E5005));
}
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) 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)

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