Search in sources :

Example 1 with SchemasActions

use of org.hisp.dhis.actions.SchemasActions 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)

Example 2 with SchemasActions

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

the class MetadataImportBasedOnSchemasTest method beforeAll.

@BeforeAll
public void beforeAll() {
    schemasActions = new SchemasActions();
    loginActions = new LoginActions();
    loginActions.loginAsSuperUser();
}
Also used : SchemasActions(org.hisp.dhis.actions.SchemasActions) LoginActions(org.hisp.dhis.actions.LoginActions) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

SchemasActions (org.hisp.dhis.actions.SchemasActions)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 IdGenerator (org.hisp.dhis.actions.IdGenerator)1 LoginActions (org.hisp.dhis.actions.LoginActions)1 RestApiActions (org.hisp.dhis.actions.RestApiActions)1 Schema (org.hisp.dhis.dto.schemas.Schema)1 SchemaProperty (org.hisp.dhis.dto.schemas.SchemaProperty)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1