Search in sources :

Example 96 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class HealthServerTest method checkResponse.

private static void checkResponse(Supplier<WebClientRequestBuilder> requestFactory, String requestPath, boolean expectContent) {
    WebClientResponse response = null;
    try {
        response = requestFactory.get().path(requestPath).accept(MediaType.APPLICATION_JSON).request().await();
        int expectedStatus = expectContent ? Http.Status.OK_200.code() : Http.Status.NO_CONTENT_204.code();
        assertThat("health response status", response.status().code(), is(expectedStatus));
        MessageBodyReadableContent content = response.content();
        String textContent = null;
        try {
            textContent = content.as(String.class).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
        assertThat("content length", textContent.length(), expectContent ? greaterThan(0) : equalTo(0));
        if (expectContent) {
            JsonObject health = Json.createReader(new StringReader(textContent)).readObject();
            assertThat("health status", health.getString("status"), is("UP"));
        }
    } finally {
        if (response != null) {
            response.close();
        }
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) StringReader(java.io.StringReader) JsonObject(jakarta.json.JsonObject) ExecutionException(java.util.concurrent.ExecutionException) MessageBodyReadableContent(io.helidon.media.common.MessageBodyReadableContent)

Example 97 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MainTest method runJsonFunctionalTest.

/**
 * Run some basic CRUD operations on the server. The server supports
 * running with any of our three JSON libraries: jsonp, jsonb, jackson.
 * So we set a system property to select the library to use before starting
 * the server
 *
 * @param edition     "mp", "se"
 * @param jsonLibrary "jsonp", "jsonb" or "jackson"
 * @throws Exception on test failure
 */
private void runJsonFunctionalTest(String edition, String jsonLibrary) throws Exception {
    JsonObject json = getBookAsJsonObject();
    int numberOfBooks = 1000;
    List<String> systemPropertyArgs = new LinkedList<>();
    systemPropertyArgs.add("-Dbookstore.size=" + numberOfBooks);
    if (jsonLibrary != null && !jsonLibrary.isEmpty()) {
        systemPropertyArgs.add("-Dapp.json-library=" + jsonLibrary);
    }
    HelidonApplication application = startTheApplication(editionToJarPath(edition), systemPropertyArgs);
    WebClient webClient = WebClient.builder().baseUri(application.getBaseUrl()).addMediaSupport(JsonpSupport.create()).build();
    webClient.get().path("/books").request(JsonArray.class).thenAccept(bookArray -> assertThat("Number of books", bookArray.size(), is(numberOfBooks))).toCompletableFuture().get();
    webClient.post().path("/books").submit(json).thenAccept(it -> assertThat("HTTP response POST", it.status(), is(Http.Status.OK_200))).thenCompose(it -> webClient.get().path("/books/123456").request(JsonObject.class)).thenAccept(it -> assertThat("Checking if correct ISBN", it.getString("isbn"), is("123456"))).toCompletableFuture().get();
    webClient.get().path("/books/0000").request().thenAccept(it -> assertThat("HTTP response GET bad ISBN", it.status(), is(Http.Status.NOT_FOUND_404))).toCompletableFuture().get();
    webClient.get().path("/books").request().thenApply(it -> {
        assertThat("HTTP response list books", it.status(), is(Http.Status.OK_200));
        return it;
    }).thenCompose(WebClientResponse::close).toCompletableFuture().get();
    webClient.delete().path("/books/123456").request().thenAccept(it -> assertThat("HTTP response delete book", it.status(), is(Http.Status.OK_200))).toCompletableFuture().get();
    application.stop();
}
Also used : JsonArray(jakarta.json.JsonArray) HttpURLConnection(java.net.HttpURLConnection) CoreMatchers.is(org.hamcrest.CoreMatchers.is) CapturingApplicationConsole(com.oracle.bedrock.runtime.console.CapturingApplicationConsole) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) URL(java.net.URL) CoreMatchers.startsWith(org.hamcrest.CoreMatchers.startsWith) Disabled(org.junit.jupiter.api.Disabled) ArrayList(java.util.ArrayList) MediaType(io.helidon.common.http.MediaType) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) Console(com.oracle.bedrock.runtime.options.Console) BeforeAll(org.junit.jupiter.api.BeforeAll) JsonObject(jakarta.json.JsonObject) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) Http(io.helidon.common.http.Http) Application(com.oracle.bedrock.runtime.Application) ValueSource(org.junit.jupiter.params.provider.ValueSource) LocalPlatform(com.oracle.bedrock.runtime.LocalPlatform) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Logger(java.util.logging.Logger) File(java.io.File) Arguments(com.oracle.bedrock.runtime.options.Arguments) Json(jakarta.json.Json) Test(org.junit.jupiter.api.Test) List(java.util.List) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Paths(java.nio.file.Paths) Assertions(org.junit.jupiter.api.Assertions) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Queue(java.util.Queue) Collections(java.util.Collections) JsonArray(jakarta.json.JsonArray) InputStream(java.io.InputStream) WebClientResponse(io.helidon.webclient.WebClientResponse) JsonObject(jakarta.json.JsonObject) WebClient(io.helidon.webclient.WebClient) LinkedList(java.util.LinkedList)

