Search in sources :

Example 11 with DbRow

use of io.helidon.dbclient.DbRow in project helidon by oracle.

the class ServerMetricsCheckIT method testHttpMetrics.

/**
 * Read and check DB Client metrics from Helidon Web Server.
 *
 * @throws InterruptedException if the current thread was interrupted
 * @throws IOException if an I/O error occurs when sending or receiving HTTP request
 */
@Test
public void testHttpMetrics() throws IOException, InterruptedException {
    // Call select-pokemons to trigger it
    Multi<DbRow> rows = DB_CLIENT.execute(exec -> exec.namedQuery("select-pokemons"));
    List<DbRow> pokemonList = rows.collectList().await();
    // Call insert-pokemon to trigger it
    Pokemon pokemon = new Pokemon(BASE_ID + 1, "Lickitung", TYPES.get(1));
    Long result = DB_CLIENT.execute(exec -> exec.namedInsert("insert-pokemon", pokemon.getId(), pokemon.getName())).await();
    // Read and process metrics response
    String response = get(URL + "/metrics/application");
    LOGGER.info("RESPONSE: " + response);
    JsonObject application = null;
    try (JsonReader jr = Json.createReader(new StringReader(response))) {
        application = jr.readObject();
    } catch (JsonParsingException | IllegalStateException ex) {
        fail(String.format("Error parsing response: %s", ex.getMessage()));
    }
    assertThat(application, notNullValue());
    assertThat(application.getValueType(), equalTo(JsonValue.ValueType.OBJECT));
    assertThat(application.size(), greaterThan(0));
    assertThat(application.containsKey("db.counter.select-pokemons"), equalTo(true));
    assertThat(application.containsKey("db.counter.insert-pokemon"), equalTo(true));
    int selectPokemons = application.getInt("db.counter.select-pokemons");
    int insertPokemons = application.getInt("db.counter.insert-pokemon");
    assertThat(selectPokemons, equalTo(1));
    assertThat(insertPokemons, equalTo(1));
    assertThat(application.containsKey("db.timer.insert-pokemon"), equalTo(true));
    JsonObject insertTimer = application.getJsonObject("db.timer.insert-pokemon");
    assertThat(insertTimer.containsKey("count"), equalTo(true));
    assertThat(insertTimer.containsKey("min"), equalTo(true));
    assertThat(insertTimer.containsKey("max"), equalTo(true));
    int timerCount = insertTimer.getInt("count");
    assertThat(timerCount, equalTo(1));
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) DbStatementType(io.helidon.dbclient.DbStatementType) JsonValue(jakarta.json.JsonValue) HttpRequest(java.net.http.HttpRequest) AfterAll(org.junit.jupiter.api.AfterAll) DbRow(io.helidon.dbclient.DbRow) BeforeAll(org.junit.jupiter.api.BeforeAll) JsonObject(jakarta.json.JsonObject) HttpClient(java.net.http.HttpClient) DbClientMetrics(io.helidon.dbclient.metrics.DbClientMetrics) TYPES(io.helidon.tests.integration.dbclient.common.AbstractIT.TYPES) URI(java.net.URI) JsonReader(jakarta.json.JsonReader) MetricsSupport(io.helidon.metrics.MetricsSupport) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DbClient(io.helidon.dbclient.DbClient) Multi(io.helidon.common.reactive.Multi) HttpResponse(java.net.http.HttpResponse) JsonParsingException(jakarta.json.stream.JsonParsingException) Config(io.helidon.config.Config) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Pokemon(io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon) IOException(java.io.IOException) Logger(java.util.logging.Logger) CONFIG(io.helidon.tests.integration.dbclient.common.AbstractIT.CONFIG) Json(jakarta.json.Json) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) StringReader(java.io.StringReader) LAST_POKEMON_ID(io.helidon.tests.integration.dbclient.common.AbstractIT.LAST_POKEMON_ID) Matchers.equalTo(org.hamcrest.Matchers.equalTo) WebServer(io.helidon.webserver.WebServer) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Routing(io.helidon.webserver.Routing) AbstractIT(io.helidon.tests.integration.dbclient.common.AbstractIT) JsonObject(jakarta.json.JsonObject) DbRow(io.helidon.dbclient.DbRow) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Pokemon(io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon) JsonParsingException(jakarta.json.stream.JsonParsingException) Test(org.junit.jupiter.api.Test)

Example 12 with DbRow

use of io.helidon.dbclient.DbRow in project helidon by oracle.

the class InterceptorService method testStatementInterceptor.

