Search in sources :

Example 1 with IdGenerator

use of org.hisp.dhis.actions.IdGenerator in project dhis2-core by dhis2.

the class XmlFileReader method replacePropertyValuesWith.

@Override
public FileReader replacePropertyValuesWith(String propertyName, String replacedValue) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    try {
        NodeList nodes = (NodeList) xPath.evaluate("//*[@" + propertyName + "]", document, XPathConstants.NODESET);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i).getAttributes().getNamedItem(propertyName);
            if (replacedValue.equalsIgnoreCase("uniqueid")) {
                node.setNodeValue(new IdGenerator().generateUniqueId());
                continue;
            }
            node.setNodeValue(replacedValue);
        }
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    return this;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IdGenerator(org.hisp.dhis.actions.IdGenerator)

Example 2 with IdGenerator

use of org.hisp.dhis.actions.IdGenerator in project dhis2-core by dhis2.

the class EventImportDataValueValidationTests method setupData.

private void setupData() throws Exception {
    programId = new IdGenerator().generateUniqueId();
    programStageId = new IdGenerator().generateUniqueId();
    JsonObject jsonObject = new JsonObjectBuilder(new FileReaderUtils().readJsonAndGenerateData(new File("src/test/resources/tracker/eventProgram.json"))).addPropertyByJsonPath("programStages[0].program.id", programId).addPropertyByJsonPath("programs[0].id", programId).addPropertyByJsonPath("programs[0].programStages[0].id", programStageId).addPropertyByJsonPath("programStages[0].id", programStageId).addPropertyByJsonPath("programStages[0].programStageDataElements", null).build();
    new MetadataActions().importAndValidateMetadata(jsonObject);
    String dataElementId = dataElementActions.get("?fields=id&filter=domainType:eq:TRACKER&filter=valueType:eq:TEXT&pageSize=1").extractString("dataElements.id[0]");
    assertNotNull(dataElementId, "Failed to create data elements");
    mandatoryDataElementId = dataElementId;
    programActions.addDataElement(programStageId, dataElementId, true).validate().statusCode(200);
}
Also used : FileReaderUtils(org.hisp.dhis.helpers.file.FileReaderUtils) JsonObject(com.google.gson.JsonObject) MetadataActions(org.hisp.dhis.actions.metadata.MetadataActions) IdGenerator(org.hisp.dhis.actions.IdGenerator) JsonObjectBuilder(org.hisp.dhis.helpers.JsonObjectBuilder) File(java.io.File)

Example 3 with IdGenerator

use of org.hisp.dhis.actions.IdGenerator in project dhis2-core by dhis2.

the class EnrollmentsTests method shouldOnlyEnrollOnce.

@ValueSource(strings = { "true", "false" })
@ParameterizedTest
public void shouldOnlyEnrollOnce(String shouldEnrollOnce) throws Exception {
    // arrange
    String program = programActions.createTrackerProgram(Constants.TRACKED_ENTITY_TYPE, Constants.ORG_UNIT_IDS).extractUid();
    JsonObject object = programActions.get(program).getBodyAsJsonBuilder().addProperty("onlyEnrollOnce", shouldEnrollOnce).addProperty("publicAccess", "rwrw----").build();
    programActions.update(program, object).validateStatus(200);
    String tei = super.importTei();
    JsonObject enrollment = new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).array(program, Constants.ORG_UNIT_IDS[2], tei, "COMPLETED");
    trackerActions.postAndGetJobReport(enrollment).validateSuccessfulImport();
    // act
    TrackerApiResponse response = trackerActions.postAndGetJobReport(new EnrollmentDataBuilder().array(program, Constants.ORG_UNIT_IDS[2], tei, "ACTIVE"));
    // assert
    if (Boolean.parseBoolean(shouldEnrollOnce)) {
        response.validateErrorReport().body("errorCode", hasItems("E1016"));
        return;
    }
    response.validateSuccessfulImport();
    trackerActions.getTrackedEntity(tei + "?fields=enrollments").validate().body("enrollments", hasSize(2));
}
Also used : JsonObject(com.google.gson.JsonObject) EnrollmentDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder) IdGenerator(org.hisp.dhis.actions.IdGenerator) TrackerApiResponse(org.hisp.dhis.dto.TrackerApiResponse) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with IdGenerator

