use of io.vertx.core.json.JsonArray in project hono by eclipse.
the class TenantObjectTest method testDeserializationOfCustomConfigProperties.
/**
* Verifies that a JSON string containing custom configuration
* properties can be deserialized into a {@code TenantObject}.
*
* @throws Exception if the JSON cannot be deserialized.
*/
@Test
public void testDeserializationOfCustomConfigProperties() throws Exception {
final JsonObject deploymentValue = new JsonObject().put("maxInstances", 4);
final JsonObject adapterConfig = new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, "custom").put(TenantConstants.FIELD_ENABLED, true).put("deployment", deploymentValue);
final JsonObject config = new JsonObject().put(TenantConstants.FIELD_PAYLOAD_TENANT_ID, "my-tenant").put(TenantConstants.FIELD_ENABLED, true).put("plan", "gold").put(TenantConstants.FIELD_ADAPTERS, new JsonArray().add(adapterConfig));
final String jsonString = config.encode();
final TenantObject obj = mapper.readValue(jsonString, TenantObject.class);
assertNotNull(obj);
assertThat(obj.getProperty("plan"), is("gold"));
final JsonObject customAdapterConfig = obj.getAdapterConfiguration("custom");
assertNotNull(customAdapterConfig);
assertThat(customAdapterConfig.getJsonObject("deployment"), is(deploymentValue));
}
use of io.vertx.core.json.JsonArray in project vertx-examples by vert-x3.
the class Transaction method start.
@Override
public void start() throws Exception {
JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver");
String sql = "CREATE TABLE colors (" + "id INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 1, INCREMENT BY 1) PRIMARY KEY, " + "name VARCHAR(255), " + "datetime TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL)";
JDBCClient client = JDBCClient.createShared(vertx, config);
// Connect to the database
client.rxGetConnection().flatMap(conn -> conn.rxSetAutoCommit(false).flatMap(autoCommit -> conn.rxExecute(sql)).flatMap(executed -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("BLACK"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("WHITE"))).flatMap(updateResult -> conn.rxUpdateWithParams("INSERT INTO colors (name) VALUES (?)", new JsonArray().add("PURPLE"))).flatMap(updateResult -> conn.rxCommit().map(commit -> updateResult)).onErrorResumeNext(ex -> conn.rxRollback().onErrorResumeNext(ex2 -> Single.error(new CompositeException(ex, ex2))).flatMap(ignore -> Single.error(ex))).flatMap(updateResult -> conn.rxQuery("SELECT * FROM colors")).doAfterTerminate(conn::close)).subscribe(resultSet -> {
// Subscribe to get the final result
System.out.println("Results : " + resultSet.getRows());
}, Throwable::printStackTrace);
}
use of io.vertx.core.json.JsonArray in project vertx-examples by vert-x3.
the class VertxWebApplication method handleListProducts.
private void handleListProducts(RoutingContext routingContext) {
JsonArray arr = new JsonArray();
products.forEach((k, v) -> arr.add(v));
routingContext.response().putHeader("content-type", "application/json").end(arr.encodePrettily());
}
use of io.vertx.core.json.JsonArray in project vertx-examples by vert-x3.
the class JDBCExample method start.
@Override
public void start() throws Exception {
final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
client.getConnection(conn -> {
if (conn.failed()) {
System.err.println(conn.cause().getMessage());
return;
}
final SQLConnection connection = conn.result();
connection.execute("create table test(id int primary key, name varchar(255))", res -> {
if (res.failed()) {
throw new RuntimeException(res.cause());
}
// insert some test data
connection.execute("insert into test values(1, 'Hello')", insert -> {
// query some data
connection.query("select * from test", rs -> {
for (JsonArray line : rs.result().getResults()) {
System.out.println(line.encode());
}
// and close the connection
connection.close(done -> {
if (done.failed()) {
throw new RuntimeException(done.cause());
}
});
});
});
});
});
}
use of io.vertx.core.json.JsonArray in project vertx-examples by vert-x3.
the class JDBCExample method start.
@Override
public void start() throws Exception {
final JDBCClient client = JDBCClient.createShared(vertx, new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true").put("driver_class", "org.hsqldb.jdbcDriver").put("max_pool_size", 30).put("user", "SA").put("password", ""));
client.getConnection(conn -> {
if (conn.failed()) {
System.err.println(conn.cause().getMessage());
return;
}
// create a test table
execute(conn.result(), "create table test(id int primary key, name varchar(255))", create -> {
// start a transaction
startTx(conn.result(), beginTrans -> {
// insert some test data
execute(conn.result(), "insert into test values(1, 'Hello')", insert -> {
// commit data
rollbackTx(conn.result(), rollbackTrans -> {
// query some data
query(conn.result(), "select count(*) from test", rs -> {
for (JsonArray line : rs.getResults()) {
System.out.println(line.encode());
}
// and close the connection
conn.result().close(done -> {
if (done.failed()) {
throw new RuntimeException(done.cause());
}
});
});
});
});
});
});
});
}
Aggregations