@Test public void shouldHandleNullSequenceNumber() { // When: final CommandStatusEntity entity = new CommandStatusEntity(STATEMENT_TEXT, COMMAND_ID, COMMAND_STATUS, null); // Then: assertThat(entity.getCommandSequenceNumber(), is(-1L)); }
@Override public Table buildTable(final CommandStatusEntity entity) { return new Table.Builder() .withColumnHeaders("Message") .withRow(entity.getCommandStatus().getMessage().split("\n", 2)[0]) .build(); } }
@Test public void shouldSetProperty() { // Given: final String csas = "CREATE STREAM " + streamName + " AS SELECT * FROM test_stream;"; // When: final List<CommandStatusEntity> results = makeMultipleRequest( "SET '" + KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY + "' = '2';\n" + csas, CommandStatusEntity.class); // Then: verify(commandStore).enqueueCommand(eq(csas), any(), any(), eq(ImmutableMap.of(KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY, "2"))); assertThat(results, hasSize(1)); assertThat(results.get(0).getStatementText(), is(csas)); }
@Test public void shouldInstantRegisterTopic() { // When: final CommandStatusEntity result = makeSingleRequest( "REGISTER TOPIC FOO WITH (kafka_topic='bar', value_format='json');", CommandStatusEntity.class); // Then: assertThat(result, is(new CommandStatusEntity( "REGISTER TOPIC FOO WITH (kafka_topic='bar', value_format='json');", commandStatus.getCommandId(), commandStatus.getStatus(), 0L))); }
private static void runStatement(final String statement, final KsqlRestClient restClient) { final RestResponse response = restClient.makeKsqlRequest(statement); Assert.assertThat(response.isSuccessful(), is(true)); final KsqlEntityList entityList = ((KsqlEntityList) response.get()); Assert.assertThat(entityList.size(), equalTo(1)); Assert.assertThat(entityList.get(0), instanceOf(CommandStatusEntity.class)); final CommandStatusEntity entity = (CommandStatusEntity) entityList.get(0); final CommandStatus status = entity.getCommandStatus(); Assert.assertThat(status, not(CommandStatus.Status.ERROR)); if (status.getStatus() != Status.SUCCESS) { assertThatEventually( "", () -> { final RestResponse statusResponse = restClient .makeStatusRequest(entity.getCommandId().toString()); Assert.assertThat(statusResponse.isSuccessful(), is(true)); Assert.assertThat(statusResponse.get(), instanceOf(CommandStatus.class)); return ((CommandStatus) statusResponse.get()).getStatus(); }, anyOf( is(CommandStatus.Status.SUCCESS), is(CommandStatus.Status.TERMINATED), is(CommandStatus.Status.ERROR)), 120, TimeUnit.SECONDS ); } }
@Test public void shouldReturnStatusEntityFromPersistentQuery() { // When: final CommandStatusEntity result = makeSingleRequest( "CREATE STREAM S AS SELECT * FROM test_stream;", CommandStatusEntity.class); // Then: assertThat(result, is(new CommandStatusEntity( "CREATE STREAM S AS SELECT * FROM test_stream;", commandStatus.getCommandId(), commandStatus.getStatus(), 0L))); }
@Test public void shouldThrowOnNullCommandId() { // Given: expectedException.expect(NullPointerException.class); expectedException.expectMessage("commandId"); // When: new CommandStatusEntity(STATEMENT_TEXT, null, COMMAND_STATUS, COMMAND_SEQUENCE_NUMBER); }
private void printKsqlResponse(final RestResponse<KsqlEntityList> response) throws IOException { if (response.isSuccessful()) { final KsqlEntityList ksqlEntities = response.getResponse(); boolean noErrorFromServer = true; for (final KsqlEntity entity : ksqlEntities) { if (entity instanceof CommandStatusEntity && ( ((CommandStatusEntity) entity).getCommandStatus().getStatus() == CommandStatus.Status.ERROR) ) { final String fullMessage = ((CommandStatusEntity) entity).getCommandStatus().getMessage(); terminal.printError(fullMessage.split("\n")[0], fullMessage); noErrorFromServer = false; } } if (noErrorFromServer) { terminal.printKsqlEntityList(response.getResponse()); } } else { terminal.printErrorMessage(response.getErrorMessage()); } }
@Test public void shouldUnsetProperty() { // Given: final String csas = "CREATE STREAM " + streamName + " AS SELECT * FROM test_stream;"; final Map<String, Object> localOverrides = ImmutableMap.of( KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY, "2" ); // When: final CommandStatusEntity result = makeSingleRequest( new KsqlRequest("UNSET '" + KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY + "';\n" + csas, localOverrides, null), CommandStatusEntity.class); // Then: verify(commandStore).enqueueCommand(eq(csas), any(), any(), eq(Collections.emptyMap())); assertThat(result.getStatementText(), is(csas)); }
@Test public void shouldThrowOnNullCommandStatus() { // Given: expectedException.expect(NullPointerException.class); expectedException.expectMessage("commandStatus"); // When: new CommandStatusEntity(STATEMENT_TEXT, COMMAND_ID, null, COMMAND_SEQUENCE_NUMBER); } }
@Test public void shoudlHandleTerminateRequestCorrectly() throws InterruptedException { // When: final Response response = ksqlResource.terminateCluster(VALID_TERMINATE_REQUEST); // Then: assertThat(response.getStatus(), equalTo(200)); assertThat(response.getEntity(), instanceOf(KsqlEntityList.class)); assertThat(((KsqlEntityList) response.getEntity()).size(), equalTo(1)); assertThat(((KsqlEntityList) response.getEntity()).get(0), instanceOf(CommandStatusEntity.class)); final CommandStatusEntity commandStatusEntity = (CommandStatusEntity) ((KsqlEntityList) response.getEntity()).get(0); assertThat(commandStatusEntity.getCommandStatus().getStatus(), equalTo(CommandStatus.Status.QUEUED)); verify(commandStore).enqueueCommand( eq(TerminateCluster.TERMINATE_CLUSTER_STATEMENT_TEXT), isA(Statement.class), any(), eq(Collections.singletonMap( ClusterTerminateRequest.DELETE_TOPIC_LIST_PROP, ImmutableList.of("Foo")))); }
@Test public void shouldDistributeTerminateQueryWithoutValidation() { // Why? Because currently if the server receives a single request containing two statements: // `CREATE STREAM FOO AS blah;` // `TERMINATE csas_foo_0;` // Then its possible that the terminate line is valid, in that it will terminate the query // started by the first line, but the server can no validate this as the CSAS may not have // be actioned by the StatementExecutor yet. // Given: final String terminateSql = "TERMINATE some_id;"; // When: final CommandStatusEntity result = makeSingleRequest(terminateSql, CommandStatusEntity.class); // Then: verify(commandStore).enqueueCommand(eq(terminateSql), isA(TerminateQuery.class), any(), any()); assertThat(result.getStatementText(), is(terminateSql)); }
private CommandStatusEntity distributeStatement( final PreparedStatement<?> statement, final Map<String, Object> propertyOverrides ) { try { final PreparedStatement<?> withSchema = addInferredSchema(statement); final QueuedCommandStatus queuedCommandStatus = commandQueue.enqueueCommand( withSchema.getStatementText(), withSchema.getStatement(), ksqlConfig, propertyOverrides); final CommandStatus commandStatus = queuedCommandStatus .tryWaitForFinalStatus(distributedCmdResponseTimeout); return new CommandStatusEntity( withSchema.getStatementText(), queuedCommandStatus.getCommandId(), commandStatus, queuedCommandStatus.getCommandSequenceNumber() ); } catch (final Exception e) { throw new KsqlException(String.format( "Could not write the statement '%s' into the command " + "topic.", statement.getStatementText()), e); } }
@Test public void shouldDistributeTerminateQuery() { // Given: final PersistentQueryMetadata queryMetadata = createQuery( "CREATE STREAM test_explain AS SELECT * FROM test_stream;", Collections.emptyMap()); final String terminateSql = "TERMINATE " + queryMetadata.getQueryId() + ";"; // When: final CommandStatusEntity result = makeSingleRequest(terminateSql, CommandStatusEntity.class); // Then: verify(commandStore).enqueueCommand(eq(terminateSql), isA(TerminateQuery.class), any(), any()); assertThat(result.getStatementText(), is(terminateSql)); }
new CommandStatusEntity( "e", CommandId.fromString("topic/1/create"),