Search in sources :

Example 86 with QueryParamsBuilder

use of org.hisp.dhis.helpers.QueryParamsBuilder in project dhis2-core by dhis2.

the class SystemSettingsTests method deleteSystemSetting.

@Test
public void deleteSystemSetting() {
    prepareData();
    ApiResponse response = systemSettingActions.delete(APPLICATION_INTRO_KEY);
    response.validate().statusCode(204);
    response = systemSettingActions.get(APPLICATION_INTRO_KEY, ContentType.TEXT.toString(), ContentType.TEXT.toString(), new QueryParamsBuilder());
    response.validate().statusCode(200);
    assertEquals(StringUtils.EMPTY, response.getAsString());
}
Also used : QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) ApiResponse(org.hisp.dhis.dto.ApiResponse) Test(org.junit.jupiter.api.Test) ApiTest(org.hisp.dhis.ApiTest)

Example 87 with QueryParamsBuilder

use of org.hisp.dhis.helpers.QueryParamsBuilder in project dhis2-core by dhis2.

the class TrackedEntityInstanceAclReadTests method setupAccessMap.

/**
 * Finds metadata a user has access to and determines what data can be read
 * or not based on sharing.
 *
 * @param user the user to setup
 */
private void setupAccessMap(User user) {
    Map<String, List<String>> dataRead = new HashMap<>();
    // Configure params to only return metadata we care about
    String params = (new QueryParamsBuilder()).add("trackedEntityTypes=true").add("dataElements=true").add("relationshipTypes=true").add("programs=true").add("trackedEntityAttributes=true").add("programStages=true").add("fields=id,userAccesses,publicAccess,userGroupAccesses").build();
    ApiResponse response = metadataActions.get(params);
    // Build map
    response.getBody().entrySet().forEach((entry) -> {
        // Skip the System property.
        if (!entry.getKey().equals("system")) {
            dataRead.put(entry.getKey(), new ArrayList<>());
            entry.getValue().getAsJsonArray().forEach(obj -> {
                JsonObject object = obj.getAsJsonObject();
                boolean hasDataRead = false;
                if (object.has("publicAccess") && object.get("publicAccess").getAsString().matches(_DATAREAD)) {
                    hasDataRead = true;
                } else {
                    JsonArray userAccesses = object.getAsJsonArray("userAccesses").getAsJsonArray();
                    JsonArray userGroupAccess = object.getAsJsonArray("userGroupAccesses").getAsJsonArray();
                    for (JsonElement access : userAccesses) {
                        if (access.getAsJsonObject().get("userUid").getAsString().equals(user.getUid()) && access.getAsJsonObject().get("access").getAsString().matches(_DATAREAD)) {
                            hasDataRead = true;
                        }
                    }
                    if (!hasDataRead) {
                        for (JsonElement access : userGroupAccess) {
                            if (user.getGroups().contains(access.getAsJsonObject().get("userGroupUid").getAsString()) && access.getAsJsonObject().get("access").getAsString().matches(_DATAREAD)) {
                                hasDataRead = true;
                            }
                        }
                    }
                }
                if (hasDataRead) {
                    dataRead.get(entry.getKey()).add(obj.getAsJsonObject().get("id").getAsString());
                }
            });
        }
    });
    user.setDataRead(dataRead);
}
Also used : JsonArray(com.google.gson.JsonArray) HashMap(java.util.HashMap) JsonElement(com.google.gson.JsonElement) QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) ApiResponse(org.hisp.dhis.dto.ApiResponse)

Example 88 with QueryParamsBuilder

use of org.hisp.dhis.helpers.QueryParamsBuilder in project dhis2-core by dhis2.

the class MetadataImportTest method shouldReturnObjectReportsWhenSomeMetadataWasIgnoredAndAtomicModeFalse.

@Test
public void shouldReturnObjectReportsWhenSomeMetadataWasIgnoredAndAtomicModeFalse() throws Exception {
    // arrange
    QueryParamsBuilder queryParamsBuilder = new QueryParamsBuilder();
    queryParamsBuilder.addAll("async=false", "importReportMode=DEBUG", "importStrategy=CREATE", "atomicMode=NONE");
    JsonObject object = new FileReaderUtils().readJsonAndGenerateData(new File("src/test/resources/metadata/uniqueMetadata.json"));
    // act
    ApiResponse response = metadataActions.post(object, queryParamsBuilder);
    response.validate().statusCode(200);
    JsonObject newObj = new FileReaderUtils().readJsonAndGenerateData(new File("src/test/resources/metadata/uniqueMetadata.json"));
    // add one of the orgunits from already imported metadata to get it
    // ignored
    newObj.get("organisationUnits").getAsJsonArray().add(object.get("organisationUnits").getAsJsonArray().get(0));
    response = metadataActions.post(newObj, queryParamsBuilder);
    // assert
    response.validate().statusCode(409).rootPath("response").body("stats", notNullValue()).body("stats.total", greaterThan(1)).body("stats.ignored", equalTo(1)).body("stats.created", equalTo((Integer) response.extract("response.stats.total") - 1));
    int total = (int) response.extract("response.stats.total");
    List<ObjectReport> objectReports = getObjectReports(response.getTypeReports());
    assertNotNull(objectReports);
    validateCreatedEntities(objectReports);
    assertThat(objectReports, hasItems(hasProperty("errorReports", notNullValue())));
    assertEquals(total, objectReports.size(), "Not all imported entities had object reports");
}
Also used : FileReaderUtils(org.hisp.dhis.helpers.file.FileReaderUtils) QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) JsonObject(com.google.gson.JsonObject) ObjectReport(org.hisp.dhis.dto.ObjectReport) File(java.io.File) ApiResponse(org.hisp.dhis.dto.ApiResponse) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ApiTest(org.hisp.dhis.ApiTest)

