Search in sources :

Example 1 with Pokemon

use of io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon in project helidon by oracle.

the class Utils method verifyPokemonsIdRange.

/**
 * Verify that {@code Multi<DbRow> rows} argument contains pokemons matching specified IDs range.
 * @param rows database query result to verify
 * @param idMin beginning of ID range
 * @param idMax end of ID range
 */
public static void verifyPokemonsIdRange(Multi<DbRow> rows, int idMin, int idMax) {
    assertThat(rows, notNullValue());
    List<DbRow> rowsList = rows.collectList().await();
    // Build Map of valid pokemons
    Map<Integer, Pokemon> valid = new HashMap<>(POKEMONS.size());
    for (Map.Entry<Integer, Pokemon> entry : POKEMONS.entrySet()) {
        int id = entry.getKey();
        Pokemon pokemon = entry.getValue();
        if (id > idMin && id < idMax) {
            valid.put(id, pokemon);
        }
    }
    // assertThat(rowsList, hasSize(valid.size()));
    for (DbRow row : rowsList) {
        Integer id = row.column(1).as(Integer.class);
        String name = row.column(2).as(String.class);
        LOGGER.info(() -> String.format("Pokemon id=%d, name=%s", id, name));
        assertThat(valid.containsKey(id), equalTo(true));
        assertThat(name, equalTo(valid.get(id).getName()));
    }
}
Also used : DbRow(io.helidon.dbclient.DbRow) HashMap(java.util.HashMap) Pokemon(io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Pokemon

use of io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon 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 3 with Pokemon

use of io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon in project helidon by oracle.

the class Utils method verifyUpdatePokemon.

/**
 * Verify that provided pokemon was successfully updated in the database.
 *
 * @param result DML statement result
 * @param pokemon pokemon to compare with
 */
public static void verifyUpdatePokemon(Long result, AbstractIT.Pokemon pokemon) {
    assertThat(result, equalTo(1L));
    Optional<DbRow> maybeRow = DB_CLIENT.execute(exec -> exec.namedGet("select-pokemon-by-id", pokemon.getId())).await();
    assertThat(maybeRow.isPresent(), equalTo(true));
    DbRow row = maybeRow.get();
    Integer id = row.column(1).as(Integer.class);
    String name = row.column(2).as(String.class);
    assertThat(id, equalTo(pokemon.getId()));
    assertThat(name, pokemon.getName().equals(name));
}
Also used : DbRow(io.helidon.dbclient.DbRow) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Pokemon(io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) DB_CLIENT(io.helidon.tests.integration.dbclient.common.AbstractIT.DB_CLIENT) List(java.util.List) DbRow(io.helidon.dbclient.DbRow) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) POKEMONS(io.helidon.tests.integration.dbclient.common.AbstractIT.POKEMONS) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AbstractIT(io.helidon.tests.integration.dbclient.common.AbstractIT) Multi(io.helidon.common.reactive.Multi)

Example 4 with Pokemon

use of io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon in project helidon by oracle.

the class Utils method verifyInsertPokemon.

/**
 * Verify that provided pokemon was successfully inserted into the database.
 *
 * @param result DML statement result
 * @param pokemon pokemon to compare with
 */
public static void verifyInsertPokemon(Long result, AbstractIT.Pokemon pokemon) {
    assertThat(result, equalTo(1L));
    Optional<DbRow> maybeRow = DB_CLIENT.execute(exec -> exec.namedGet("select-pokemon-by-id", pokemon.getId())).await();
    assertThat(maybeRow.isPresent(), equalTo(true));
    DbRow row = maybeRow.get();
    Integer id = row.column("id").as(Integer.class);
    String name = row.column("name").as(String.class);
    assertThat(id, equalTo(pokemon.getId()));
    assertThat(name, pokemon.getName().equals(name));
}
Also used : DbRow(io.helidon.dbclient.DbRow) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Pokemon(io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) DB_CLIENT(io.helidon.tests.integration.dbclient.common.AbstractIT.DB_CLIENT) List(java.util.List) DbRow(io.helidon.dbclient.DbRow) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) POKEMONS(io.helidon.tests.integration.dbclient.common.AbstractIT.POKEMONS) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AbstractIT(io.helidon.tests.integration.dbclient.common.AbstractIT) Multi(io.helidon.common.reactive.Multi)

Example 5 with Pokemon

use of io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon in project helidon by oracle.

the class Utils method verifyPokemonsIdRange.

/**
 * Verify that {@code Multi<DbRow> rows} argument contains single pokemon matching specified IDs range.
 * @param maybeRow database query result to verify
 * @param idMin beginning of ID range
 * @param idMax end of ID range
 */
public static void verifyPokemonsIdRange(Optional<DbRow> maybeRow, int idMin, int idMax) {
    assertThat(maybeRow.isPresent(), equalTo(true));
    DbRow row = maybeRow.get();
    // Build Map of valid pokemons
    Map<Integer, Pokemon> valid = new HashMap<>(POKEMONS.size());
    for (Map.Entry<Integer, Pokemon> entry : POKEMONS.entrySet()) {
        int id = entry.getKey();
        Pokemon pokemon = entry.getValue();
        if (id > idMin && id < idMax) {
            valid.put(id, pokemon);
        }
    }
    Integer id = row.column(1).as(Integer.class);
    String name = row.column(2).as(String.class);
    assertThat(valid.containsKey(id), equalTo(true));
    assertThat(name, equalTo(valid.get(id).getName()));
}
Also used : DbRow(io.helidon.dbclient.DbRow) HashMap(java.util.HashMap) Pokemon(io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DbRow (io.helidon.dbclient.DbRow)5 Pokemon (io.helidon.tests.integration.dbclient.common.AbstractIT.Pokemon)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Multi (io.helidon.common.reactive.Multi)3 AbstractIT (io.helidon.tests.integration.dbclient.common.AbstractIT)3 List (java.util.List)3 Logger (java.util.logging.Logger)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Matchers.equalTo (org.hamcrest.Matchers.equalTo)3 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)3 DB_CLIENT (io.helidon.tests.integration.dbclient.common.AbstractIT.DB_CLIENT)2 POKEMONS (io.helidon.tests.integration.dbclient.common.AbstractIT.POKEMONS)2 Optional (java.util.Optional)2 Matchers.hasSize (org.hamcrest.Matchers.hasSize)2 Config (io.helidon.config.Config)1 DbClient (io.helidon.dbclient.DbClient)1 DbStatementType (io.helidon.dbclient.DbStatementType)1 DbClientMetrics (io.helidon.dbclient.metrics.DbClientMetrics)1 MetricsSupport (io.helidon.metrics.MetricsSupport)1