Search in sources :

Example 1 with SchemaProperty

use of org.hisp.dhis.dto.schemas.SchemaProperty 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 SchemaProperty

use of org.hisp.dhis.dto.schemas.SchemaProperty 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

JsonObject (com.google.gson.JsonObject)2 RestApiActions (org.hisp.dhis.actions.RestApiActions)2 SchemasActions (org.hisp.dhis.actions.SchemasActions)2 SchemaProperty (org.hisp.dhis.dto.schemas.SchemaProperty)2 JsonElement (com.google.gson.JsonElement)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Stream (java.util.stream.Stream)1 Matchers (org.hamcrest.Matchers)1 ApiTest (org.hisp.dhis.ApiTest)1 IdGenerator (org.hisp.dhis.actions.IdGenerator)1 LoginActions (org.hisp.dhis.actions.LoginActions)1 ApiResponse (org.hisp.dhis.dto.ApiResponse)1 PropertyType (org.hisp.dhis.dto.schemas.PropertyType)1 Schema (org.hisp.dhis.dto.schemas.Schema)1 ResponseValidationHelper (org.hisp.dhis.helpers.ResponseValidationHelper)1 DataGenerator (org.hisp.dhis.utils.DataGenerator)1 Assumptions (org.junit.jupiter.api.Assumptions)1