Search in sources :

Example 1 with JsonIdentifiableObject

use of org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject in project dhis2-core by dhis2.

the class SharingControllerTest method testPostSharing_InvalidPublicSharing.

@Test
void testPostSharing_InvalidPublicSharing() {
    String groupId = assertStatus(HttpStatus.CREATED, POST("/userGroups", "{'name':'test'}"));
    JsonIdentifiableObject group = GET("/userGroups/{id}", groupId).content().as(JsonIdentifiableObject.class);
    JsonNode sharing = group.getSharing().node().extract().addMember("publicAccess", "\"xyz\"");
    assertWebMessage("Conflict", 409, "ERROR", "Invalid public access string: xyz", POST("/sharing?type=userGroup&id=" + groupId, "{'object': " + sharing.getDeclaration() + "}").content(HttpStatus.CONFLICT));
}
Also used : JsonIdentifiableObject(org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject) JsonNode(org.hisp.dhis.jsontree.JsonNode) Test(org.junit.jupiter.api.Test) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest)

Example 2 with JsonIdentifiableObject

use of org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject in project dhis2-core by dhis2.

the class MetadataImportExportControllerTest method testPostValidGeoJsonAttribute.

@Test
void testPostValidGeoJsonAttribute() throws IOException {
    POST("/metadata", "{\"organisationUnits\": [ {\"id\":\"rXnqqH2Pu6N\",\"name\": \"My Unit 2\",\"shortName\": \"OU2\",\"openingDate\": \"2020-01-01\"," + "\"attributeValues\": [{\"value\":  \"{\\\"type\\\": \\\"Polygon\\\"," + "\\\"coordinates\\\":  [[[100,0],[101,0],[101,1],[100,1],[100,0]]] }\"," + "\"attribute\": {\"id\": \"RRH9IFiZZYN\"}}]}]," + "\"attributes\":[{\"id\":\"RRH9IFiZZYN\",\"valueType\":\"GEOJSON\",\"organisationUnitAttribute\":true,\"name\":\"testgeojson\"}]}").content(HttpStatus.OK);
    JsonIdentifiableObject organisationUnit = GET("/organisationUnits/{id}", "rXnqqH2Pu6N").content().asObject(JsonIdentifiableObject.class);
    assertEquals(1, organisationUnit.getAttributeValues().size());
    JsonAttributeValue attributeValue = organisationUnit.getAttributeValues().get(0);
    GeoJsonObject geoJSON = new ObjectMapper().readValue(attributeValue.getValue(), GeoJsonObject.class);
    assertTrue(geoJSON instanceof Polygon);
    Polygon polygon = (Polygon) geoJSON;
    assertEquals(100, polygon.getCoordinates().get(0).get(0).getLongitude());
}
Also used : JsonIdentifiableObject(org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject) JsonAttributeValue(org.hisp.dhis.webapi.json.domain.JsonAttributeValue) GeoJsonObject(org.geojson.GeoJsonObject) Polygon(org.geojson.Polygon) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Example 3 with JsonIdentifiableObject

use of org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject in project dhis2-core by dhis2.

the class BulkPatchSharingControllerTest method testApplyPatchWithInvalidUidAtomic.

/**
 * Payload contains two DataElements but only one exists in database. Atomic
 * = true so no sharing settings will be saved.
 *
 * @throws IOException
 */
@Test
void testApplyPatchWithInvalidUidAtomic() throws IOException {
    assertStatus(HttpStatus.CREATED, POST("/users", createUser("usera", userAId)));
    assertStatus(HttpStatus.CREATED, POST("/users", createUser("userb", userBId)));
    userCId = assertStatus(HttpStatus.CREATED, POST("/users", createUser("userc")));
    assertStatus(HttpStatus.CREATED, POST("/dataElements", jsonMapper.writeValueAsString(createDataElement('B', deBId, userCId))));
    HttpResponse response = PATCH("/dataElements/sharing?atomic=true", "patch/bulk_sharing_patch.json");
    assertEquals(HttpStatus.CONFLICT, response.status());
    assertEquals("Invalid UID `" + deAId + "` for property `dataElement`", getFirstErrorMessage(response));
    JsonIdentifiableObject savedDeB = GET("/dataElements/{uid}", deBId).content(HttpStatus.OK).as(JsonIdentifiableObject.class);
    assertEquals(0, savedDeB.getSharing().getUsers().size());
}
Also used : JsonIdentifiableObject(org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Example 4 with JsonIdentifiableObject

use of org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject in project dhis2-core by dhis2.

the class AbstractCrudControllerTest method testSetSharing.

@Test
void testSetSharing() {
    String userId = getCurrentUser().getUid();
    // first create an object which can be shared
    String programId = assertStatus(HttpStatus.CREATED, POST("/programs/", "{'name':'test', 'shortName':'test', 'programType':'WITHOUT_REGISTRATION'}"));
    String sharing = "{'owner':'" + userId + "', 'public':'rwrw----', 'external': true }";
    assertStatus(HttpStatus.NO_CONTENT, PUT("/programs/" + programId + "/sharing", sharing));
    JsonIdentifiableObject program = GET("/programs/{id}", programId).content().as(JsonIdentifiableObject.class);
    assertTrue(program.exists());
    assertEquals("rwrw----", program.getSharing().getPublic().string());
    assertFalse(program.getSharing().isExternal(), "programs cannot be external");
}
Also used : JsonIdentifiableObject(org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Example 5 with JsonIdentifiableObject

use of org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject in project dhis2-core by dhis2.

the class BulkPatchSharingControllerTest method testApplyPatchWithInvalidUid.

/**
 * Payload contains two DataElements but only one exists in database.
 *
 * @throws IOException
 */
@Test
void testApplyPatchWithInvalidUid() throws IOException {
    assertStatus(HttpStatus.CREATED, POST("/users", createUser("usera", userAId)));
    assertStatus(HttpStatus.CREATED, POST("/users", createUser("userb", userBId)));
    userCId = assertStatus(HttpStatus.CREATED, POST("/users", createUser("userc")));
    assertStatus(HttpStatus.CREATED, POST("/dataElements", toJsonString(createDataElement('B', deBId, userCId))));
    HttpResponse response = PATCH("/dataElements/sharing", "patch/bulk_sharing_patch.json");
    assertEquals(HttpStatus.CONFLICT, response.status());
    assertEquals("Invalid UID `" + deAId + "` for property `dataElement`", getFirstErrorMessage(response));
    JsonIdentifiableObject savedDeB = GET("/dataElements/{uid}", deBId).content(HttpStatus.OK).as(JsonIdentifiableObject.class);
    assertEquals(2, savedDeB.getSharing().getUsers().size());
    assertNotNull(savedDeB.getSharing().getUsers().get(userAId));
    assertNotNull(savedDeB.getSharing().getUsers().get(userBId));
}
Also used : JsonIdentifiableObject(org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Aggregations

DhisControllerConvenienceTest (org.hisp.dhis.webapi.DhisControllerConvenienceTest)9 JsonIdentifiableObject (org.hisp.dhis.webapi.json.domain.JsonIdentifiableObject)9 Test (org.junit.jupiter.api.Test)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GeoJsonObject (org.geojson.GeoJsonObject)1 Polygon (org.geojson.Polygon)1 JsonNode (org.hisp.dhis.jsontree.JsonNode)1 JsonAttributeValue (org.hisp.dhis.webapi.json.domain.JsonAttributeValue)1