Example 89 with QueryParamsBuilder

use of org.hisp.dhis.helpers.QueryParamsBuilder in project dhis2-core by dhis2.

the class MetadataImportTest method shouldUpdateExistingMetadata.

@ParameterizedTest(name = "withImportStrategy[{0}]")
@CsvSource({ "CREATE, ignored, 409", "CREATE_AND_UPDATE, updated, 200" })
public void shouldUpdateExistingMetadata(String importStrategy, String expected, int expectedStatusCode) {
    // arrange
    JsonObject exported = metadataActions.get().getBody();
    QueryParamsBuilder queryParamsBuilder = new QueryParamsBuilder();
    queryParamsBuilder.addAll("async=false", "importReportMode=FULL", "importStrategy=" + importStrategy);
    // act
    ApiResponse response = metadataActions.post(exported, queryParamsBuilder);
    // assert
    response.validate().statusCode(expectedStatusCode).rootPath("response").body("stats", notNullValue()).rootPath("response.stats").body("total", greaterThan(0)).body("created", Matchers.equalTo(0)).body("deleted", Matchers.equalTo(0)).body("total", equalTo(response.extract("response.stats." + expected)));
    List<HashMap> typeReports = response.extractList("response.typeReports.stats");
    typeReports.forEach(x -> {
        assertEquals(x.get(expected), x.get("total"), expected + " for " + x + " not equals to total");
    });
}
Also used : HashMap(java.util.HashMap) QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) JsonObject(com.google.gson.JsonObject) ApiResponse(org.hisp.dhis.dto.ApiResponse) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 90 with QueryParamsBuilder

use of org.hisp.dhis.helpers.QueryParamsBuilder in project dhis2-core by dhis2.

the class OrgUnitProfileTests method shouldApplyDataItems.

@Test
public void shouldApplyDataItems() {
    List<String> datItems = new DataItemActions().get("", new QueryParamsBuilder().add("filter=dimensionItemType:in:[DATA_ELEMENT,PROGRAM_INDICATOR]")).extractList("dataItems.id");
    JsonArray array = new JsonArray();
    datItems.forEach(p -> array.add(p));
    orgUnitProfileActions.post(new JsonObjectBuilder().addArray("dataItems", array).build()).validate().statusCode(200);
    orgUnitProfileActions.get().validate().body("dataItems", hasSize(greaterThanOrEqualTo(1)));
// todo add validation for organisationUnitProfile/id/data
}
Also used : JsonArray(com.google.gson.JsonArray) DataItemActions(org.hisp.dhis.actions.dataitem.DataItemActions) QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) JsonObjectBuilder(org.hisp.dhis.helpers.JsonObjectBuilder) Test(org.junit.jupiter.api.Test) ApiTest(org.hisp.dhis.ApiTest)

Aggregations

QueryParamsBuilder (org.hisp.dhis.helpers.QueryParamsBuilder)90 JsonObject (com.google.gson.JsonObject)54 Test (org.junit.jupiter.api.Test)51 ApiResponse (org.hisp.dhis.dto.ApiResponse)49 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)43 ApiTest (org.hisp.dhis.ApiTest)27 TrackerApiResponse (org.hisp.dhis.dto.TrackerApiResponse)21 File (java.io.File)20 TrackerNtiApiTest (org.hisp.dhis.tracker.TrackerNtiApiTest)20 FileReaderUtils (org.hisp.dhis.helpers.file.FileReaderUtils)14 MethodSource (org.junit.jupiter.params.provider.MethodSource)13 ValueSource (org.junit.jupiter.params.provider.ValueSource)9 JsonObjectBuilder (org.hisp.dhis.helpers.JsonObjectBuilder)8 EventDataBuilder (org.hisp.dhis.tracker.importer.databuilder.EventDataBuilder)6 TeiDataBuilder (org.hisp.dhis.tracker.importer.databuilder.TeiDataBuilder)5 ProgramActions (org.hisp.dhis.actions.metadata.ProgramActions)4 JsonArray (com.google.gson.JsonArray)3 HashMap (java.util.HashMap)3 ProgramStageActions (org.hisp.dhis.actions.metadata.ProgramStageActions)3 ImportSummary (org.hisp.dhis.dto.ImportSummary)3