Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DROP CONNECTOR functionality #3245

Merged
merged 1 commit into from
Aug 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add DROP CONNECTOR functionality
agavra committed Aug 21, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit cd0da39aeda49e6d0d9efa51ee5c9262a44ddcd0
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import io.confluent.ksql.cli.console.table.builder.CommandStatusTableBuilder;
import io.confluent.ksql.cli.console.table.builder.ConnectorInfoTableBuilder;
import io.confluent.ksql.cli.console.table.builder.ConnectorListTableBuilder;
import io.confluent.ksql.cli.console.table.builder.DropConnectorTableBuilder;
import io.confluent.ksql.cli.console.table.builder.ErrorEntityTableBuilder;
import io.confluent.ksql.cli.console.table.builder.ExecutionPlanTableBuilder;
import io.confluent.ksql.cli.console.table.builder.FunctionNameListTableBuilder;
@@ -45,6 +46,7 @@
import io.confluent.ksql.rest.entity.ConnectorDescription;
import io.confluent.ksql.rest.entity.ConnectorList;
import io.confluent.ksql.rest.entity.CreateConnectorEntity;
import io.confluent.ksql.rest.entity.DropConnectorEntity;
import io.confluent.ksql.rest.entity.ErrorEntity;
import io.confluent.ksql.rest.entity.ExecutionPlan;
import io.confluent.ksql.rest.entity.FieldInfo;
@@ -149,6 +151,8 @@ public class Console implements Closeable {
Console::printFunctionDescription)
.put(CreateConnectorEntity.class,
tablePrinter(CreateConnectorEntity.class, ConnectorInfoTableBuilder::new))
.put(DropConnectorEntity.class,
tablePrinter(DropConnectorEntity.class, DropConnectorTableBuilder::new))
.put(ConnectorList.class,
tablePrinter(ConnectorList.class, ConnectorListTableBuilder::new))
.put(ConnectorDescription.class,
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"; you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.cli.console.table.builder;

import io.confluent.ksql.cli.console.table.Table;
import io.confluent.ksql.rest.entity.DropConnectorEntity;

public class DropConnectorTableBuilder implements TableBuilder<DropConnectorEntity> {

@Override
public Table buildTable(final DropConnectorEntity entity) {
return new Table.Builder()
.withColumnHeaders("Message")
.withRow("Dropped connector \"" + entity.getConnectorName() + '"')
.build();
}
}
Original file line number Diff line number Diff line change
@@ -41,6 +41,7 @@
import io.confluent.ksql.rest.entity.CommandStatusEntity;
import io.confluent.ksql.rest.entity.ConnectorDescription;
import io.confluent.ksql.rest.entity.ConnectorList;
import io.confluent.ksql.rest.entity.DropConnectorEntity;
import io.confluent.ksql.rest.entity.EntityQueryId;
import io.confluent.ksql.rest.entity.ErrorEntity;
import io.confluent.ksql.rest.entity.ExecutionPlan;
@@ -1039,6 +1040,37 @@ public void shouldPrintWarnings() throws IOException {
}
}

@Test
public void shouldPrintDropConnector() throws IOException {
// Given:
final KsqlEntity entity = new DropConnectorEntity("statementText", "connectorName");

// When:
console.printKsqlEntityList(ImmutableList.of(entity));

// Then:
final String output = terminal.getOutputString();
if (console.getOutputFormat() == OutputFormat.TABULAR) {
assertThat(
output,
is("\n"
+ " Message \n"
+ "-----------------------------------\n"
+ " Dropped connector \"connectorName\" \n"
+ "-----------------------------------\n")
);
} else {
assertThat(
output,
is("[ {\n"
+ " \"statementText\" : \"statementText\",\n"
+ " \"connectorName\" : \"connectorName\",\n"
+ " \"warnings\" : [ ]\n"
+ "} ]\n")
);
}
}

