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());
}
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());
}
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"));
}
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());
}
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));
}
Aggregations