use of org.hisp.dhis.actions.IdGenerator in project dhis2-core by dhis2.

the class EnrollmentAttributeTests method shouldValidateUniquenessWithinThePayload.

@Test
public void shouldValidateUniquenessWithinThePayload() throws Exception {
    String tei = importTei();
    String teiB = importTei();
    String value = DataGenerator.randomString();
    Function<String, JsonObject> singleEnrollment = (teiId) -> {
        return new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).setTei(teiId).addAttribute(uniqueAttributeId, value).setProgram(programId).setOu(Constants.ORG_UNIT_IDS[1]).single();
    };
    JsonObject payload = new JsonObjectBuilder().addOrAppendToArray("enrollments", singleEnrollment.apply(tei), singleEnrollment.apply(teiB)).build();
    trackerActions.postAndGetJobReport(payload).validateErrorReport();
}
Also used : JsonObject(com.google.gson.JsonObject) DataGenerator(org.hisp.dhis.utils.DataGenerator) BeforeEach(org.junit.jupiter.api.BeforeEach) Constants(org.hisp.dhis.Constants) Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) IdGenerator(org.hisp.dhis.actions.IdGenerator) ProgramActions(org.hisp.dhis.actions.metadata.ProgramActions) Matchers(org.hamcrest.Matchers) Disabled(org.junit.jupiter.api.Disabled) Function(java.util.function.Function) JsonObjectBuilder(org.hisp.dhis.helpers.JsonObjectBuilder) QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) Test(org.junit.jupiter.api.Test) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest) EnrollmentDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder) TrackedEntityAttributeActions(org.hisp.dhis.actions.metadata.TrackedEntityAttributeActions) JsonObject(com.google.gson.JsonObject) EnrollmentDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder) IdGenerator(org.hisp.dhis.actions.IdGenerator) JsonObjectBuilder(org.hisp.dhis.helpers.JsonObjectBuilder) Test(org.junit.jupiter.api.Test) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest)

Example 5 with IdGenerator

use of org.hisp.dhis.actions.IdGenerator in project dhis2-core by dhis2.

the class EnrollmentAttributeTests method shouldRemoveAttributeValue.

@Test
public void shouldRemoveAttributeValue() throws Exception {
    String tei = importTei();
    JsonObject payload = new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).setTei(tei).addAttribute(optionSetAttributeId, "TA_YES").array(programId, Constants.ORG_UNIT_IDS[1]);
    trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
    payload = new JsonObjectBuilder(payload).addPropertyByJsonPath("enrollments[0].attributes[0].value", null).build();
    trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
    trackerActions.getTrackedEntity(tei + "?program=" + programId).validateStatus(200).validate().statusCode(200).body("attributes", hasSize(greaterThanOrEqualTo(1))).body("attributes.attribute", not(hasItem(optionSetAttributeId))).body("attributes.value", not(hasItem("TA_YES")));
}
Also used : JsonObject(com.google.gson.JsonObject) EnrollmentDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder) IdGenerator(org.hisp.dhis.actions.IdGenerator) JsonObjectBuilder(org.hisp.dhis.helpers.JsonObjectBuilder) Test(org.junit.jupiter.api.Test) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest)

Aggregations

IdGenerator (org.hisp.dhis.actions.IdGenerator)10 JsonObject (com.google.gson.JsonObject)9 TrackerNtiApiTest (org.hisp.dhis.tracker.TrackerNtiApiTest)6 EnrollmentDataBuilder (org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder)6 Test (org.junit.jupiter.api.Test)6 JsonObjectBuilder (org.hisp.dhis.helpers.JsonObjectBuilder)4 TrackerApiResponse (org.hisp.dhis.dto.TrackerApiResponse)2 QueryParamsBuilder (org.hisp.dhis.helpers.QueryParamsBuilder)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 File (java.io.File)1 Arrays (java.util.Arrays)1 Function (java.util.function.Function)1 XPath (javax.xml.xpath.XPath)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 CoreMatchers.hasItem (org.hamcrest.CoreMatchers.hasItem)1 Matchers (org.hamcrest.Matchers)1 Constants (org.hisp.dhis.Constants)1 RestApiActions (org.hisp.dhis.actions.RestApiActions)1