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;
}
}
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;
}
}
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());
}
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());
}
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());
}
Aggregations