use of io.confluent.ksql.rest.EndpointResponse in project ksql by confluentinc.
the class TestEndpoints method executeKsqlRequest.
@Override
public synchronized CompletableFuture<EndpointResponse> executeKsqlRequest(final KsqlRequest request, final WorkerExecutor workerExecutor, final ApiSecurityContext apiSecurityContext) {
this.lastSql = request.getKsql();
this.lastProperties = new JsonObject(request.getConfigOverrides());
this.lastSessionVariables = new JsonObject(request.getSessionVariables());
this.lastApiSecurityContext = apiSecurityContext;
CompletableFuture<EndpointResponse> cf = new CompletableFuture<>();
if (executeKsqlRequestException != null) {
executeKsqlRequestException.fillInStackTrace();
cf.completeExceptionally(executeKsqlRequestException);
} else {
cf.complete(EndpointResponse.ok(ksqlEndpointResponse));
}
return cf;
}
use of io.confluent.ksql.rest.EndpointResponse in project ksql by confluentinc.
the class KsqlResourceTest method shouldThrowOnDenyListValidatorWhenHandleKsqlStatement.
@Test
public void shouldThrowOnDenyListValidatorWhenHandleKsqlStatement() {
// Given:
ksqlResource = new KsqlResource(ksqlEngine, commandRunner, DISTRIBUTED_COMMAND_RESPONSE_TIMEOUT, activenessRegistrar, (ec, sc) -> InjectorChain.of(schemaInjectorFactory.apply(sc), topicInjectorFactory.apply(ec), new TopicDeleteInjector(ec, sc)), Optional.of(authorizationValidator), errorsHandler, connectErrorHandler, denyListPropertyValidator, commandRunnerWarning);
final Map<String, Object> props = new HashMap<>(ksqlRestConfig.getKsqlConfigProperties());
props.put(KsqlConfig.KSQL_PROPERTIES_OVERRIDES_DENYLIST, StreamsConfig.NUM_STREAM_THREADS_CONFIG);
ksqlResource.configure(new KsqlConfig(props));
final Map<String, Object> overrides = ImmutableMap.of(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 1);
doThrow(new KsqlException("deny override")).when(denyListPropertyValidator).validateAll(overrides);
// When:
final EndpointResponse response = ksqlResource.handleKsqlStatements(securityContext, new KsqlRequest("query", // stream properties
overrides, emptyMap(), null));
// Then:
verify(denyListPropertyValidator).validateAll(overrides);
assertThat(response.getStatus(), CoreMatchers.is(BAD_REQUEST.code()));
assertThat(((KsqlErrorMessage) response.getEntity()).getMessage(), is("deny override"));
}
use of io.confluent.ksql.rest.EndpointResponse in project ksql by confluentinc.
the class KsqlResourceTest method shouldReturnBadRequestWhenIsValidatorIsCalledWithProhibitedProps.
@Test
public void shouldReturnBadRequestWhenIsValidatorIsCalledWithProhibitedProps() {
final Map<String, Object> properties = new HashMap<>();
properties.put("ksql.service.id", "");
// Given:
doThrow(new KsqlException("deny override")).when(denyListPropertyValidator).validateAll(properties);
// When:
final EndpointResponse response = ksqlResource.isValidProperty("ksql.service.id");
// Then:
assertThat(response.getStatus(), equalTo(400));
}
use of io.confluent.ksql.rest.EndpointResponse in project ksql by confluentinc.
the class KsqlResourceTest method setUp.
@Before
public void setUp() throws IOException, RestClientException {
commandStatus = new QueuedCommandStatus(0, new CommandStatusFuture(new CommandId(TOPIC, "whateva", CREATE)));
commandStatus1 = new QueuedCommandStatus(1, new CommandStatusFuture(new CommandId(TABLE, "something", DROP)));
final QueuedCommandStatus commandStatus2 = new QueuedCommandStatus(2, new CommandStatusFuture(new CommandId(STREAM, "something", EXECUTE)));
kafkaTopicClient = new FakeKafkaTopicClient();
kafkaConsumerGroupClient = new FakeKafkaConsumerGroupClient();
serviceContext = TestServiceContext.create(kafkaTopicClient, kafkaConsumerGroupClient);
schemaRegistryClient = serviceContext.getSchemaRegistryClient();
registerValueSchema(schemaRegistryClient);
ksqlRestConfig = new KsqlRestConfig(getDefaultKsqlConfig());
ksqlConfig = new KsqlConfig(ksqlRestConfig.getKsqlConfigProperties());
final KsqlExecutionContext.ExecuteResult result = mock(KsqlExecutionContext.ExecuteResult.class);
when(sandbox.execute(any(), any(ConfiguredKsqlPlan.class))).thenReturn(result);
when(result.getQuery()).thenReturn(Optional.empty());
MutableFunctionRegistry fnRegistry = new InternalFunctionRegistry();
final Metrics metrics = new Metrics();
UserFunctionLoader.newInstance(ksqlConfig, fnRegistry, ".", metrics).load();
metaStore = new MetaStoreImpl(fnRegistry);
final MetricCollectors metricCollectors = new MetricCollectors(metrics);
realEngine = KsqlEngineTestUtil.createKsqlEngine(serviceContext, metaStore, (engine) -> new KsqlEngineMetrics("", engine, Collections.emptyMap(), Optional.empty(), metricCollectors), new SequentialQueryIdGenerator(), ksqlConfig, metricCollectors);
securityContext = new KsqlSecurityContext(Optional.empty(), serviceContext);
when(commandRunner.getCommandQueue()).thenReturn(commandStore);
when(commandRunnerWarning.get()).thenReturn("");
when(commandStore.createTransactionalProducer()).thenReturn(transactionalProducer);
ksqlEngine = realEngine;
when(sandbox.getMetaStore()).thenAnswer(inv -> metaStore.copy());
addTestTopicAndSources();
when(commandStore.enqueueCommand(any(), any(), any(Producer.class))).thenReturn(commandStatus).thenReturn(commandStatus1).thenReturn(commandStatus2);
streamName = KsqlIdentifierTestUtil.uniqueIdentifierName();
when(schemaInjectorFactory.apply(any())).thenReturn(sandboxSchemaInjector);
when(schemaInjectorFactory.apply(serviceContext)).thenReturn(schemaInjector);
when(topicInjectorFactory.apply(any())).thenReturn(sandboxTopicInjector);
when(topicInjectorFactory.apply(ksqlEngine)).thenReturn(topicInjector);
when(sandboxSchemaInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(schemaInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(sandboxTopicInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(topicInjector.inject(any())).thenAnswer(inv -> inv.getArgument(0));
when(errorsHandler.generateResponse(any(), any())).thenAnswer(new Answer<EndpointResponse>() {
@Override
public EndpointResponse answer(final InvocationOnMock invocation) throws Throwable {
final Object[] args = invocation.getArguments();
return (EndpointResponse) args[1];
}
});
setUpKsqlResource();
}
use of io.confluent.ksql.rest.EndpointResponse in project ksql by confluentinc.
the class KsqlResourceTest method makeMultipleRequest.
private <T extends KsqlEntity> List<T> makeMultipleRequest(final KsqlRequest ksqlRequest, final Class<T> expectedEntityType) {
final EndpointResponse response = ksqlResource.handleKsqlStatements(securityContext, ksqlRequest);
if (response.getStatus() != OK.code()) {
throw new KsqlRestException(response);
}
final Object entity = response.getEntity();
assertThat(entity, instanceOf(KsqlEntityList.class));
final KsqlEntityList entityList = (KsqlEntityList) entity;
entityList.forEach(e -> assertThat(e, instanceOf(expectedEntityType)));
return entityList.stream().map(expectedEntityType::cast).collect(Collectors.toList());
}
Aggregations