use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class CsvImportServiceTest method testCategoryOptionImportNoHeader.
@Test
void testCategoryOptionImportNoHeader() throws IOException {
inputBasicObjects = new ClassPathResource("csv/basic_objects_no_header.csv").getInputStream();
Metadata metadata = csvImportService.fromCsv(inputBasicObjects, new CsvImportOptions().setImportClass(CsvImportClass.CATEGORY_OPTION).setFirstRowIsHeader(false));
assertEquals(3, metadata.getCategoryOptions().size());
for (CategoryOption categoryOption : metadata.getCategoryOptions()) {
assertNotNull(categoryOption.getUid());
assertNotNull(categoryOption.getName());
assertNotNull(categoryOption.getShortName());
}
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class CsvImportServiceTest method testCategoryOptionImport.
@Test
void testCategoryOptionImport() throws IOException {
Metadata metadata = csvImportService.fromCsv(inputBasicObjects, new CsvImportOptions().setImportClass(CsvImportClass.CATEGORY_OPTION).setFirstRowIsHeader(true));
assertEquals(3, metadata.getCategoryOptions().size());
for (CategoryOption categoryOption : metadata.getCategoryOptions()) {
assertNotNull(categoryOption.getUid());
assertNotNull(categoryOption.getName());
assertNotNull(categoryOption.getShortName());
}
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class InputUtils method getAttributeOptionCombo.
/**
* Validates and retrieves the attribute option combo. 409 conflict as
* status code along with a textual message will be set on the response in
* case of invalid input.
*
* @param categoryCombo the category combo.
* @param opts list of category option uid.
* @param attributeOptionCombo the explicit attribute option combo
* identifier.
* @return the attribute option combo identified from the given input, or
* null if the input was invalid.
*/
public CategoryOptionCombo getAttributeOptionCombo(CategoryCombo categoryCombo, Set<String> opts, String attributeOptionCombo, IdScheme idScheme) {
if (categoryCombo == null) {
throw new IllegalQueryException("Illegal category combo");
}
// ---------------------------------------------------------------------
// Attribute category options validation
// ---------------------------------------------------------------------
CategoryOptionCombo attrOptCombo = null;
if (opts != null) {
Set<CategoryOption> categoryOptions = new HashSet<>();
for (String uid : opts) {
CategoryOption categoryOption = idObjectManager.getObject(CategoryOption.class, idScheme, uid);
if (categoryOption == null) {
throw new IllegalQueryException("Illegal category option identifier: " + uid);
}
categoryOptions.add(categoryOption);
}
attrOptCombo = categoryService.getCategoryOptionCombo(categoryCombo, categoryOptions);
if (attrOptCombo == null) {
throw new IllegalQueryException("Attribute option combo does not exist for given category combo and category options");
}
} else if (attributeOptionCombo != null) {
attrOptCombo = categoryService.getCategoryOptionCombo(attributeOptionCombo);
}
if (attrOptCombo == null) {
attrOptCombo = categoryService.getDefaultCategoryOptionCombo();
}
if (attrOptCombo == null) {
throw new IllegalQueryException("Default attribute option combo does not exist");
}
return attrOptCombo;
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class DefaultCompleteDataSetRegistrationExchangeServiceTest method verifyUserHasNoWritePermissionOnCategoryOption.
@Test
void verifyUserHasNoWritePermissionOnCategoryOption() {
OrganisationUnit organisationUnit = createOrganisationUnit('A');
DataSet dataSetA = createDataSet('A', new MonthlyPeriodType());
CategoryCombo categoryCombo = createCategoryCombo('A');
CategoryOption categoryOptionA = createCategoryOption('A');
CategoryOption categoryOptionB = createCategoryOption('B');
CategoryOptionCombo categoryOptionCombo = createCategoryOptionCombo(categoryCombo, categoryOptionA, categoryOptionB);
Period period = createPeriod("201907");
String payload = createPayload(period, organisationUnit, dataSetA, categoryCombo, categoryOptionA, categoryOptionB);
try (MockedConstruction<MetadataCaches> mocked = mockConstruction(MetadataCaches.class, (mock, context) -> {
when(mock.getDataSets()).thenReturn(datasetCache);
when(mock.getPeriods()).thenReturn(periodCache);
when(mock.getOrgUnits()).thenReturn(orgUnitCache);
aocCache = new CachingMap<>();
when(mock.getAttrOptionCombos()).thenReturn(aocCache);
when(mock.getOrgUnitInHierarchyMap()).thenReturn(orgUnitInHierarchyCache);
when(mock.getAttrOptComboOrgUnitMap()).thenReturn(attrOptComboOrgUnitCache);
})) {
when(currentUserService.getCurrentUser()).thenReturn(user);
when(batchHandler.init()).thenReturn(batchHandler);
when(idObjManager.get(CategoryCombo.class, categoryCombo.getUid())).thenReturn(categoryCombo);
when(idObjManager.getObject(CategoryOption.class, IdScheme.UID, categoryOptionA.getUid())).thenReturn(categoryOptionA);
when(idObjManager.getObject(CategoryOption.class, IdScheme.UID, categoryOptionB.getUid())).thenReturn(categoryOptionB);
when(categoryService.getCategoryOptionCombo(categoryCombo, Sets.newHashSet(categoryOptionA, categoryOptionB))).thenReturn(categoryOptionCombo);
when(datasetCache.get(eq(dataSetA.getUid()), any())).thenReturn(dataSetA);
when(periodCache.get(eq(period.getIsoDate()), any())).thenReturn(period);
when(orgUnitCache.get(eq(organisationUnit.getUid()), any())).thenReturn(createOrganisationUnit('A'));
when(orgUnitInHierarchyCache.get(eq(organisationUnit.getUid()), any())).thenReturn(Boolean.TRUE);
when(attrOptComboOrgUnitCache.get(eq(categoryOptionCombo.getUid() + organisationUnit.getUid()), any())).thenReturn(Boolean.TRUE);
when(categoryService.getCategoryOptionCombo(categoryOptionCombo.getUid())).thenReturn(categoryOptionCombo);
// force error on access check for Category Option Combo
when(aclService.canDataWrite(user, dataSetA)).thenReturn(true);
when(aclService.canDataWrite(user, categoryOptionA)).thenReturn(false);
when(aclService.canDataWrite(user, categoryOptionB)).thenReturn(true);
when(notifier.clear(null)).thenReturn(notifier);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS)).thenReturn(false);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ATTRIBUTE_OPTION_COMBOS)).thenReturn(false);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS)).thenReturn(false);
when(systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_REQUIRE_ATTRIBUTE_OPTION_COMBO)).thenReturn(false);
when(currentUserService.getCurrentUserOrganisationUnits()).thenReturn(Collections.singleton(createOrganisationUnit('A')));
when(i18nManager.getI18n()).thenReturn(i18n);
when(categoryService.getDefaultCategoryOptionCombo()).thenReturn(DEFAULT_COC);
when(batchHandlerFactory.createBatchHandler(CompleteDataSetRegistrationBatchHandler.class)).thenReturn(batchHandler);
when(notifier.notify(any(), anyString())).thenReturn(notifier);
when(notifier.notify(null, NotificationLevel.INFO, "Import done", true)).thenReturn(notifier);
// call method under test
ImportSummary summary = subject.saveCompleteDataSetRegistrationsJson(new ByteArrayInputStream(payload.getBytes()), new ImportOptions());
assertThat(summary.getStatus(), is(ImportStatus.ERROR));
assertThat(summary.getImportCount().getIgnored(), is(1));
assertEquals(1, summary.getConflictCount());
assertThat(summary.getConflicts().iterator().next().getValue(), is("User has no data write access for CategoryOption: " + categoryOptionA.getUid()));
}
}
use of org.hisp.dhis.category.CategoryOption in project dhis2-core by dhis2.
the class EventImportTest method setUpTest.
@Override
protected void setUpTest() throws Exception {
userService = _userService;
organisationUnitA = createOrganisationUnit('A');
organisationUnitB = createOrganisationUnit('B');
manager.save(organisationUnitA);
manager.save(organisationUnitB);
TrackedEntityType trackedEntityType = createTrackedEntityType('A');
trackedEntityTypeService.addTrackedEntityType(trackedEntityType);
org.hisp.dhis.trackedentity.TrackedEntityInstance maleA = createTrackedEntityInstance(organisationUnitA);
maleA.setTrackedEntityType(trackedEntityType);
manager.save(maleA);
trackedEntityInstanceMaleA = trackedEntityInstanceService.getTrackedEntityInstance(maleA);
CategoryOption categoryOption1 = new CategoryOption("male");
categoryOption1.setAutoFields();
CategoryOption categoryOption2 = new CategoryOption("female");
categoryOption2.setAutoFields();
manager.save(Lists.newArrayList(categoryOption1, categoryOption2));
Category cat1 = new Category("cat1", DataDimensionType.DISAGGREGATION);
cat1.setShortName(cat1.getName());
cat1.setCategoryOptions(Lists.newArrayList(categoryOption1, categoryOption2));
manager.save(Lists.newArrayList(cat1));
CategoryCombo categoryCombo = manager.getByName(CategoryCombo.class, "default");
categoryCombo.setCategories(Lists.newArrayList(cat1));
dataElementA = createDataElement('A');
dataElementA.setValueType(ValueType.INTEGER);
dataElementA.setCategoryCombo(categoryCombo);
manager.save(dataElementA);
dataElementA2 = createDataElement('a');
dataElementA2.setValueType(ValueType.INTEGER);
dataElementA2.setCategoryCombo(categoryCombo);
manager.save(dataElementA2);
dataElementB = createDataElement('B');
dataElementB.setValueType(ValueType.INTEGER);
dataElementB.setCategoryCombo(categoryCombo);
manager.save(dataElementB);
programStageA = createProgramStage('A', 0);
programStageA.setFeatureType(FeatureType.POINT);
manager.save(programStageA);
programStageA2 = createProgramStage('a', 0);
programStageA2.setFeatureType(FeatureType.POINT);
programStageA2.setRepeatable(true);
manager.save(programStageA2);
programStageB = createProgramStage('B', 0);
programStageB.setFeatureType(FeatureType.POINT);
manager.save(programStageB);
programA = createProgram('A', new HashSet<>(), organisationUnitA);
programA.setProgramType(ProgramType.WITH_REGISTRATION);
programA.setCategoryCombo(categoryCombo);
manager.save(programA);
programB = createProgram('B', new HashSet<>(), organisationUnitB);
programB.setProgramType(ProgramType.WITHOUT_REGISTRATION);
programB.setCategoryCombo(categoryCombo);
manager.save(programB);
ProgramStageDataElement programStageDataElement = new ProgramStageDataElement();
programStageDataElement.setDataElement(dataElementA);
programStageDataElement.setProgramStage(programStageA);
programStageDataElementService.addProgramStageDataElement(programStageDataElement);
ProgramStageDataElement programStageDataElementA2 = new ProgramStageDataElement();
programStageDataElementA2.setDataElement(dataElementA2);
programStageDataElementA2.setProgramStage(programStageA2);
programStageDataElementService.addProgramStageDataElement(programStageDataElementA2);
ProgramStageDataElement programStageDataElementB = new ProgramStageDataElement();
programStageDataElementB.setDataElement(dataElementB);
programStageDataElementB.setProgramStage(programStageB);
programStageDataElementService.addProgramStageDataElement(programStageDataElementB);
programStageA.getProgramStageDataElements().add(programStageDataElement);
programStageA2.getProgramStageDataElements().add(programStageDataElementA2);
programStageA.setProgram(programA);
programStageA2.setProgram(programA);
programA.getProgramStages().add(programStageA);
programA.getProgramStages().add(programStageA2);
programStageB.getProgramStageDataElements().add(programStageDataElementB);
programStageB.setProgram(programB);
programB.getProgramStages().add(programStageB);
manager.update(programStageA);
manager.update(programStageA2);
manager.update(programA);
manager.update(programStageB);
manager.update(programB);
pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(programB);
pi.setStatus(ProgramStatus.ACTIVE);
pi.setStoredBy("test");
pi.setName("EventImportTestPI");
pi.setUid(CodeGenerator.generateUid());
manager.save(pi);
event = createEvent("eventUid001");
createUserAndInjectSecurityContext(true);
// Flush all data to disk
manager.flush();
}
Aggregations