Search in sources :

Example 1 with DbClient

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

the class DbClientHealthCheckTest method testBuilderNamedQueryFromConfig.

/**
 * Test health check builder with named query statement
 */
@Test
void testBuilderNamedQueryFromConfig() {
    final Config config = Config.create(ConfigSources.classpath("testBuilderFromConfig.yaml"));
    final DbClient dbClient = new DbClientMock();
    final Config healthConfig = config.get("health-check-query-named");
    final DbClientHealthCheck.Builder builder = DbClientHealthCheck.builder(dbClient).config(healthConfig);
    // Config file value
    final String configName = healthConfig.get("name").as(String.class).get();
    final String configType = healthConfig.get("type").as(String.class).get();
    // final String configStatement = healthConfig.get("statement").as(String.class).get();
    final String configStatementName = healthConfig.get("statementName").as(String.class).get();
    final Long configTimeout = healthConfig.get("timeout").as(Long.class).get();
    final String configTimeUnit = healthConfig.get("timeUnit").as(String.class).get();
    // Builder instance content
    final String name = builder.name();
    final boolean isDML = builder.isDML();
    final String statement = builder.statement();
    final String statementName = builder.statementName();
    final boolean isNamedstatement = builder.isNamedstatement();
    final long timeoutDuration = builder.timeoutDuration();
    final TimeUnit timeoutUnit = builder.timeoutUnit();
    // Verify builder internal state
    assertThat(name, equalTo(configName));
    assertThat(isDML, equalTo(false));
    assertThat(statement, nullValue());
    assertThat(statementName, equalTo(configStatementName));
    assertThat(isNamedstatement, equalTo(true));
    assertThat(timeoutDuration, equalTo(configTimeout));
    assertThat(timeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
    DbClientHealthCheck check = builder.build();
    // Health check instance content
    final String checkName = check.name();
    final long checkTimeoutDuration = check.timeoutDuration();
    final TimeUnit checkTimeoutUnit = check.timeoutUnit();
    // Verify health check class and internal content
    assertThat(check.getClass().getSimpleName(), equalTo("DbClientHealthCheckAsNamedQuery"));
    assertThat(checkName, equalTo(configName));
    assertThat(((DbClientHealthCheck.DbClientHealthCheckAsNamedQuery) check).statementName(), equalTo(configStatementName));
    assertThat(checkTimeoutDuration, equalTo(configTimeout));
    assertThat(checkTimeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test)

Example 2 with DbClient

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

the class DbClientHealthCheckTest method testBuilderQueryFromConfig.

/**
 * Test health check builder with custom query statement
 */
@Test
void testBuilderQueryFromConfig() {
    final Config config = Config.create(ConfigSources.classpath("testBuilderFromConfig.yaml"));
    final DbClient dbClient = new DbClientMock();
    final Config healthConfig = config.get("health-check-query");
    final DbClientHealthCheck.Builder builder = DbClientHealthCheck.builder(dbClient).config(healthConfig);
    // Config file value
    final String configName = healthConfig.get("name").as(String.class).get();
    final String configType = healthConfig.get("type").as(String.class).get();
    final String configStatement = healthConfig.get("statement").as(String.class).get();
    // final String configStatementName = healthConfig.get("statementName").as(String.class).get();
    final Long configTimeout = healthConfig.get("timeout").as(Long.class).get();
    final String configTimeUnit = healthConfig.get("timeUnit").as(String.class).get();
    // Builder instance content
    final String name = builder.name();
    final boolean isDML = builder.isDML();
    final String statement = builder.statement();
    final String statementName = builder.statementName();
    final boolean isNamedstatement = builder.isNamedstatement();
    final long timeoutDuration = builder.timeoutDuration();
    final TimeUnit timeoutUnit = builder.timeoutUnit();
    // Verify builder internal state
    assertThat(name, equalTo(configName));
    assertThat(isDML, equalTo(false));
    assertThat(statement, equalTo(configStatement));
    assertThat(statementName, nullValue());
    assertThat(isNamedstatement, equalTo(false));
    assertThat(timeoutDuration, equalTo(configTimeout));
    assertThat(timeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
    DbClientHealthCheck check = builder.build();
    // Health check instance content
    final String checkName = check.name();
    final long checkTimeoutDuration = check.timeoutDuration();
    final TimeUnit checkTimeoutUnit = check.timeoutUnit();
    // Verify health check class and internal content
    assertThat(check.getClass().getSimpleName(), equalTo("DbClientHealthCheckAsQuery"));
    assertThat(checkName, equalTo(configName));
    assertThat(((DbClientHealthCheck.DbClientHealthCheckAsQuery) check).statement(), equalTo(configStatement));
    assertThat(checkTimeoutDuration, equalTo(configTimeout));
    assertThat(checkTimeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test)

Example 3 with DbClient

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

the class DbClientHealthCheckTest method testBuilderNamedDmlFromConfig.

/**
 * Test health check builder with named DML statement
 */
@Test
void testBuilderNamedDmlFromConfig() {
    final Config config = Config.create(ConfigSources.classpath("testBuilderFromConfig.yaml"));
    final DbClient dbClient = new DbClientMock();
    final Config healthConfig = config.get("health-check-dml-named");
    final DbClientHealthCheck.Builder builder = DbClientHealthCheck.builder(dbClient).config(healthConfig);
    // Config file value
    final String configName = healthConfig.get("name").as(String.class).get();
    final String configType = healthConfig.get("type").as(String.class).get();
    final String configStatementName = healthConfig.get("statementName").as(String.class).get();
    final Long configTimeout = healthConfig.get("timeout").as(Long.class).get();
    final String configTimeUnit = healthConfig.get("timeUnit").as(String.class).get();
    // Builder instance content
    final String name = builder.name();
    final boolean isDML = builder.isDML();
    final String statement = builder.statement();
    final String statementName = builder.statementName();
    final boolean isNamedstatement = builder.isNamedstatement();
    final long timeoutDuration = builder.timeoutDuration();
    final TimeUnit timeoutUnit = builder.timeoutUnit();
    // Verify builder internal state
    assertThat(name, equalTo(configName));
    assertThat(isDML, equalTo(true));
    assertThat(statement, nullValue());
    assertThat(statementName, equalTo(configStatementName));
    assertThat(isNamedstatement, equalTo(true));
    assertThat(timeoutDuration, equalTo(configTimeout));
    assertThat(timeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
    DbClientHealthCheck check = builder.build();
    assertThat(check.getClass().getSimpleName(), equalTo("DbClientHealthCheckAsNamedDml"));
    // Health check instance content
    final String checkName = check.name();
    final long checkTimeoutDuration = check.timeoutDuration();
    final TimeUnit checkTimeoutUnit = check.timeoutUnit();
    // Verify health check class and internal content
    assertThat(check.getClass().getSimpleName(), equalTo("DbClientHealthCheckAsNamedDml"));
    assertThat(checkName, equalTo(configName));
    assertThat(((DbClientHealthCheck.DbClientHealthCheckAsNamedDml) check).statementName(), equalTo(configStatementName));
    assertThat(checkTimeoutDuration, equalTo(configTimeout));
    assertThat(checkTimeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test)

Example 4 with DbClient

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

the class DbClientHealthCheckTest method testBuilderDmlFromConfig.

/**
 * Test health check builder with custom DML statement
 */
@Test
void testBuilderDmlFromConfig() {
    final Config config = Config.create(ConfigSources.classpath("testBuilderFromConfig.yaml"));
    final DbClient dbClient = new DbClientMock();
    final Config healthConfig = config.get("health-check-dml");
    final DbClientHealthCheck.Builder builder = DbClientHealthCheck.builder(dbClient).config(healthConfig);
    // Config file value
    final String configName = healthConfig.get("name").as(String.class).get();
    final String configStatement = healthConfig.get("statement").as(String.class).get();
    final Long configTimeout = healthConfig.get("timeout").as(Long.class).get();
    final String configTimeUnit = healthConfig.get("timeUnit").as(String.class).get();
    // Builder instance content
    final String name = builder.name();
    final boolean isDML = builder.isDML();
    final String statement = builder.statement();
    final String statementName = builder.statementName();
    final boolean isNamedstatement = builder.isNamedstatement();
    final long timeoutDuration = builder.timeoutDuration();
    final TimeUnit timeoutUnit = builder.timeoutUnit();
    // Verify builder internal content
    assertThat(name, equalTo(configName));
    assertThat(isDML, equalTo(true));
    assertThat(statement, equalTo(configStatement));
    assertThat(statementName, nullValue());
    assertThat(isNamedstatement, equalTo(false));
    assertThat(timeoutDuration, equalTo(configTimeout));
    assertThat(timeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
    DbClientHealthCheck check = builder.build();
    // Health check instance content
    final String checkName = check.name();
    final long checkTimeoutDuration = check.timeoutDuration();
    final TimeUnit checkTimeoutUnit = check.timeoutUnit();
    // Verify health check class and internal content
    assertThat(check.getClass().getSimpleName(), equalTo("DbClientHealthCheckAsDml"));
    assertThat(checkName, equalTo(configName));
    assertThat(((DbClientHealthCheck.DbClientHealthCheckAsDml) check).statement(), equalTo(configStatement));
    assertThat(checkTimeoutDuration, equalTo(configTimeout));
    assertThat(checkTimeoutUnit.name().toLowerCase(), equalTo(configTimeUnit.toLowerCase()));
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test)

Example 5 with DbClient

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

the class PokemonMain method createRouting.

/**
 * Creates new {@link io.helidon.webserver.Routing}.
 *
 * @param config configuration of this server
 * @return routing configured with JSON support, a health check, and a service
 */
private static Routing createRouting(Config config) {
    Config dbConfig = config.get("db");
    // Client services are added through a service loader - see mongoDB example for explicit services
    DbClient dbClient = DbClient.builder(dbConfig).build();
    // Some relational databases do not support DML statement as ping so using query which works for all of them
    HealthSupport health = HealthSupport.builder().addLiveness(DbClientHealthCheck.create(dbClient, dbConfig.get("health-check"))).build();
    // Initialize database schema
    InitializeDb.init(dbClient);
    return Routing.builder().register(// Health at "/health"
    health).register(// Metrics at "/metrics"
    MetricsSupport.create()).register("/db", new PokemonService(dbClient)).build();
}
Also used : DbClient(io.helidon.dbclient.DbClient) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport)

Aggregations

DbClient (io.helidon.dbclient.DbClient)10 Config (io.helidon.config.Config)9 LogConfig (io.helidon.common.LogConfig)5 Test (org.junit.jupiter.api.Test)5 HealthSupport (io.helidon.health.HealthSupport)4 TimeUnit (java.util.concurrent.TimeUnit)4 Routing (io.helidon.webserver.Routing)2 WebServer (io.helidon.webserver.WebServer)2 DbClientService (io.helidon.dbclient.DbClientService)1 DbRow (io.helidon.dbclient.DbRow)1 HealthCheckService (io.helidon.tests.integration.dbclient.appl.health.HealthCheckService)1 InterceptorService (io.helidon.tests.integration.dbclient.appl.interceptor.InterceptorService)1 MapperService (io.helidon.tests.integration.dbclient.appl.mapping.MapperService)1 FlowControlService (io.helidon.tests.integration.dbclient.appl.result.FlowControlService)1 SimpleDeleteService (io.helidon.tests.integration.dbclient.appl.simple.SimpleDeleteService)1 SimpleGetService (io.helidon.tests.integration.dbclient.appl.simple.SimpleGetService)1 SimpleInsertService (io.helidon.tests.integration.dbclient.appl.simple.SimpleInsertService)1 SimpleQueryService (io.helidon.tests.integration.dbclient.appl.simple.SimpleQueryService)1 SimpleUpdateService (io.helidon.tests.integration.dbclient.appl.simple.SimpleUpdateService)1 DmlStatementService (io.helidon.tests.integration.dbclient.appl.statement.DmlStatementService)1