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