// Check that statement interceptor was called before statement execution.
private void testStatementInterceptor(final ServerRequest request, final ServerResponse response) {
    LOGGER.fine(() -> "Running Interceptor.testStatementInterceptor on server");
    Multi<DbRow> rows = dbClient().execute(exec -> exec.createNamedQuery("select-pokemon-named-arg").addParam("name", Pokemon.POKEMONS.get(6).getName()).execute());
    final JsonArrayBuilder jab = Json.createArrayBuilder();
    rows.forEach(row -> jab.add(row.as(JsonObject.class))).onComplete(() -> {
        if (((TestClientService) interceptor).called()) {
            response.send(AppResponse.okStatus(jab.build()));
        } else {
            response.send(exceptionStatus(new RemoteTestException("Interceptor service was not called")));
        }
    }).exceptionally(t -> {
        response.send(exceptionStatus(t));
        return null;
    });
}
Also used : DbRow(io.helidon.dbclient.DbRow) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 13 with DbRow

use of io.helidon.dbclient.DbRow in project helidon by oracle.

the class TransactionQueriesService method executeTest.

// Common test execution code
private void executeTest(final ServerRequest request, final ServerResponse response, final String testName, final TestFunction test) {
    LOGGER.fine(() -> String.format("Running SimpleQueryService.%s on server", testName));
    try {
        String name = param(request, QUERY_NAME_PARAM);
        Multi<DbRow> future = test.apply(name);
        final JsonArrayBuilder jab = Json.createArrayBuilder();
        future.forEach(dbRow -> jab.add(dbRow.as(JsonObject.class))).onComplete(() -> response.send(AppResponse.okStatus(jab.build()))).exceptionally(t -> {
            response.send(exceptionStatus(t));
            return null;
        });
    } catch (RemoteTestException ex) {
        LOGGER.fine(() -> String.format("Error in SimpleQueryService.%s on server", testName));
        response.send(exceptionStatus(ex));
    }
}
Also used : DbRow(io.helidon.dbclient.DbRow) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 14 with DbRow

use of io.helidon.dbclient.DbRow in project helidon by oracle.

the class SimpleQueryService method executeTest.

// Common test execution code
private void executeTest(final ServerRequest request, final ServerResponse response, final String testName, final TestFunction test) {
    LOGGER.fine(() -> String.format("Running SimpleQueryService.%s on server", testName));
    try {
        String name = param(request, QUERY_NAME_PARAM);
        Multi<DbRow> future = test.apply(name);
        final JsonArrayBuilder jab = Json.createArrayBuilder();
        future.forEach(dbRow -> jab.add(dbRow.as(JsonObject.class))).onComplete(() -> response.send(AppResponse.okStatus(jab.build()))).exceptionally(t -> {
            response.send(exceptionStatus(t));
            return null;
        });
    } catch (RemoteTestException ex) {
        LOGGER.fine(() -> String.format("Error in SimpleQueryService.%s on server", testName));
        response.send(exceptionStatus(ex));
    }
}
Also used : DbRow(io.helidon.dbclient.DbRow) JsonArrayBuilder(jakarta.json.JsonArrayBuilder) RemoteTestException(io.helidon.tests.integration.tools.service.RemoteTestException)

Example 15 with DbRow

use of io.helidon.dbclient.DbRow in project helidon by oracle.

the class InterceptorIT method testStatementInterceptor.

/**
 * Check that statement interceptor was called before statement execution.
 */
@Test
public void testStatementInterceptor() {
    TestClientService interceptor = new TestClientService();
    DbClient dbClient = initDbClient(interceptor);
    Multi<DbRow> rows = dbClient.execute(exec -> exec.createNamedQuery("select-pokemon-named-arg").addParam("name", POKEMONS.get(6).getName()).execute());
    assertThat(interceptor.called(), equalTo(true));
}
Also used : DbRow(io.helidon.dbclient.DbRow) DbClient(io.helidon.dbclient.DbClient) Test(org.junit.jupiter.api.Test)

Aggregations

DbRow (io.helidon.dbclient.DbRow)27 Test (org.junit.jupiter.api.Test)17 Multi (io.helidon.common.reactive.Multi)5 Pokemon (io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon)5 Logger (java.util.logging.Logger)5 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)5 Matchers.equalTo (org.hamcrest.Matchers.equalTo)5 AbstractIT (io.helidon.tests.integration.dbclient.common.AbstractIT)4 RangePoJo (io.helidon.tests.integration.dbclient.common.utils.RangePoJo)4 RemoteTestException (io.helidon.tests.integration.tools.service.RemoteTestException)4 JsonArrayBuilder (jakarta.json.JsonArrayBuilder)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 List (java.util.List)4 DB_CLIENT (io.helidon.tests.integration.dbclient.common.AbstractIT.DB_CLIENT)3 Map (java.util.Map)3 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)3 Config (io.helidon.config.Config)2 DbClient (io.helidon.dbclient.DbClient)2 CONFIG (io.helidon.tests.integration.dbclient.common.AbstractIT.CONFIG)2