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