Search in sources :

Example 91 with JsonObject

use of jakarta.json.JsonObject in project helidon by oracle.

the class GreetService method processErrors.

private static <T> T processErrors(Throwable ex, ServerRequest request, ServerResponse response) {
    if (ex.getCause() instanceof JsonException) {
        LOGGER.log(Level.FINE, "Invalid JSON", ex);
        JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "Invalid JSON").build();
        response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
    } else {
        LOGGER.log(Level.FINE, "Internal error", ex);
        JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "Internal error").build();
        response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send(jsonErrorObject);
    }
    return null;
}
Also used : JsonException(jakarta.json.JsonException) JsonObject(jakarta.json.JsonObject)

Example 92 with JsonObject

use of jakarta.json.JsonObject in project helidon by oracle.

the class GreetService method updateGreetingFromJson.

private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
    if (!jo.containsKey("greeting")) {
        JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "No greeting provided").build();
        response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
        return;
    }
    greeting.set(jo.getString("greeting"));
    response.status(Http.Status.NO_CONTENT_204).send();
}
Also used : JsonObject(jakarta.json.JsonObject)

Example 93 with JsonObject

use of jakarta.json.JsonObject in project helidon by oracle.

the class FileServiceTest method testList.

@Test
@Order(3)
public void testList() {
    WebClientResponse response = webClient.get().contentType(MediaType.APPLICATION_JSON).request().await();
    assertThat(response.status().code(), Matchers.is(200));
    JsonObject json = response.content().as(JsonObject.class).await();
    assertThat(json, Matchers.is(notNullValue()));
    List<String> files = json.getJsonArray("files").getValuesAs(v -> ((JsonString) v).getString());
    assertThat(files, hasItem("foo.txt"));
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) JsonObject(jakarta.json.JsonObject) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) JsonString(jakarta.json.JsonString) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 94 with JsonObject

use of jakarta.json.JsonObject in project helidon by oracle.

the class ImplicitHelloWorldTest method testJsonResource.

@Test
void testJsonResource() {
    JsonObject jsonObject = target.path("/helloworld/unit").request().get(JsonObject.class);
    assertAll("JSON fields must match expected injection values", () -> assertThat("Name from request", jsonObject.getString("name"), is("unit")), () -> assertThat("Request id from CDI provider", jsonObject.getInt("requestId"), is(1)), () -> assertThat("App name from config", jsonObject.getString("appName"), is("Hello World Application")), () -> assertThat("Logger name", jsonObject.getString("logger"), is(HelloWorldResource.class.getName())));
}
Also used : JsonObject(jakarta.json.JsonObject) Test(org.junit.jupiter.api.Test) HelidonTest(io.helidon.microprofile.tests.junit5.HelidonTest)

Example 95 with JsonObject

use of jakarta.json.JsonObject in project helidon by oracle.

the class GreetService method updateGreetingFromJson.

private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
    if (!jo.containsKey("greeting")) {
        JsonObject jsonErrorObject = JSON.createObjectBuilder().add("error", "No greeting provided").build();
        response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
        return;
    }
    greeting.set(jo.getString("greeting"));
    response.status(Http.Status.NO_CONTENT_204).send();
}
Also used : JsonObject(jakarta.json.JsonObject)

Aggregations

JsonObject (jakarta.json.JsonObject)356 Test (org.junit.Test)125 Test (org.junit.jupiter.api.Test)92 JsonArray (jakarta.json.JsonArray)29 Response (jakarta.ws.rs.core.Response)29 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)24 JsonString (jakarta.json.JsonString)24 JsonValue (jakarta.json.JsonValue)20 WebClientResponse (io.helidon.webclient.WebClientResponse)19 Client (jakarta.ws.rs.client.Client)18 StringReader (java.io.StringReader)17 HelidonTest (io.helidon.microprofile.tests.junit5.HelidonTest)15 JsonReader (jakarta.json.JsonReader)14 List (java.util.List)14 JsonException (jakarta.json.JsonException)13 Map (java.util.Map)13 Logger (java.util.logging.Logger)13 Routing (io.helidon.webserver.Routing)11 Json (jakarta.json.Json)11 Produces (jakarta.ws.rs.Produces)11