use of io.strimzi.test.TestUtils.map in project strimzi by strimzi.
the class ConnectorMockTest method setupMockConnectAPI.
private void setupMockConnectAPI() {
api = mock(KafkaConnectApi.class);
runningConnectors = new HashMap<>();
when(api.list(any(), anyInt())).thenAnswer(i -> {
String host = i.getArgument(0);
String matchingKeyPrefix = host + "##";
return Future.succeededFuture(runningConnectors.keySet().stream().filter(s -> s.startsWith(matchingKeyPrefix)).map(s -> s.substring(matchingKeyPrefix.length())).collect(Collectors.toList()));
});
when(api.listConnectorPlugins(any(), any(), anyInt())).thenAnswer(i -> {
ConnectorPlugin connectorPlugin = new ConnectorPluginBuilder().withConnectorClass("io.strimzi.MyClass").withType("sink").withVersion("1.0.0").build();
return Future.succeededFuture(Collections.singletonList(connectorPlugin));
});
when(api.updateConnectLoggers(any(), anyString(), anyInt(), anyString(), any(OrderedProperties.class))).thenReturn(Future.succeededFuture());
when(api.getConnectorConfig(any(), any(), any(), anyInt(), any())).thenAnswer(invocation -> {
String host = invocation.getArgument(2);
String connectorName = invocation.getArgument(4);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState != null) {
Map<String, String> map = new HashMap<>();
map.put("name", connectorName);
for (Map.Entry<String, Object> entry : connectorState.config) {
if (entry.getValue() != null) {
map.put(entry.getKey(), entry.getValue().toString());
}
}
return Future.succeededFuture(map);
} else {
return Future.failedFuture(new ConnectRestException("GET", String.format("/connectors/%s/config", connectorName), 404, "Not Found", ""));
}
});
when(api.getConnector(any(), any(), anyInt(), any())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
String connectorName = invocation.getArgument(3);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("GET", String.format("/connectors/%s", connectorName), 404, "Not Found", ""));
}
return Future.succeededFuture(TestUtils.map("name", connectorName, "config", connectorState.config, "tasks", emptyMap()));
});
when(api.createOrUpdatePutRequest(any(), any(), anyInt(), anyString(), any())).thenAnswer(invocation -> {
LOGGER.info((String) invocation.getArgument(1) + invocation.getArgument(2) + invocation.getArgument(3) + invocation.getArgument(4));
String host = invocation.getArgument(1);
LOGGER.info("###### create " + host);
String connectorName = invocation.getArgument(3);
JsonObject connectorConfig = invocation.getArgument(4);
runningConnectors.putIfAbsent(key(host, connectorName), new ConnectorState(false, connectorConfig));
return Future.succeededFuture();
});
when(api.delete(any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
LOGGER.info("###### delete " + host);
String connectorName = invocation.getArgument(3);
ConnectorState remove = runningConnectors.remove(key(host, connectorName));
return remove != null ? Future.succeededFuture() : Future.failedFuture("No such connector " + connectorName);
});
when(api.statusWithBackOff(any(), any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(2);
LOGGER.info("###### status " + host);
String connectorName = invocation.getArgument(4);
return kafkaConnectApiStatusMock(host, connectorName);
});
when(api.status(any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
LOGGER.info("###### status " + host);
String connectorName = invocation.getArgument(3);
return kafkaConnectApiStatusMock(host, connectorName);
});
when(api.pause(any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
if (!connectorState.paused) {
runningConnectors.put(key(host, connectorName), new ConnectorState(true, connectorState.config));
}
return Future.succeededFuture();
});
when(api.resume(any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
if (connectorState.paused) {
runningConnectors.put(key(host, connectorName), new ConnectorState(false, connectorState.config));
}
return Future.succeededFuture();
});
when(api.restart(any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
return Future.succeededFuture();
});
when(api.restartTask(any(), anyInt(), anyString(), anyInt())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
return Future.succeededFuture();
});
when(api.getConnectorTopics(any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
String connectorName = invocation.getArgument(3);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("GET", String.format("/connectors/%s/topics", connectorName), 404, "Not Found", ""));
}
return Future.succeededFuture(List.of("my-topic"));
});
}
use of io.strimzi.test.TestUtils.map in project strimzi-kafka-operator by strimzi.
the class ConnectorMockTest method setupMockConnectAPI.
private void setupMockConnectAPI() {
api = mock(KafkaConnectApi.class);
runningConnectors = new HashMap<>();
when(api.list(any(), anyInt())).thenAnswer(i -> {
String host = i.getArgument(0);
String matchingKeyPrefix = host + "##";
return Future.succeededFuture(runningConnectors.keySet().stream().filter(s -> s.startsWith(matchingKeyPrefix)).map(s -> s.substring(matchingKeyPrefix.length())).collect(Collectors.toList()));
});
when(api.listConnectorPlugins(any(), any(), anyInt())).thenAnswer(i -> {
ConnectorPlugin connectorPlugin = new ConnectorPluginBuilder().withConnectorClass("io.strimzi.MyClass").withType("sink").withVersion("1.0.0").build();
return Future.succeededFuture(Collections.singletonList(connectorPlugin));
});
when(api.updateConnectLoggers(any(), anyString(), anyInt(), anyString(), any(OrderedProperties.class))).thenReturn(Future.succeededFuture());
when(api.getConnectorConfig(any(), any(), any(), anyInt(), any())).thenAnswer(invocation -> {
String host = invocation.getArgument(2);
String connectorName = invocation.getArgument(4);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState != null) {
Map<String, String> map = new HashMap<>();
map.put("name", connectorName);
for (Map.Entry<String, Object> entry : connectorState.config) {
if (entry.getValue() != null) {
map.put(entry.getKey(), entry.getValue().toString());
}
}
return Future.succeededFuture(map);
} else {
return Future.failedFuture(new ConnectRestException("GET", String.format("/connectors/%s/config", connectorName), 404, "Not Found", ""));
}
});
when(api.getConnector(any(), any(), anyInt(), any())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
String connectorName = invocation.getArgument(3);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("GET", String.format("/connectors/%s", connectorName), 404, "Not Found", ""));
}
return Future.succeededFuture(TestUtils.map("name", connectorName, "config", connectorState.config, "tasks", emptyMap()));
});
when(api.createOrUpdatePutRequest(any(), any(), anyInt(), anyString(), any())).thenAnswer(invocation -> {
LOGGER.info((String) invocation.getArgument(1) + invocation.getArgument(2) + invocation.getArgument(3) + invocation.getArgument(4));
String host = invocation.getArgument(1);
LOGGER.info("###### create " + host);
String connectorName = invocation.getArgument(3);
JsonObject connectorConfig = invocation.getArgument(4);
runningConnectors.putIfAbsent(key(host, connectorName), new ConnectorState(false, connectorConfig));
return Future.succeededFuture();
});
when(api.delete(any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
LOGGER.info("###### delete " + host);
String connectorName = invocation.getArgument(3);
ConnectorState remove = runningConnectors.remove(key(host, connectorName));
return remove != null ? Future.succeededFuture() : Future.failedFuture("No such connector " + connectorName);
});
when(api.statusWithBackOff(any(), any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(2);
LOGGER.info("###### status " + host);
String connectorName = invocation.getArgument(4);
return kafkaConnectApiStatusMock(host, connectorName);
});
when(api.status(any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
LOGGER.info("###### status " + host);
String connectorName = invocation.getArgument(3);
return kafkaConnectApiStatusMock(host, connectorName);
});
when(api.pause(any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
if (!connectorState.paused) {
runningConnectors.put(key(host, connectorName), new ConnectorState(true, connectorState.config));
}
return Future.succeededFuture();
});
when(api.resume(any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
if (connectorState.paused) {
runningConnectors.put(key(host, connectorName), new ConnectorState(false, connectorState.config));
}
return Future.succeededFuture();
});
when(api.restart(any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
return Future.succeededFuture();
});
when(api.restartTask(any(), anyInt(), anyString(), anyInt())).thenAnswer(invocation -> {
String host = invocation.getArgument(0);
String connectorName = invocation.getArgument(2);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("PUT", "", 404, "Not found", "Connector name " + connectorName));
}
return Future.succeededFuture();
});
when(api.getConnectorTopics(any(), any(), anyInt(), anyString())).thenAnswer(invocation -> {
String host = invocation.getArgument(1);
String connectorName = invocation.getArgument(3);
ConnectorState connectorState = runningConnectors.get(key(host, connectorName));
if (connectorState == null) {
return Future.failedFuture(new ConnectRestException("GET", String.format("/connectors/%s/topics", connectorName), 404, "Not Found", ""));
}
return Future.succeededFuture(List.of("my-topic"));
});
}
Aggregations