Search in sources :

Example 96 with JsonObject

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

the class BackendTests method testTodoScenario.

@Test
void testTodoScenario() {
    String basicAuth = "Basic " + Base64.getEncoder().encodeToString("john:password".getBytes());
    JsonObject todo = Json.createObjectBuilder().add("title", "todo title").build();
    // Add a new todo
    JsonObject returnedTodo = webTarget.path("/api/backend").request(MediaType.APPLICATION_JSON_TYPE).header(Http.Header.AUTHORIZATION, basicAuth).post(Entity.json(todo), JsonObject.class);
    assertEquals("john", returnedTodo.getString("user"));
    assertEquals(todo.getString("title"), returnedTodo.getString("title"));
    // Get the todo created earlier
    JsonObject fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id")).request(MediaType.APPLICATION_JSON_TYPE).header(Http.Header.AUTHORIZATION, basicAuth).get(JsonObject.class);
    assertEquals(returnedTodo, fromServer);
    // Update the todo created earlier
    JsonObject updatedTodo = Json.createObjectBuilder().add("title", "updated title").add("completed", false).build();
    fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id")).request(MediaType.APPLICATION_JSON_TYPE).header(Http.Header.AUTHORIZATION, basicAuth).put(Entity.json(updatedTodo), JsonObject.class);
    assertEquals(updatedTodo.getString("title"), fromServer.getString("title"));
    // Delete the todo created earlier
    fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id")).request(MediaType.APPLICATION_JSON_TYPE).header(Http.Header.AUTHORIZATION, basicAuth).delete(JsonObject.class);
    assertEquals(returnedTodo.getString("id"), fromServer.getString("id"));
    // Get list of todos
    JsonArray jsonValues = webTarget.path("/api/backend").request(MediaType.APPLICATION_JSON_TYPE).header(Http.Header.AUTHORIZATION, basicAuth).get(JsonArray.class);
    assertEquals(0, jsonValues.size(), "There should be no todos on server");
}
Also used : JsonArray(jakarta.json.JsonArray) JsonObject(jakarta.json.JsonObject) HelidonTest(io.helidon.microprofile.tests.junit5.HelidonTest) Test(org.junit.jupiter.api.Test)

Example 97 with JsonObject

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

the class HealthSupport method callHealthChecks.

HealthResponse callHealthChecks(List<HealthCheck> healthChecks) {
    List<HcResponse> responses = healthChecks.stream().map(this::callHealthChecks).filter(this::notExcluded).filter(this::allOrIncluded).sorted(Comparator.comparing(HcResponse::name)).collect(Collectors.toList());
    Status status = responses.stream().map(HcResponse::status).filter(Status.DOWN::equals).findFirst().orElse(Status.UP);
    Http.ResponseStatus httpStatus = responses.stream().filter(HcResponse::internalError).findFirst().map(it -> Http.Status.INTERNAL_SERVER_ERROR_500).orElse((status == Status.UP) ? Http.Status.OK_200 : Http.Status.SERVICE_UNAVAILABLE_503);
    JsonObject json = toJson(status, responses);
    return new HealthResponse(httpStatus, json);
}
Also used : Status(org.eclipse.microprofile.health.HealthCheckResponse.Status) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) HelidonRestServiceSupport(io.helidon.servicecommon.rest.HelidonRestServiceSupport) Level(java.util.logging.Level) HashSet(java.util.HashSet) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonStructure(jakarta.json.JsonStructure) HealthCheckResponse(org.eclipse.microprofile.health.HealthCheckResponse) Duration(java.time.Duration) Map(java.util.Map) ServerResponse(io.helidon.webserver.ServerResponse) JsonObject(jakarta.json.JsonObject) Single(io.helidon.common.reactive.Single) LinkedList(java.util.LinkedList) Http(io.helidon.common.http.Http) ConfiguredOption(io.helidon.config.metadata.ConfiguredOption) HealthCheck(org.eclipse.microprofile.health.HealthCheck) Timeout(io.helidon.faulttolerance.Timeout) MessageBodyWriter(io.helidon.media.common.MessageBodyWriter) Config(io.helidon.config.Config) Collection(java.util.Collection) Configured(io.helidon.config.metadata.Configured) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) Set(java.util.Set) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) ServerRequest(io.helidon.webserver.ServerRequest) Json(jakarta.json.Json) Status(org.eclipse.microprofile.health.HealthCheckResponse.Status) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) List(java.util.List) Optional(java.util.Optional) Comparator(java.util.Comparator) Async(io.helidon.faulttolerance.Async) Routing(io.helidon.webserver.Routing) Collections(java.util.Collections) JsonObject(jakarta.json.JsonObject) Http(io.helidon.common.http.Http)

