Search in sources :

Example 1 with JsonResponse

use of org.hisp.dhis.jsontree.JsonResponse in project dhis2-core by dhis2.

the class H2SqlFunction method jsonb_extract_path_text.

// Postgres inbuilt function
public static String jsonb_extract_path_text(PGobject json, String... paths) {
    try {
        String content = json.getValue();
        if (content == null) {
            return null;
        }
        JsonValue value = new JsonResponse(content).get(toJsonPath(paths));
        if (!value.exists()) {
            return null;
        }
        if (value.node().getType() == JsonNodeType.STRING) {
            return value.as(JsonString.class).string();
        }
        return value.node().getDeclaration();
    } catch (Exception e) {
        log.error("Failed to extract path", e);
        throw e;
    }
}
Also used : JsonValue(org.hisp.dhis.jsontree.JsonValue) JsonString(org.hisp.dhis.jsontree.JsonString) JsonString(org.hisp.dhis.jsontree.JsonString) JsonResponse(org.hisp.dhis.jsontree.JsonResponse) SQLException(java.sql.SQLException)

Example 2 with JsonResponse

use of org.hisp.dhis.jsontree.JsonResponse in project dhis2-core by dhis2.

the class H2SqlFunction method jsonb_extract_path.

// Postgres inbuilt function
public static String jsonb_extract_path(PGobject json, String... paths) throws SQLException {
    try {
        String content = json.getValue();
        if (content == null) {
            return null;
        }
        JsonValue value = new JsonResponse(content).get(toJsonPath(paths));
        if (!value.exists()) {
            return null;
        }
        return value.node().getDeclaration();
    } catch (Exception e) {
        log.error("Failed to extract path", e);
        throw e;
    }
}
Also used : JsonValue(org.hisp.dhis.jsontree.JsonValue) JsonString(org.hisp.dhis.jsontree.JsonString) JsonResponse(org.hisp.dhis.jsontree.JsonResponse) SQLException(java.sql.SQLException)

Example 3 with JsonResponse

use of org.hisp.dhis.jsontree.JsonResponse in project dhis2-core by dhis2.

the class SqlViewControllerTest method testCreateWithDefaultValues.

@Test
public void testCreateWithDefaultValues() {
    String uid = assertStatus(HttpStatus.CREATED, POST("/sqlViews/", "{'name':'My SQL View','sqlQuery':'select 1 from userinfo'}"));
    JsonResponse sqlView = GET("/sqlViews/{uid}", uid).content();
    assertEquals("VIEW", sqlView.getString("type").string());
    assertEquals("RESPECT_SYSTEM_SETTING", sqlView.getString("cacheStrategy").string());
}
Also used : JsonResponse(org.hisp.dhis.jsontree.JsonResponse) Test(org.junit.jupiter.api.Test) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest)

Example 4 with JsonResponse

use of org.hisp.dhis.jsontree.JsonResponse in project dhis2-core by dhis2.

the class AbstractCrudControllerTest method testUpdateObject.

@Test
void testUpdateObject() {
    String peter = "{'name': 'Peter', 'firstName':'Peter', 'surname':'Pan', 'username':'peter47'}";
    String peterUserId = assertStatus(HttpStatus.CREATED, POST("/users", peter));
    JsonResponse roles = GET("/userRoles?fields=id").content();
    String roleId = roles.getArray("userRoles").getObject(0).getString("id").string();
    assertStatus(HttpStatus.NO_CONTENT, POST("/userRoles/" + roleId + "/users/" + peterUserId));
    JsonUser oldPeter = GET("/users/{id}", peterUserId).content().as(JsonUser.class);
    assertEquals("Peter", oldPeter.getFirstName());
    assertEquals(1, oldPeter.getArray("userRoles").size());
    assertStatus(HttpStatus.OK, PUT("/users/" + peterUserId, Body(oldPeter.getString("firstName").node().replaceWith("\"Fry\"").getDeclaration()), ContentType(MediaType.APPLICATION_JSON)));
    JsonUser newPeter = GET("/users/{id}", peterUserId).content().as(JsonUser.class);
    assertEquals("Fry", newPeter.getFirstName());
    // are user roles still there?
    assertEquals(1, newPeter.getArray("userRoles").size());
}
Also used : JsonUser(org.hisp.dhis.webapi.json.domain.JsonUser) JsonResponse(org.hisp.dhis.jsontree.JsonResponse) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Example 5 with JsonResponse

use of org.hisp.dhis.jsontree.JsonResponse in project dhis2-core by dhis2.

the class AbstractCrudControllerTest method replaceTranslationsOk.

@Test
public void replaceTranslationsOk() {
    String id = assertStatus(HttpStatus.CREATED, POST("/dataSets/", "{'name':'My data set', 'periodType':'Monthly'}"));
    JsonArray translations = GET("/dataSets/{id}/translations", id).content().getArray("translations");
    assertTrue(translations.isEmpty());
    PUT("/dataSets/" + id + "/translations", "{'translations': [{'locale':'sv', 'property':'name', 'value':'name sv'}]}").content(HttpStatus.NO_CONTENT);
    JsonResponse content = GET("/dataSets/{id}", id).content();
    translations = GET("/dataSets/{id}/translations", id).content().getArray("translations");
    assertEquals(1, translations.size());
    JsonTranslation translation = translations.get(0, JsonTranslation.class);
    assertEquals("sv", translation.getLocale());
    assertEquals("name", translation.getProperty());
    assertEquals("name sv", translation.getValue());
}
Also used : JsonArray(org.hisp.dhis.jsontree.JsonArray) JsonResponse(org.hisp.dhis.jsontree.JsonResponse) JsonTranslation(org.hisp.dhis.webapi.json.domain.JsonTranslation) DhisControllerConvenienceTest(org.hisp.dhis.webapi.DhisControllerConvenienceTest) Test(org.junit.jupiter.api.Test)

Aggregations

JsonResponse (org.hisp.dhis.jsontree.JsonResponse)21 DhisControllerConvenienceTest (org.hisp.dhis.webapi.DhisControllerConvenienceTest)18 Test (org.junit.jupiter.api.Test)18 Map (java.util.Map)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 JsonNode (org.hisp.dhis.jsontree.JsonNode)11 SQLException (java.sql.SQLException)2 JsonString (org.hisp.dhis.jsontree.JsonString)2 JsonValue (org.hisp.dhis.jsontree.JsonValue)2 JsonUser (org.hisp.dhis.webapi.json.domain.JsonUser)2 JsonArray (org.hisp.dhis.jsontree.JsonArray)1 JsonObject (org.hisp.dhis.jsontree.JsonObject)1 User (org.hisp.dhis.user.User)1 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)1 JsonFollowupValue (org.hisp.dhis.webapi.json.domain.JsonFollowupValue)1 JsonTranslation (org.hisp.dhis.webapi.json.domain.JsonTranslation)1