Search in sources :

Example 11 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ApplyMigrationCommandTest method shouldApplyDefineUndefineCommands.

@SuppressWarnings("unchecked")
@Test
public void shouldApplyDefineUndefineCommands() throws Exception {
    // Given:
    final Map<String, Object> variables = ImmutableMap.of("pre", "a", "str", "abc");
    command = PARSER.parse("-n");
    createMigrationFile(1, NAME, migrationsDir, DEFINE_COMMANDS);
    when(versionQueryResult.get()).thenReturn(ImmutableList.of());
    when(ksqlClient.getVariables()).thenReturn(ImmutableMap.of(), ImmutableMap.of(), variables, variables, variables, variables, variables, variables, variables, variables, variables, variables, variables, variables, variables, variables, variables, ImmutableMap.of());
    // When:
    final int result = command.command(config, (cfg, headers) -> ksqlClient, migrationsDir, Clock.fixed(Instant.ofEpochMilli(1000), ZoneId.systemDefault()));
    // Then:
    assertThat(result, is(0));
    final InOrder inOrder = inOrder(ksqlClient);
    verifyMigratedVersion(inOrder, 1, "<none>", MigrationState.MIGRATED, () -> {
        inOrder.verify(ksqlClient).executeStatement(COMMAND, new HashMap<>());
        inOrder.verify(ksqlClient).define("pre", "a");
        inOrder.verify(ksqlClient).define("str", "abc");
        inOrder.verify(ksqlClient).executeStatement(eq("CREATE STREAM ${str} AS SELECT * FROM FOO;"), propCaptor.capture());
        assertThat(propCaptor.getValue().size(), is(1));
        assertThat(propCaptor.getValue().get("abc"), is("yay"));
        inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "abc")));
        inOrder.verify(ksqlClient).undefine("str");
        inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "${str}")));
    });
    inOrder.verify(ksqlClient).close();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) KsqlObject(io.confluent.ksql.api.client.KsqlObject) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Test(org.junit.Test)

Example 12 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class RowImplTest method shouldGetAsObject.

@Test
public void shouldGetAsObject() {
    // When
    final KsqlObject obj = row.asObject();
    // Then
    assertThat(obj.getString("f_str"), is("foo"));
    assertThat(obj.getInteger("f_int"), is(2));
    assertThat(obj.getLong("f_long"), is(1234L));
    assertThat(obj.getDouble("f_double"), is(34.43));
    assertThat(obj.getBoolean("f_bool"), is(false));
    assertThat(obj.getDecimal("f_decimal"), is(new BigDecimal("12.21")));
    assertThat(obj.getBytes("f_bytes"), is(new byte[] { 0, 1, 2 }));
    assertThat(obj.getKsqlArray("f_array"), is(new KsqlArray(ImmutableList.of("e1", "e2"))));
    assertThat(obj.getKsqlObject("f_map"), is(new KsqlObject(ImmutableMap.of("k1", "v1", "k2", "v2"))));
    assertThat(obj.getKsqlObject("f_struct"), is(new KsqlObject(ImmutableMap.of("f1", "baz", "f2", 12))));
    assertThat(obj.getString("f_timestamp"), is("2020-01-01T04:40:34.789"));
    assertThat(obj.getString("f_date"), is("2020-01-01"));
    assertThat(obj.getString("f_time"), is("04:40:34.789"));
}
Also used : BigDecimal(java.math.BigDecimal) KsqlArray(io.confluent.ksql.api.client.KsqlArray) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Test(org.junit.Test)

Example 13 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientMutationIntegrationTest method shouldInsertIntoTable.

