Search in sources :

Example 21 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportSharingWithMergeModeReplace.

/**
 * 1. Create an object with UserGroupAccessA 2. Update object with only
 * UserGroupAccessB in payload and mergeMode=REPLACE Expected: updated
 * object will have only UserGroupAccessB
 *
 * @throws IOException
 */
@Test
void testImportSharingWithMergeModeReplace() 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.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());
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/dataset_with_accesses_merge_mode.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.CREATE_AND_UPDATE, metadata);
    params.setMergeMode(MergeMode.REPLACE);
    params.setUser(user);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    dataSet = manager.get(DataSet.class, "em8Bg4LCr5k");
    assertNotNull(dataSet.getSharing().getUserGroups());
    assertEquals(1, dataSet.getSharing().getUserGroups().size());
    assertNotNull(dataSet.getSharing().getUserGroups().get("FnJeHbPOtVF"));
}
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 22 with MetadataImportParams

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

the class MetadataImportServiceTest method testUpdateImmutableCreatedByField.

@Test
void testUpdateImmutableCreatedByField() throws IOException {
    User userA = createUser('A', Lists.newArrayList("ALL"));
    userService.addUser(userA);
    injectSecurityContext(userA);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setUser(userA);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    UserGroup userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals(userA.getUid(), userGroup.getCreatedBy().getUid());
    User userB = createUser("B", "ALL");
    userB.setUid("userabcdefB");
    userService.addUser(userB);
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups_update.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setUser(userA);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals("TA user group updated", userGroup.getName());
    assertEquals(userA.getUid(), userGroup.getCreatedBy().getUid());
}
Also used : User(org.hisp.dhis.user.User) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 23 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportEventReportWithProgramIndicators.

@Test
void testImportEventReportWithProgramIndicators() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/eventreport_with_program_indicator.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    EventReport eventReport = manager.get(EventReport.class, "pCSijMNjMcJ");
    assertNotNull(eventReport.getProgramIndicatorDimensions());
    assertEquals(1, eventReport.getProgramIndicatorDimensions().size());
    assertEquals("Cl00ghs775c", eventReport.getProgramIndicatorDimensions().get(0).getUid());
}
Also used : ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) EventReport(org.hisp.dhis.eventreport.EventReport) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 24 with MetadataImportParams

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

the class MetadataImportServiceTest method testUpdateUserGroupWithoutCreatedUserProperty.

@Test
void testUpdateUserGroupWithoutCreatedUserProperty() throws IOException {
    User userA = createUser('A', Lists.newArrayList("ALL"));
    userService.addUser(userA);
    injectSecurityContext(userA);
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups.json").getInputStream(), RenderFormat.JSON);
    MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
    params.setUser(userA);
    ImportReport report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    UserGroup userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals(userA.getUid(), userGroup.getSharing().getOwner());
    User userB = createUser("B", "ALL");
    userService.addUser(userB);
    metadata = renderService.fromMetadata(new ClassPathResource("dxf2/usergroups_update.json").getInputStream(), RenderFormat.JSON);
    params = createParams(ImportStrategy.UPDATE, metadata);
    params.setUser(userB);
    report = importService.importMetadata(params);
    assertEquals(Status.OK, report.getStatus());
    userGroup = manager.get(UserGroup.class, "OPVIvvXzNTw");
    assertEquals("TA user group updated", userGroup.getName());
    assertEquals(userA.getUid(), userGroup.getSharing().getOwner());
}
Also used : User(org.hisp.dhis.user.User) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) List(java.util.List) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) UserGroup(org.hisp.dhis.user.UserGroup) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 25 with MetadataImportParams

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

the class MetadataImportServiceTest method testImportNewObjectWithSkipTranslationIsFalse.

@Test
void testImportNewObjectWithSkipTranslationIsFalse() 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(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());
    // Payload has translations and skipTranslation = false
    assertEquals(2, 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)

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