Search in sources :

Example 56 with JsonArray

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));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 57 with JsonArray

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);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Single(rx.Single) CompositeException(rx.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) Runner(io.vertx.example.util.Runner) AbstractVerticle(io.vertx.rxjava.core.AbstractVerticle) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient) JsonArray(io.vertx.core.json.JsonArray) CompositeException(rx.exceptions.CompositeException) JsonObject(io.vertx.core.json.JsonObject) JDBCClient(io.vertx.rxjava.ext.jdbc.JDBCClient)

Example 58 with JsonArray

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());
}
Also used : JsonArray(io.vertx.core.json.JsonArray)

Example 59 with JsonArray

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());
                        }
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) SQLConnection(io.vertx.ext.sql.SQLConnection) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Example 60 with JsonArray

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());
                                }
                            });
                        });
                    });
                });
            });
        });
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JDBCClient(io.vertx.ext.jdbc.JDBCClient) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)535 JsonObject (io.vertx.core.json.JsonObject)379 Test (org.junit.Test)185 List (java.util.List)69 ArrayList (java.util.ArrayList)66 Map (java.util.Map)52 Handler (io.vertx.core.Handler)49 HashMap (java.util.HashMap)42 Collectors (java.util.stream.Collectors)42 Test (org.junit.jupiter.api.Test)41 Future (io.vertx.core.Future)37 IOException (java.io.IOException)35 AsyncResult (io.vertx.core.AsyncResult)34 Buffer (io.vertx.core.buffer.Buffer)33 HttpURLConnection (java.net.HttpURLConnection)30 StandardCharsets (java.nio.charset.StandardCharsets)29 RoutingContext (io.vertx.ext.web.RoutingContext)26 Objects (java.util.Objects)25 Instant (java.time.Instant)24 Rule (org.junit.Rule)22