@Test
public void shouldPrintErrorEntityLongNonJson() throws IOException {
// Given:
Original file line number Diff line number Diff line change
@@ -58,6 +58,13 @@ public interface ConnectClient {
*/
ConnectResponse<ConnectorStateInfo> status(String connector);

/**
* Delete the {@code connector}.
*
* @param connector the connector name
*/
ConnectResponse<String> delete(String connector);

/**
* An optionally successful response. Either contains a value of type
* {@code <T>} or an error, which is the string representation of the
@@ -68,11 +75,11 @@ class ConnectResponse<T> {
private final Optional<String> error;
private final int httpCode;

public static <T> ConnectResponse<T> of(final T datum, final int code) {
public static <T> ConnectResponse<T> success(final T datum, final int code) {
return new ConnectResponse<>(datum, null, code);
}

public static <T> ConnectResponse<T> of(final String error, final int code) {
public static <T> ConnectResponse<T> failure(final String error, final int code) {
return new ConnectResponse<>(null, error, code);
}

Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.fluent.Request;
@@ -176,6 +177,29 @@ public ConnectResponse<ConnectorInfo> describe(final String connector) {
}
}

@Override
public ConnectResponse<String> delete(final String connector) {
try {
LOG.debug("Issuing request to Kafka Connect at URI {} to delete {}",
connectUri, connector);

final ConnectResponse<String> connectResponse = withRetries(() -> Request
.Delete(connectUri.resolve(String.format("%s/%s", CONNECTORS, connector)))
.socketTimeout(DEFAULT_TIMEOUT_MS)
.connectTimeout(DEFAULT_TIMEOUT_MS)
.execute()
.handleResponse(
createHandler(HttpStatus.SC_NO_CONTENT, Object.class, foo -> connector)));

connectResponse.error()
.ifPresent(error -> LOG.warn("Could not delete connector: {}.", error));

return connectResponse;
} catch (final Exception e) {
throw new KsqlServerException(e);
}
}

@SuppressWarnings("unchecked")
private static <T> ConnectResponse<T> withRetries(final Callable<ConnectResponse<T>> action) {
try {
@@ -211,14 +235,17 @@ private static <T, C> ResponseHandler<ConnectResponse<T>> createHandler(
final int code = httpResponse.getStatusLine().getStatusCode();
if (httpResponse.getStatusLine().getStatusCode() != expectedStatus) {
final String entity = EntityUtils.toString(httpResponse.getEntity());
return ConnectResponse.of(entity, code);
return ConnectResponse.failure(entity, code);
}

final T info = cast.apply(MAPPER.readValue(
httpResponse.getEntity().getContent(),
entityClass));
final HttpEntity entity = httpResponse.getEntity();
final T data = cast.apply(
entity == null
? null
: MAPPER.readValue(entity.getContent(), entityClass)
);

return ConnectResponse.of(info, code);
return ConnectResponse.success(data, code);
};
}
}
Original file line number Diff line number Diff line change
@@ -34,13 +34,15 @@ private SandboxConnectClient() { }
public static ConnectClient createProxy() {
return LimitedProxyBuilder.forClass(ConnectClient.class)
.swallow("create", methodParams(String.class, Map.class),
ConnectResponse.of("sandbox", HttpStatus.SC_INTERNAL_SERVER_ERROR))
ConnectResponse.failure("sandbox", HttpStatus.SC_INTERNAL_SERVER_ERROR))
.swallow("describe", methodParams(String.class),
ConnectResponse.of("sandbox", HttpStatus.SC_INTERNAL_SERVER_ERROR))
ConnectResponse.failure("sandbox", HttpStatus.SC_INTERNAL_SERVER_ERROR))
.swallow("connectors", methodParams(),
ConnectResponse.of(ImmutableList.of(), HttpStatus.SC_OK))
ConnectResponse.success(ImmutableList.of(), HttpStatus.SC_OK))
.swallow("status", methodParams(String.class),
ConnectResponse.of("sandbox", HttpStatus.SC_INTERNAL_SERVER_ERROR))
ConnectResponse.failure("sandbox", HttpStatus.SC_INTERNAL_SERVER_ERROR))
.swallow("delete", methodParams(String.class),
ConnectResponse.success("sandbox", HttpStatus.SC_NO_CONTENT))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -228,14 +228,14 @@ public void shouldIgnoreConnectClientFailureToDescribe() throws InterruptedExcep

private void givenConnectors(final String... names){
for (final String name : names) {
when(connectClient.describe(name)).thenReturn(ConnectResponse.of(new ConnectorInfo(
when(connectClient.describe(name)).thenReturn(ConnectResponse.success(new ConnectorInfo(
name,
ImmutableMap.of(),
ImmutableList.of(),
ConnectorType.SOURCE
), HttpStatus.SC_CREATED));
}
when(connectClient.connectors()).thenReturn(ConnectResponse.of(ImmutableList.copyOf(names),
when(connectClient.connectors()).thenReturn(ConnectResponse.success(ImmutableList.copyOf(names),
HttpStatus.SC_OK));
}

Original file line number Diff line number Diff line change
@@ -169,6 +169,23 @@ public void testStatus() throws JsonProcessingException {
assertThat("Expected no error!", !response.error().isPresent());
}

@Test
public void testDelete() throws JsonProcessingException {
// Given:
WireMock.stubFor(
WireMock.delete(WireMock.urlEqualTo("/connectors/foo"))
.willReturn(WireMock.aResponse()
.withStatus(HttpStatus.SC_NO_CONTENT))
);

// When:
final ConnectResponse<String> response = client.delete("foo");

// Then:
assertThat(response.datum(), OptionalMatchers.of(is("foo")));
assertThat("Expected no error!", !response.error().isPresent());
}

@Test
public void testListShouldRetryOnFailure() throws JsonProcessingException {
// Given:
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ statement
| INSERT INTO qualifiedName (columns)? VALUES values #insertValues
| DROP STREAM (IF EXISTS)? qualifiedName (DELETE TOPIC)? #dropStream
| DROP TABLE (IF EXISTS)? qualifiedName (DELETE TOPIC)? #dropTable
| DROP CONNECTOR identifier #dropConnector
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have (IF EXISTS) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about this, but using an IF EXISTS will require an extra call to connect. If it doesn't exist you just get a 404 in the response, which is probably okay.

| EXPLAIN (statement | qualifiedName) #explain
| RUN SCRIPT STRING #runScript
;
Original file line number Diff line number Diff line change
@@ -56,6 +56,7 @@
import io.confluent.ksql.metastore.model.DataSource;
import io.confluent.ksql.parser.SqlBaseParser.CreateConnectorContext;
import io.confluent.ksql.parser.SqlBaseParser.DescribeConnectorContext;
import io.confluent.ksql.parser.SqlBaseParser.DropConnectorContext;
import io.confluent.ksql.parser.SqlBaseParser.InsertValuesContext;
import io.confluent.ksql.parser.SqlBaseParser.IntervalClauseContext;
import io.confluent.ksql.parser.SqlBaseParser.LimitClauseContext;
@@ -75,6 +76,7 @@
import io.confluent.ksql.parser.tree.CreateTableAsSelect;
import io.confluent.ksql.parser.tree.DescribeConnector;
import io.confluent.ksql.parser.tree.DescribeFunction;
import io.confluent.ksql.parser.tree.DropConnector;
import io.confluent.ksql.parser.tree.DropStream;
import io.confluent.ksql.parser.tree.DropTable;
import io.confluent.ksql.parser.tree.Explain;
@@ -350,6 +352,14 @@ public Node visitDropStream(final SqlBaseParser.DropStreamContext context) {
);
}

@Override
public Node visitDropConnector(final DropConnectorContext context) {
return new DropConnector(
getLocation(context),
ParserUtil.getIdentifierText(context.identifier())
);
}

@Override
public Query visitQuery(final SqlBaseParser.QueryContext context) {
final Relation from = (Relation) visit(context.from);
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"; you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.parser.tree;

import io.confluent.ksql.parser.NodeLocation;
import java.util.Objects;
import java.util.Optional;

public class DropConnector extends Statement {

private final String connectorName;

public DropConnector(final Optional<NodeLocation> location, final String connectorName) {
super(location);
this.connectorName = connectorName;
}

public String getConnectorName() {
return connectorName;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final DropConnector that = (DropConnector) o;
return Objects.equals(connectorName, that.connectorName);
}

@Override
public int hashCode() {
return Objects.hash(connectorName);
}

@Override
public String toString() {
return "DropConnector{"
+ "connectorName='" + connectorName + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"; you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.rest.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

@JsonIgnoreProperties(ignoreUnknown = true)
public class DropConnectorEntity extends KsqlEntity {

private final String connectorName;

public DropConnectorEntity(
@JsonProperty("statementText") final String statementText,
@JsonProperty("connectorName") final String connectorName
) {
super(statementText);
this.connectorName = Objects.requireNonNull(connectorName, "connectorName");
}

public String getConnectorName() {
return connectorName;
}

@Override
public String toString() {
return "DropConnectorEntity{"
+ "connectorName='" + connectorName + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
@JsonSubTypes.Type(value = FunctionDescriptionList.class, name = "describe_function"),
@JsonSubTypes.Type(value = FunctionNameList.class, name = "function_names"),
@JsonSubTypes.Type(value = CreateConnectorEntity.class, name = "connector_info"),
@JsonSubTypes.Type(value = DropConnectorEntity.class, name = "drop_connector"),
@JsonSubTypes.Type(value = ConnectorList.class, name = "connector_list"),
@JsonSubTypes.Type(value = ConnectorDescription.class, name = "connector_description"),
@JsonSubTypes.Type(value = ErrorEntity.class, name = "error_entity")
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import io.confluent.ksql.parser.tree.CreateConnector;
import io.confluent.ksql.parser.tree.DescribeConnector;
import io.confluent.ksql.parser.tree.DescribeFunction;
import io.confluent.ksql.parser.tree.DropConnector;
import io.confluent.ksql.parser.tree.Explain;
import io.confluent.ksql.parser.tree.InsertValues;
import io.confluent.ksql.parser.tree.ListConnectors;
@@ -67,6 +68,7 @@ public enum CustomExecutors {
UNSET_PROPERTY(UnsetProperty.class, PropertyExecutor::unset),
INSERT_VALUES(InsertValues.class, insertValuesExecutor()),
CREATE_CONNECTOR(CreateConnector.class, ConnectExecutor::execute),
DROP_CONNECTOR(DropConnector.class, DropConnectorExecutor::execute),
DESCRIBE_CONNECTOR(DescribeConnector.class, new DescribeConnectorExecutor()::execute)
;

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"; you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.rest.server.execution;

import io.confluent.ksql.KsqlExecutionContext;
import io.confluent.ksql.parser.tree.DropConnector;
import io.confluent.ksql.rest.entity.DropConnectorEntity;
import io.confluent.ksql.rest.entity.ErrorEntity;
import io.confluent.ksql.rest.entity.KsqlEntity;
import io.confluent.ksql.services.ConnectClient.ConnectResponse;
import io.confluent.ksql.services.ServiceContext;
import io.confluent.ksql.statement.ConfiguredStatement;
import java.util.Optional;

public final class DropConnectorExecutor {

private DropConnectorExecutor() { }

public static Optional<KsqlEntity> execute(
final ConfiguredStatement<DropConnector> statement,
final KsqlExecutionContext executionContext,
final ServiceContext serviceContext
) {
final String connectorName = statement.getStatement().getConnectorName();
final ConnectResponse<String> response =
serviceContext.getConnectClient().delete(connectorName);

if (response.error().isPresent()) {
return Optional.of(new ErrorEntity(statement.getStatementText(), response.error().get()));
}

return Optional.of(new DropConnectorEntity(statement.getStatementText(), connectorName));
}
}
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
import io.confluent.ksql.parser.tree.CreateConnector;
import io.confluent.ksql.parser.tree.DescribeConnector;
import io.confluent.ksql.parser.tree.DescribeFunction;
import io.confluent.ksql.parser.tree.DropConnector;
import io.confluent.ksql.parser.tree.Explain;
import io.confluent.ksql.parser.tree.InsertValues;
import io.confluent.ksql.parser.tree.ListConnectors;
@@ -70,6 +71,7 @@ public enum CustomValidators {
LIST_PROPERTIES(ListProperties.class, StatementValidator.NO_VALIDATION),
LIST_CONNECTORS(ListConnectors.class, StatementValidator.NO_VALIDATION),
CREATE_CONNECTOR(CreateConnector.class, StatementValidator.NO_VALIDATION),
DROP_CONNECTOR(DropConnector.class, StatementValidator.NO_VALIDATION),

INSERT_VALUES(InsertValues.class, new InsertValuesExecutor()::execute),
SHOW_COLUMNS(ShowColumns.class, ListSourceExecutor::columns),
Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ public void shouldReturnErrorEntityOnError() {

private void givenSuccess() {
when(connectClient.create(anyString(), anyMap()))
.thenReturn(ConnectResponse.of(
.thenReturn(ConnectResponse.success(
new ConnectorInfo(
"foo",
ImmutableMap.of(),
@@ -124,7 +124,7 @@ private void givenSuccess() {

private void givenError() {
when(connectClient.create(anyString(), anyMap()))
.thenReturn(ConnectResponse.of("error!", HttpStatus.SC_BAD_REQUEST));
.thenReturn(ConnectResponse.failure("error!", HttpStatus.SC_BAD_REQUEST));
}

}
Original file line number Diff line number Diff line change
@@ -123,8 +123,8 @@ public void setUp() {
when(source.getDataSourceType()).thenReturn(DataSourceType.KTABLE);
when(source.getKeyField()).thenReturn(KeyField.none());
when(source.getName()).thenReturn("source");
when(connectClient.status(CONNECTOR_NAME)).thenReturn(ConnectResponse.of(STATUS, HttpStatus.SC_OK));
when(connectClient.describe("connector")).thenReturn(ConnectResponse.of(INFO, HttpStatus.SC_OK));
when(connectClient.status(CONNECTOR_NAME)).thenReturn(ConnectResponse.success(STATUS, HttpStatus.SC_OK));
when(connectClient.describe("connector")).thenReturn(ConnectResponse.success(INFO, HttpStatus.SC_OK));

when(connector.matches(any())).thenReturn(false);
when(connector.matches("kafka-topic")).thenReturn(true);
@@ -160,7 +160,7 @@ public void shouldDescribeKnownConnector() {
@Test
public void shouldErrorIfConnectClientFailsStatus() {
// Given:
when(connectClient.describe(any())).thenReturn(ConnectResponse.of("error", HttpStatus.SC_INTERNAL_SERVER_ERROR));
when(connectClient.describe(any())).thenReturn(ConnectResponse.failure("error", HttpStatus.SC_INTERNAL_SERVER_ERROR));

// When:
final Optional<KsqlEntity> entity = executor.execute(describeStatement, engine, serviceContext);
@@ -175,7 +175,7 @@ public void shouldErrorIfConnectClientFailsStatus() {
@Test
public void shouldErrorIfConnectClientFailsDescribe() {
// Given:
when(connectClient.describe(any())).thenReturn(ConnectResponse.of("error", HttpStatus.SC_INTERNAL_SERVER_ERROR));
when(connectClient.describe(any())).thenReturn(ConnectResponse.failure("error", HttpStatus.SC_INTERNAL_SERVER_ERROR));

// When:
final Optional<KsqlEntity> entity = executor.execute(describeStatement, engine, serviceContext);
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2019 Confluent Inc.
*
* Licensed under the Confluent Community License (the "License"; you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.confluent.io/confluent-community-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.confluent.ksql.rest.server.execution;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.common.collect.ImmutableMap;
import io.confluent.ksql.parser.KsqlParser.PreparedStatement;
import io.confluent.ksql.parser.tree.DropConnector;
import io.confluent.ksql.rest.entity.DropConnectorEntity;
import io.confluent.ksql.rest.entity.ErrorEntity;
import io.confluent.ksql.rest.entity.KsqlEntity;
import io.confluent.ksql.services.ConnectClient;
import io.confluent.ksql.services.ConnectClient.ConnectResponse;
import io.confluent.ksql.services.ServiceContext;
import io.confluent.ksql.statement.ConfiguredStatement;
import io.confluent.ksql.util.KsqlConfig;
import java.util.Optional;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class DropConnectorExecutorTest {

private static final KsqlConfig CONFIG = new KsqlConfig(ImmutableMap.of());

private static final DropConnector CREATE_CONNECTOR = new DropConnector(Optional.empty(), "foo");

private static final ConfiguredStatement<DropConnector> DROP_CONNECTOR_CONFIGURED =
ConfiguredStatement.of(
PreparedStatement.of(
"DROP CONNECTOR \"foo\"",
CREATE_CONNECTOR),
ImmutableMap.of(),
CONFIG);

@Mock
private ServiceContext serviceContext;
@Mock
private ConnectClient connectClient;

@Before
public void setUp() {
when(serviceContext.getConnectClient()).thenReturn(connectClient);
}


@Test
public void shouldPassInCorrectArgsToConnectClient() {
// Given:
when(connectClient.delete(anyString()))
.thenReturn(ConnectResponse.success("foo", HttpStatus.SC_OK));

// When:
DropConnectorExecutor.execute(DROP_CONNECTOR_CONFIGURED, null, serviceContext);

// Then:
verify(connectClient).delete("foo");
}

@Test
public void shouldReturnOnSuccess() {
// Given:
when(connectClient.delete(anyString()))
.thenReturn(ConnectResponse.success("foo", HttpStatus.SC_OK));

// When:
final Optional<KsqlEntity> response = DropConnectorExecutor
.execute(DROP_CONNECTOR_CONFIGURED, null, serviceContext);

// Then:
assertThat("expected response", response.isPresent());
assertThat(((DropConnectorEntity) response.get()).getConnectorName(), is("foo"));
}

@Test
public void shouldReturnErrorEntityOnError() {
// Given:
when(connectClient.delete(anyString()))
.thenReturn(ConnectResponse.failure("Danger Mouse!", HttpStatus.SC_INTERNAL_SERVER_ERROR));

// When:
final Optional<KsqlEntity> entity = DropConnectorExecutor
.execute(DROP_CONNECTOR_CONFIGURED, null, serviceContext);

// Then:
assertThat("Expected non-empty response", entity.isPresent());
assertThat(entity.get(), instanceOf(ErrorEntity.class));
}

}
Original file line number Diff line number Diff line change
@@ -69,16 +69,16 @@ public class ListConnectorsExecutorTest {
public void setUp() {
when(serviceContext.getConnectClient()).thenReturn(connectClient);
when(connectClient.describe("connector"))
.thenReturn(ConnectResponse.of(INFO, HttpStatus.SC_OK));
.thenReturn(ConnectResponse.success(INFO, HttpStatus.SC_OK));
when(connectClient.describe("connector2"))
.thenReturn(ConnectResponse.of("DANGER WILL ROBINSON.", HttpStatus.SC_NOT_FOUND));
.thenReturn(ConnectResponse.failure("DANGER WILL ROBINSON.", HttpStatus.SC_NOT_FOUND));
}

@Test
public void shouldListValidConnector() {
// Given:
when(connectClient.connectors())
.thenReturn(ConnectResponse.of(ImmutableList.of("connector"), HttpStatus.SC_OK));
.thenReturn(ConnectResponse.success(ImmutableList.of("connector"), HttpStatus.SC_OK));
final ConfiguredStatement<ListConnectors> statement = ConfiguredStatement.of(
PreparedStatement.of("", new ListConnectors(Optional.empty(), Scope.ALL)),
ImmutableMap.of(),
@@ -106,7 +106,7 @@ public void shouldListValidConnector() {
public void shouldFilterNonMatchingConnectors() {
// Given:
when(connectClient.connectors())
.thenReturn(ConnectResponse.of(ImmutableList.of("connector", "connector2"),
.thenReturn(ConnectResponse.success(ImmutableList.of("connector", "connector2"),
HttpStatus.SC_OK));
final ConfiguredStatement<ListConnectors> statement = ConfiguredStatement.of(
PreparedStatement.of("", new ListConnectors(Optional.empty(), Scope.SINK)),
@@ -133,7 +133,7 @@ public void shouldFilterNonMatchingConnectors() {
public void shouldListInvalidConnectorWithNoInfo() {
// Given:
when(connectClient.connectors())
.thenReturn(ConnectResponse.of(ImmutableList.of("connector2"), HttpStatus.SC_OK));
.thenReturn(ConnectResponse.success(ImmutableList.of("connector2"), HttpStatus.SC_OK));
final ConfiguredStatement<ListConnectors> statement = ConfiguredStatement.of(
PreparedStatement.of("", new ListConnectors(Optional.empty(), Scope.ALL)),
ImmutableMap.of(),