Example 98 with JsonObject

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

the class ConfigTest method bothPass.

@Test
void bothPass() throws InterruptedException, ExecutionException, TimeoutException {
    JsonObject health = runWithConfig("bothPass", Http.Status.OK_200.code());
    JsonObject diskSpace = getLivenessCheck(health, "diskSpace");
    assertThat("Disk space liveness return data", diskSpace, is(notNullValue()));
    assertThat("Disk space liveness check status", diskSpace.getString("status"), is("UP"));
    JsonObject heapMemory = getLivenessCheck(health, "heapMemory");
    assertThat("Heap memory liveness return data", heapMemory, is(notNullValue()));
    assertThat("Heap memory liveness check status", heapMemory.getString("status"), is("UP"));
}
Also used : JsonObject(jakarta.json.JsonObject) Test(org.junit.jupiter.api.Test)

Example 99 with JsonObject

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

the class ConfigTest method runWithConfig.

private JsonObject runWithConfig(String configKey, int expectedStatus) throws InterruptedException, ExecutionException, TimeoutException {
    HealthSupport healthSupport = HealthSupport.builder().addLiveness(HealthChecks.healthChecks(testConfig.get(configKey + ".helidon.health"))).build();
    WebServer webServer = null;
    try {
        webServer = startServer(healthSupport);
        WebClientResponse response = webClientBuilder(webServer).build().get().accept(MediaType.APPLICATION_JSON).path("health/live").submit().await();
        assertThat("Normal health URL HTTP response", response.status().code(), is(expectedStatus));
        return response.content().as(JsonObject.class).await();
    } finally {
        if (webServer != null) {
            shutdownServer(webServer);
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) WebServer(io.helidon.webserver.WebServer) HealthSupport(io.helidon.health.HealthSupport) JsonObject(jakarta.json.JsonObject)

Example 100 with JsonObject

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

the class WeldFeature method weldProxyConfigurations.

static List<WeldProxyConfig> weldProxyConfigurations(DuringSetupAccess access) {
    try {
        ClassLoader classLoader = access.findClassByName("io.helidon.config.Config").getClassLoader();
        Enumeration<URL> resources = classLoader.getResources("META-INF/helidon/native-image/weld-proxies.json");
        JsonReaderFactory readerFactory = Json.createReaderFactory(Map.of());
        List<WeldProxyConfig> weldProxies = new ArrayList<>();
        while (resources.hasMoreElements()) {
            URL url = resources.nextElement();
            JsonArray proxies;
            try {
                proxies = readerFactory.createReader(url.openStream()).readArray();
            } catch (JsonParsingException e) {
                throw new NativeImageException("Failed to read JSON config: " + url, e);
            }
            proxies.forEach(jsonValue -> {
                weldProxies.add(new WeldProxyConfig((JsonObject) jsonValue));
            });
        }
        return weldProxies;
    } catch (IOException e) {
        throw new IllegalStateException("Failed to get resources", e);
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(jakarta.json.JsonObject) IOException(java.io.IOException) URL(java.net.URL) JsonArray(jakarta.json.JsonArray) JsonReaderFactory(jakarta.json.JsonReaderFactory) JsonParsingException(jakarta.json.stream.JsonParsingException)

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