use of org.hisp.dhis.dxf2.metadata.MetadataImportParams in project dhis2-core by dhis2.
the class MetadataImportServiceTest method testImportEmbeddedObjectWithSkipSharingIsFalse.
@Test
void testImportEmbeddedObjectWithSkipSharingIsFalse() throws IOException {
User user = createUser('A');
manager.save(user);
User userA = manager.get(User.class, user.getUid());
assertNotNull(userA);
UserGroup userGroup = createUserGroup('A', Sets.newHashSet(userA));
manager.save(userGroup);
userGroup = manager.get(UserGroup.class, "ugabcdefghA");
assertNotNull(userGroup);
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/metadata_visualization_with_accesses.json").getInputStream(), RenderFormat.JSON);
MetadataImportParams params = createParams(ImportStrategy.CREATE, metadata);
params.setSkipSharing(false);
ImportReport report = importService.importMetadata(params);
assertEquals(Status.OK, report.getStatus());
dbmsManager.clearSession();
Visualization visualization = manager.get(Visualization.class, "gyYXi0rXAIc");
assertNotNull(visualization);
assertEquals(1, visualization.getUserGroupAccesses().size());
assertEquals(1, visualization.getUserAccesses().size());
assertEquals(user.getUid(), visualization.getUserAccesses().iterator().next().getUser().getUid());
assertEquals(userGroup.getUid(), visualization.getUserGroupAccesses().iterator().next().getUserGroup().getUid());
metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/metadata_visualization_with_accesses_update.json").getInputStream(), RenderFormat.JSON);
params = createParams(ImportStrategy.UPDATE, metadata);
params.setSkipSharing(false);
dbmsManager.clearSession();
report = importService.importMetadata(params);
assertEquals(Status.OK, report.getStatus());
visualization = manager.get(Visualization.class, "gyYXi0rXAIc");
assertNotNull(visualization);
assertEquals(0, visualization.getUserGroupAccesses().size());
assertEquals(0, visualization.getUserAccesses().size());
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams 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());
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams 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());
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams 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());
}
use of org.hisp.dhis.dxf2.metadata.MetadataImportParams 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());
}
Aggregations