Search in sources :

Example 1 with RestApiActions

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

the class MetadataImportBasedOnSchemasTest method postBasedOnSchema.

@ParameterizedTest
@MethodSource("getSchemaEndpoints")
public void postBasedOnSchema(String endpoint, String schema) {
    RestApiActions apiActions = new RestApiActions(endpoint);
    List<String> blacklistedEndpoints = Arrays.asList("jobConfigurations", "relationshipTypes", "messageConversations", "users", "organisationUnitLevels", "programRuleActions", "programRuleVariables", "eventCharts", // blacklisted because contains
    "programStages");
    // conditionally required properties, which
    // are not marked as required
    List<SchemaProperty> schemaProperties = schemasActions.getRequiredProperties(schema);
    Assumptions.assumeFalse(blacklistedEndpoints.contains(endpoint), "N/A test case - blacklisted endpoint.");
    Assumptions.assumeFalse(schemaProperties.stream().anyMatch(schemaProperty -> schemaProperty.getPropertyType() == PropertyType.COMPLEX), "N/A test case - body would require COMPLEX objects.");
    // post
    JsonObject object = DataGenerator.generateObjectMatchingSchema(schemaProperties);
    ApiResponse response = apiActions.post(object);
    // validate response;
    ResponseValidationHelper.validateObjectCreation(response);
    // validate removal;
    response = apiActions.delete(response.extractUid());
    ResponseValidationHelper.validateObjectRemoval(response, endpoint + " was not deleted");
}
Also used : DataGenerator(org.hisp.dhis.utils.DataGenerator) JsonObject(com.google.gson.JsonObject) Arrays(java.util.Arrays) SchemaProperty(org.hisp.dhis.dto.schemas.SchemaProperty) ResponseValidationHelper(org.hisp.dhis.helpers.ResponseValidationHelper) Matchers(org.hamcrest.Matchers) Arguments(org.junit.jupiter.params.provider.Arguments) ApiResponse(org.hisp.dhis.dto.ApiResponse) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Stream(java.util.stream.Stream) Assumptions(org.junit.jupiter.api.Assumptions) BeforeAll(org.junit.jupiter.api.BeforeAll) RestApiActions(org.hisp.dhis.actions.RestApiActions) ApiTest(org.hisp.dhis.ApiTest) SchemasActions(org.hisp.dhis.actions.SchemasActions) PropertyType(org.hisp.dhis.dto.schemas.PropertyType) LoginActions(org.hisp.dhis.actions.LoginActions) MethodSource(org.junit.jupiter.params.provider.MethodSource) RestApiActions(org.hisp.dhis.actions.RestApiActions) SchemaProperty(org.hisp.dhis.dto.schemas.SchemaProperty) JsonObject(com.google.gson.JsonObject) ApiResponse(org.hisp.dhis.dto.ApiResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with RestApiActions

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

the class MetadataImportBasedOnSchemasTest method getMatchesSchema.

@ParameterizedTest
@MethodSource("getSchemaEndpoints")
public // todo add better schema validation when spec is ready
void getMatchesSchema(String endpoint, String schema) {
    RestApiActions apiActions = new RestApiActions(endpoint);
    ApiResponse response = apiActions.get("?fields=*");
    response.validate().statusCode(200).body(endpoint, Matchers.notNullValue());
    Object firstObject = response.extract(endpoint + "[0]");
    if (firstObject == null) {
        return;
    }
    schemasActions.validateObjectAgainstSchema(schema, firstObject).validate().statusCode(200);
}
Also used : RestApiActions(org.hisp.dhis.actions.RestApiActions) JsonObject(com.google.gson.JsonObject) ApiResponse(org.hisp.dhis.dto.ApiResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with RestApiActions

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

the class TestCleanUp method deleteEntity.

public boolean deleteEntity(String resource, String id) {
    ApiResponse response = new RestApiActions(resource).delete(id + "?force=true");
    if (response.statusCode() == 200 || response.statusCode() == 404) {
        logger.info(String.format("Entity from resource %s with id %s deleted", resource, id));
        if (response.containsImportSummaries()) {
            return response.extract("response.importCount.deleted").equals(1);
        }
        return true;
    }
    logger.warning(String.format("Entity from resource %s with id %s was not deleted. Status code: %s", resource, id, response.statusCode()));
    return false;
}
Also used : RestApiActions(org.hisp.dhis.actions.RestApiActions) ApiResponse(org.hisp.dhis.dto.ApiResponse)

Example 4 with RestApiActions

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

the class RuleEngineTests method shouldSendProgramRuleNotification.

@Test
public void shouldSendProgramRuleNotification() {
    JsonObject payload = new EventDataBuilder().addDataValue("ILRgzHhzFkg", "true").addDataValue("z3Z4TD3oBCP", "true").addDataValue("BuZ5LGNfGEU", "40").array(Constants.ORG_UNIT_IDS[0], eventProgramId, "Mt6Ac5brjoK");
    loginActions.loginAsAdmin();
    ApiResponse response = new RestApiActions("/messageConversations").get("", new QueryParamsBuilder().add("fields=*"));
    int size = response.getBody().getAsJsonArray("messageConversations").size();
    loginActions.loginAsSuperUser();
    trackerActions.postAndGetJobReport(payload).validateSuccessfulImport();
    loginActions.loginAsAdmin();
    messageConversationsActions.waitForNotification(size + 1);
    messageConversationsActions.get("", new QueryParamsBuilder().add("fields=*")).validate().statusCode(200).body("messageConversations", hasSize(size + 1)).body("messageConversations.subject", hasItem("Program rule triggered"));
}
Also used : RestApiActions(org.hisp.dhis.actions.RestApiActions) EventDataBuilder(org.hisp.dhis.tracker.importer.databuilder.EventDataBuilder) QueryParamsBuilder(org.hisp.dhis.helpers.QueryParamsBuilder) JsonObject(com.google.gson.JsonObject) ApiResponse(org.hisp.dhis.dto.ApiResponse) TrackerApiResponse(org.hisp.dhis.dto.TrackerApiResponse) Test(org.junit.jupiter.api.Test) TrackerNtiApiTest(org.hisp.dhis.tracker.TrackerNtiApiTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with RestApiActions

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

the class TrackedEntityInstanceAclReadTests method setupUser.

/**
 * Takes a User object and retrieves information about the users from the
 * api. Updates the password of the user to allow access.
 *
 * @param user to setup
 */
private void setupUser(User user) {
    userActions.updateUserPassword(user.getUid(), user.getPassword());
    new LoginActions().loginAsUser(user.getUsername(), user.getPassword());
    // Get User information from /me
    ApiResponse apiResponse = new RestApiActions("/me").get();
    String asString = apiResponse.getAsString();
    Me me = apiResponse.as(Me.class);
    // Add userGroups
    user.setGroups(me.getUserGroups().stream().map(UserGroup::getId).collect(Collectors.toList()));
    // Add search-scope ous
    user.setSearchScope(me.getTeiSearchOrganisationUnits().stream().map(OrgUnit::getId).collect(Collectors.toList()));
    // Add capture-scope ous
    user.setCaptureScope(me.getOrganisationUnits().stream().map(OrgUnit::getId).collect(Collectors.toList()));
    // Add hasAllAuthority if user has ALL authority
    user.setAllAuthority(me.getAuthorities().contains("ALL"));
    // Setup map to decide what data can and cannot be read.
    setupAccessMap(user);
}
Also used : OrgUnit(org.hisp.dhis.dto.OrgUnit) RestApiActions(org.hisp.dhis.actions.RestApiActions) Me(org.hisp.dhis.dto.Me) LoginActions(org.hisp.dhis.actions.LoginActions) ApiResponse(org.hisp.dhis.dto.ApiResponse) UserGroup(org.hisp.dhis.dto.UserGroup)

Aggregations

RestApiActions (org.hisp.dhis.actions.RestApiActions)20 LoginActions (org.hisp.dhis.actions.LoginActions)13 BeforeAll (org.junit.jupiter.api.BeforeAll)10 JsonObject (com.google.gson.JsonObject)8 ApiResponse (org.hisp.dhis.dto.ApiResponse)7 File (java.io.File)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 UserActions (org.hisp.dhis.actions.UserActions)3 MetadataActions (org.hisp.dhis.actions.metadata.MetadataActions)3 FileReaderUtils (org.hisp.dhis.helpers.file.FileReaderUtils)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 SchemasActions (org.hisp.dhis.actions.SchemasActions)2 DataElementActions (org.hisp.dhis.actions.metadata.DataElementActions)2 OrgUnitActions (org.hisp.dhis.actions.metadata.OrgUnitActions)2 EventActions (org.hisp.dhis.actions.tracker.EventActions)2 TEIActions (org.hisp.dhis.actions.tracker.TEIActions)2 SchemaProperty (org.hisp.dhis.dto.schemas.SchemaProperty)2 JsonObjectBuilder (org.hisp.dhis.helpers.JsonObjectBuilder)2 QueryParamsBuilder (org.hisp.dhis.helpers.QueryParamsBuilder)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2