Search in sources :

Example 6 with IdGenerator

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

the class EnrollmentAttributeTests method shouldValidateUniqueness.

@Test
public void shouldValidateUniqueness() throws Exception {
    String tei = importTei();
    String value = DataGenerator.randomString();
    JsonObject payload = new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).setTei(tei).addAttribute(uniqueAttributeId, value).array(programId, Constants.ORG_UNIT_IDS[1]);
    trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
    payload = new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).setTei(importTei()).addAttribute(uniqueAttributeId, value).array(programId, Constants.ORG_UNIT_IDS[1]);
    trackerActions.postAndGetJobReport(payload).validateErrorReport();
}
Also used : JsonObject(com.google.gson.JsonObject) EnrollmentDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder) IdGenerator(org.hisp.dhis.actions.IdGenerator) Test(org.junit.jupiter.api.Test) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest)

Example 7 with IdGenerator

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

the class RelationshipsTests method shouldNotUpdateRelationship.

@Test
public void shouldNotUpdateRelationship() {
    // arrange
    String relationshipId = new IdGenerator().generateUniqueId();
    JsonObject relationship = JsonObjectBuilder.jsonObject().addProperty("relationship", relationshipId).addProperty("relationshipType", relationshipType).addObject("from", JsonObjectBuilder.jsonObject().addProperty("trackedEntity", teis.get(0))).addObject("to", JsonObjectBuilder.jsonObject().addProperty("trackedEntity", teis.get(1))).wrapIntoArray("relationships");
    trackerActions.postAndGetJobReport(relationship).validate().statusCode(200);
    JsonObject relationshipBody = trackerActions.get("/relationships/" + relationshipId).getBody();
    JsonObjectBuilder.jsonObject(relationship).addObjectByJsonPath("relationships[0]", "from", JsonObjectBuilder.jsonObject().addProperty("trackedEntity", teis.get(1)).build()).addObjectByJsonPath("relationships[0]", "to", JsonObjectBuilder.jsonObject().addProperty("trackedEntity", teis.get(0)).build()).build();
    // act
    TrackerApiResponse response = trackerActions.postAndGetJobReport(relationship, new QueryParamsBuilder().add("importStrategy=UPDATE"));
    // assert
    response.validateErrorReport();
    assertThat(trackerActions.get("/relationships/" + relationshipId).getBody(), matchesJSON(relationshipBody));
}
Also used : QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) JsonObject(com.google.gson.JsonObject) IdGenerator(org.hisp.dhis.actions.IdGenerator) TrackerApiResponse(org.hisp.dhis.dto.TrackerApiResponse) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with IdGenerator

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

the class EnrollmentAttributeTests method shouldRejectTetAttributes.

@Test
public void shouldRejectTetAttributes() throws Exception {
    String tetAttribute = "dIVt4l5vIOa";
    String tei = importTei();
    JsonObject payload = new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).setTei(tei).addAttribute(tetAttribute, "NOT_A_VALUE").array(programId, Constants.ORG_UNIT_IDS[1]);
    trackerActions.postAndGetJobReport(payload).validateErrorReport().body("", hasSize(1)).body("errorCode", hasItem("E1019")).body("message", hasItem(containsStringIgnoringCase("Only program attributes")));
}
Also used : JsonObject(com.google.gson.JsonObject) EnrollmentDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EnrollmentDataBuilder) IdGenerator(org.hisp.dhis.actions.IdGenerator) Test(org.junit.jupiter.api.Test) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest)

Example 9 with IdGenerator

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

the class EnrollmentAttributeTests method shouldUpdateAttributeValue.

@Test
public void shouldUpdateAttributeValue() throws Exception {
    String tei = importTei();
    JsonObject payload = new EnrollmentDataBuilder().setId(new IdGenerator().generateUniqueId()).setTei(tei).addAttribute(numberAttributeId, "5").array(programId, Constants.ORG_UNIT_IDS[1]);
    trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
    payload = new JsonObjectBuilder(payload).addPropertyByJsonPath("enrollments[0].attributes[0].value", "9").build();
    trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
    trackerActions.getTrackedEntity(tei + "?program=" + programId).validateStatus(200).validate().statusCode(200).body("attributes", hasSize(greaterThanOrEqualTo(1))).body("attributes.attribute", hasItem(numberAttributeId)).body("attributes.value", hasItem("9"));
}
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)

Example 10 with IdGenerator

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

the class DataGenerator method generateObjectMatchingSchema.

public static JsonObject generateObjectMatchingSchema(List<SchemaProperty> schemaProperties) {
    JsonObject objectBody = new JsonObject();
    for (SchemaProperty prop : schemaProperties) {
        JsonElement element;
        if (prop.getPropertyType() == PropertyType.REFERENCE) {
            List<SchemaProperty> referenceProperties = new SchemasActions().getRequiredProperties(prop.getName());
            JsonObject referenceObject = generateObjectMatchingSchema(referenceProperties);
            String uid = new RestApiActions(prop.getRelativeApiEndpoint()).post(referenceObject).extractUid();
            referenceObject.addProperty("id", uid);
            element = referenceObject;
        } else if (prop.getPropertyType() == PropertyType.IDENTIFIER) {
            if (!StringUtils.containsAny(prop.getName(), "id", "uid", "code")) {
                Schema schema = new SchemasActions().getSchema(prop.getName());
                JsonObject referenceObject = generateObjectMatchingSchema(schema.getRequiredProperties());
                String uid = new RestApiActions(schema.getPlural()).post(referenceObject).extractUid();
                element = new JsonPrimitive(uid);
            } else {
                element = new JsonPrimitive(new IdGenerator().generateUniqueId());
            }
        } else {
            element = generateRandomValueMatchingSchema(prop);
        }
        objectBody.add(prop.getName(), element);
    }
    return objectBody;
}
Also used : RestApiActions(org.hisp.dhis.actions.RestApiActions) SchemaProperty(org.hisp.dhis.dto.schemas.SchemaProperty) JsonPrimitive(com.google.gson.JsonPrimitive) JsonElement(com.google.gson.JsonElement) SchemasActions(org.hisp.dhis.actions.SchemasActions) Schema(org.hisp.dhis.dto.schemas.Schema) JsonObject(com.google.gson.JsonObject) IdGenerator(org.hisp.dhis.actions.IdGenerator)

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