@Test
public void shouldInsertIntoTable() throws Exception {
    // Given
    final Map<String, Object> properties = new HashMap<>();
    properties.put("auto.offset.reset", "earliest");
    final KsqlObject insertRow = new KsqlObject().put("K", "my_key").put("LONG", 11L);
    // When
    final String query = "SELECT * from " + TEST_TABLE + " WHERE K='my_key' EMIT CHANGES LIMIT 1;";
    StreamedQueryResult queryResult = client.streamQuery(query, properties).get();
    final Row row = assertThatEventually(() -> {
        // Potentially try inserting multiple times, in case the query wasn't started by the first time
        try {
            client.insertInto(TEST_TABLE, insertRow).get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return queryResult.poll(Duration.ofMillis(10));
    }, is(notNullValue()));
    // Then: a newly inserted row arrives
    assertThat(row.getString("K"), is("my_key"));
    assertThat(row.getLong("LONG"), is(11L));
}
Also used : HashMap(java.util.HashMap) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(io.confluent.ksql.api.client.Row) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecutionException(java.util.concurrent.ExecutionException) KsqlObject(io.confluent.ksql.api.client.KsqlObject) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 14 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ClientMutationIntegrationTest method shouldExecuteQueryWithProperties.

@Test
public void shouldExecuteQueryWithProperties() {
    // Given
    final Map<String, Object> properties = new HashMap<>();
    properties.put("auto.offset.reset", "latest");
    final String sql = "SELECT * FROM " + TEST_STREAM + " EMIT CHANGES LIMIT 1;";
    final KsqlObject insertRow = new KsqlObject().put("K", new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldExecuteQueryWithProperties"))).put("STR", "Value_shouldExecuteQueryWithProperties").put("LONG", 2000L).put("DEC", new BigDecimal("12.34")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v1_shouldExecuteQueryWithProperties").add("v2_shouldExecuteQueryWithProperties")).put("MAP", new KsqlObject().put("test_name", "shouldExecuteQueryWithProperties")).put("STRUCT", new KsqlObject().put("F1", 4)).put("COMPLEX", COMPLEX_FIELD_VALUE).put("TIMESTAMP", "1970-01-01T00:00:00.001").put("DATE", "1970-01-01").put("TIME", "00:00:00");
    // When
    final BatchedQueryResult queryResult = client.executeQuery(sql, properties);
    // Then: a newly inserted row arrives
    // Wait for row to arrive
    final AtomicReference<Row> rowRef = new AtomicReference<>();
    new Thread(() -> {
        try {
            final List<Row> rows = queryResult.get();
            assertThat(rows, hasSize(1));
            rowRef.set(rows.get(0));
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }).start();
    // Insert a new row
    final Row row = assertThatEventually(() -> {
        // Potentially try inserting multiple times, in case the query wasn't started by the first time
        try {
            client.insertInto(TEST_STREAM, insertRow).get();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
        return rowRef.get();
    }, is(notNullValue()));
    // Verify received row
    assertThat(row.getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldExecuteQueryWithProperties"))));
    assertThat(row.getString("STR"), is("Value_shouldExecuteQueryWithProperties"));
    assertThat(row.getLong("LONG"), is(2000L));
    assertThat(row.getDecimal("DEC"), is(new BigDecimal("12.34")));
    assertThat(row.getBytes("BYTES_"), is(new byte[] { 0, 1, 2 }));
    assertThat(row.getKsqlArray("ARRAY"), is(new KsqlArray().add("v1_shouldExecuteQueryWithProperties").add("v2_shouldExecuteQueryWithProperties")));
    assertThat(row.getKsqlObject("MAP"), is(new KsqlObject().put("test_name", "shouldExecuteQueryWithProperties")));
    assertThat(row.getKsqlObject("STRUCT"), is(new KsqlObject().put("F1", 4)));
    assertThat(row.getKsqlObject("COMPLEX"), is(EXPECTED_COMPLEX_FIELD_VALUE));
    assertThat(row.getString("TIMESTAMP"), is("1970-01-01T00:00:00.001"));
    assertThat(row.getString("DATE"), is("1970-01-01"));
    assertThat(row.getString("TIME"), is("00:00"));
}
Also used : HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) BigDecimal(java.math.BigDecimal) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecutionException(java.util.concurrent.ExecutionException) KsqlObject(io.confluent.ksql.api.client.KsqlObject) KsqlObject(io.confluent.ksql.api.client.KsqlObject) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) List(java.util.List) Row(io.confluent.ksql.api.client.Row) KsqlArray(io.confluent.ksql.api.client.KsqlArray) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 15 with KsqlObject

use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.

the class ApplyMigrationCommandTest method shouldApplyArgumentVariablesEveryMigration.

@Test
public void shouldApplyArgumentVariablesEveryMigration() throws Exception {
    // Given:
    command = PARSER.parse("-a", "-d", "name=tame", "-d", "dame=blame");
    createMigrationFile(1, NAME, migrationsDir, "INSERT INTO FOO VALUES ('${name}');");
    createMigrationFile(2, NAME, migrationsDir, "INSERT INTO FOO VALUES ('${dame}');");
    when(versionQueryResult.get()).thenReturn(ImmutableList.of());
    when(ksqlClient.getVariables()).thenReturn(ImmutableMap.of("name", "tame", "dame", "blame"));
    givenAppliedMigration(1, NAME, MigrationState.MIGRATED);
    // When:
    final int result = command.command(config, (cfg, headers) -> ksqlClient, migrationsDir, Clock.fixed(Instant.ofEpochMilli(1000), ZoneId.systemDefault()));
    // Then:
    assertThat(result, is(0));
    final InOrder inOrder = inOrder(ksqlClient);
    inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "tame")));
    inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "blame")));
    inOrder.verify(ksqlClient).close();
    inOrder.verifyNoMoreInteractions();
}
Also used : InOrder(org.mockito.InOrder) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Test(org.junit.Test)

Aggregations

KsqlObject (io.confluent.ksql.api.client.KsqlObject)16 Test (org.junit.Test)12 KsqlArray (io.confluent.ksql.api.client.KsqlArray)8 IntegrationTest (io.confluent.common.utils.IntegrationTest)6 KsqlClientException (io.confluent.ksql.api.client.exception.KsqlClientException)6 Row (io.confluent.ksql.api.client.Row)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 InOrder (org.mockito.InOrder)5 StreamedQueryResult (io.confluent.ksql.api.client.StreamedQueryResult)4 JsonArray (io.vertx.core.json.JsonArray)4 JsonObject (io.vertx.core.json.JsonObject)4 AcksPublisher (io.confluent.ksql.api.client.AcksPublisher)3 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Client (io.confluent.ksql.api.client.Client)2 ClientOptions (io.confluent.ksql.api.client.ClientOptions)2 ConnectorDescription (io.confluent.ksql.api.client.ConnectorDescription)2 ConnectorInfo (io.confluent.ksql.api.client.ConnectorInfo)2