Example 98 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MetricsTest method testCounter.

@Test
public void testCounter() {
    WebClientService serviceCounterAll = WebClientMetrics.counter().nameFormat("counter.%1$s.%2$s").build();
    WebClientService serviceCounterGet = WebClientMetrics.counter().methods(Http.Method.GET).nameFormat("counter.get.%1$s.%2$s").build();
    WebClientService serviceCounterError = WebClientMetrics.counter().nameFormat("counter.error.%1$s.%2$s").success(false).build();
    WebClientService serviceCounterSuccess = WebClientMetrics.counter().nameFormat("counter.success.%1$s.%2$s").errors(false).build();
    WebClient webClient = createNewClient(serviceCounterAll, serviceCounterGet, serviceCounterError, serviceCounterSuccess);
    Counter counterAll = FACTORY.counter("counter.GET.localhost");
    Counter counterGet = FACTORY.counter("counter.get.GET.localhost");
    Counter counterError = FACTORY.counter("counter.error.GET.localhost");
    Counter counterSuccess = FACTORY.counter("counter.success.GET.localhost");
    assertThat(counterAll.getCount(), is(0L));
    assertThat(counterGet.getCount(), is(0L));
    assertThat(counterError.getCount(), is(0L));
    assertThat(counterSuccess.getCount(), is(0L));
    try {
        webClient.get().request(String.class).toCompletableFuture().get();
        assertThat(counterAll.getCount(), is(1L));
        assertThat(counterGet.getCount(), is(1L));
        assertThat(counterError.getCount(), is(0L));
        assertThat(counterSuccess.getCount(), is(1L));
        webClient.get().path("/error").request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
        assertThat(counterAll.getCount(), is(2L));
        assertThat(counterGet.getCount(), is(2L));
        assertThat(counterError.getCount(), is(1L));
        assertThat(counterSuccess.getCount(), is(1L));
    } catch (Exception e) {
        fail(e);
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Counter(org.eclipse.microprofile.metrics.Counter) WebClientService(io.helidon.webclient.spi.WebClientService) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 99 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MetricsTest method testMeter.

@Test
public void testMeter() {
    WebClientService serviceMeterAll = WebClientMetrics.meter().nameFormat("meter.%1$s.%2$s").build();
    WebClientService serviceMeterGet = WebClientMetrics.meter().methods(Http.Method.GET).nameFormat("meter.get.%1$s.%2$s").build();
    WebClientService serviceMeterError = WebClientMetrics.meter().nameFormat("meter.error.%1$s.%2$s").success(false).build();
    WebClientService serviceMeterSuccess = WebClientMetrics.meter().nameFormat("meter.success.%1$s.%2$s").errors(false).build();
    WebClient webClient = createNewClient(serviceMeterAll, serviceMeterGet, serviceMeterError, serviceMeterSuccess);
    Meter meterAll = FACTORY.meter("meter.GET.localhost");
    Meter meterGet = FACTORY.meter("meter.get.GET.localhost");
    Meter meterError = FACTORY.meter("meter.error.GET.localhost");
    Meter meterSuccess = FACTORY.meter("meter.success.GET.localhost");
    assertThat(meterAll.getCount(), is(0L));
    assertThat(meterGet.getCount(), is(0L));
    assertThat(meterError.getCount(), is(0L));
    assertThat(meterSuccess.getCount(), is(0L));
    try {
        webClient.get().request(String.class).toCompletableFuture().get();
        assertThat(meterAll.getCount(), is(1L));
        assertThat(meterGet.getCount(), is(1L));
        assertThat(meterError.getCount(), is(0L));
        assertThat(meterSuccess.getCount(), is(1L));
        webClient.get().path("/error").request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
        assertThat(meterAll.getCount(), is(2L));
        assertThat(meterGet.getCount(), is(2L));
        assertThat(meterError.getCount(), is(1L));
        assertThat(meterSuccess.getCount(), is(1L));
    } catch (Exception e) {
        fail(e);
    }
}
Also used : WebClientResponse(io.helidon.webclient.WebClientResponse) Meter(org.eclipse.microprofile.metrics.Meter) WebClientService(io.helidon.webclient.spi.WebClientService) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Example 100 with WebClientResponse

use of io.helidon.webclient.WebClientResponse in project helidon by oracle.

the class MetricsTest method testGaugeInProgress.

@Test
public void testGaugeInProgress() throws Exception {
    ConcurrentGauge progressAll = FACTORY.concurrentGauge("gauge.GET.localhost");
    ConcurrentGauge progressPut = FACTORY.concurrentGauge("gauge.put.PUT.localhost");
    ConcurrentGauge progressGet = FACTORY.concurrentGauge("gauge.get.GET.localhost");
    WebClientService inProgressAll = WebClientMetrics.gaugeInProgress().nameFormat("gauge.%1$s.%2$s").build();
    WebClientService inProgressPut = WebClientMetrics.gaugeInProgress().methods(Http.Method.PUT).nameFormat("gauge.put.%1$s.%2$s").build();
    WebClientService inProgressGet = WebClientMetrics.gaugeInProgress().methods(Http.Method.GET).nameFormat("gauge.get.%1$s.%2$s").build();
    WebClientService clientService = request -> {
        request.whenSent().thenAccept(clientServiceRequest -> {
            assertThat(progressAll.getCount(), is(1L));
            assertThat(progressGet.getCount(), is(1L));
            assertThat(progressPut.getCount(), is(0L));
        });
        return Single.just(request);
    };
    WebClient webClient = createNewClient(inProgressAll, inProgressPut, inProgressGet, clientService);
    assertThat(progressAll.getCount(), is(0L));
    assertThat(progressGet.getCount(), is(0L));
    assertThat(progressPut.getCount(), is(0L));
    webClient.get().request().thenCompose(WebClientResponse::close).toCompletableFuture().get();
    assertThat(progressAll.getCount(), is(0L));
    assertThat(progressGet.getCount(), is(0L));
    assertThat(progressPut.getCount(), is(0L));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) WebClient(io.helidon.webclient.WebClient) WebClientService(io.helidon.webclient.spi.WebClientService) WebClientResponse(io.helidon.webclient.WebClientResponse) Meter(org.eclipse.microprofile.metrics.Meter) Test(org.junit.jupiter.api.Test) ConcurrentGauge(org.eclipse.microprofile.metrics.ConcurrentGauge) Counter(org.eclipse.microprofile.metrics.Counter) Single(io.helidon.common.reactive.Single) Matchers.is(org.hamcrest.Matchers.is) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Http(io.helidon.common.http.Http) RegistryFactory(io.helidon.metrics.RegistryFactory) WebClientMetrics(io.helidon.webclient.metrics.WebClientMetrics) WebClientResponse(io.helidon.webclient.WebClientResponse) ConcurrentGauge(org.eclipse.microprofile.metrics.ConcurrentGauge) WebClientService(io.helidon.webclient.spi.WebClientService) WebClient(io.helidon.webclient.WebClient) Test(org.junit.jupiter.api.Test)

Aggregations

WebClientResponse (io.helidon.webclient.WebClientResponse)120 Test (org.junit.jupiter.api.Test)85 WebClientRequestBuilder (io.helidon.webclient.WebClientRequestBuilder)38 Headers (io.helidon.common.http.Headers)24 WebClient (io.helidon.webclient.WebClient)24 JsonObject (jakarta.json.JsonObject)22 Order (org.junit.jupiter.api.Order)16 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)16 Http (io.helidon.common.http.Http)15 DataChunk (io.helidon.common.http.DataChunk)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Single (io.helidon.common.reactive.Single)7 WebServer (io.helidon.webserver.WebServer)7 Logger (java.util.logging.Logger)7 Context (io.helidon.common.context.Context)6 MediaType (io.helidon.common.http.MediaType)6 URI (java.net.URI)6 Optional (java.util.Optional)6 Contexts (io.helidon.common.context.Contexts)5 Multi (io.helidon.common.reactive.Multi)5