From 1d771263b93603e320dd376c20464116767f9f15 Mon Sep 17 00:00:00 2001 From: Vihas Splunk Date: Fri, 15 Mar 2024 12:57:00 +0530 Subject: [PATCH] chore: record errors in Config --- .../kafka/connect/SplunkSinkConnector.java | 11 ++++++++++ .../connect/SplunkSinkConnecterTest.java | 21 ++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/splunk/kafka/connect/SplunkSinkConnector.java b/src/main/java/com/splunk/kafka/connect/SplunkSinkConnector.java index fa9f0410..21454391 100644 --- a/src/main/java/com/splunk/kafka/connect/SplunkSinkConnector.java +++ b/src/main/java/com/splunk/kafka/connect/SplunkSinkConnector.java @@ -149,6 +149,17 @@ private void validateSplunkConfigurations(final Map configs) thr if (configs.containsKey(DISABLE_VALIDATION) && Boolean.parseBoolean(configs.get(DISABLE_VALIDATION))) { return; } + if (configs.getOrDefault(TOKEN_CONF,"").equals("") + || configs.getOrDefault(URI_CONF,"").equals("")) { + String errorMessage = String.format( + "Either one of '%s' or '%s' must be set for Splunk validation check.", + TOKEN_CONF, + URI_CONF + ); + addErrorMessage(TOKEN_CONF, errorMessage); + addErrorMessage(URI_CONF, errorMessage); + return; + } SplunkSinkConnectorConfig connectorConfig = new SplunkSinkConnectorConfig(configs); String[] indexes = split(connectorConfig.indexes, ","); if(indexes == null || indexes.length == 0) { diff --git a/src/test/java/com/splunk/kafka/connect/SplunkSinkConnecterTest.java b/src/test/java/com/splunk/kafka/connect/SplunkSinkConnecterTest.java index d72cf01f..cef37c13 100644 --- a/src/test/java/com/splunk/kafka/connect/SplunkSinkConnecterTest.java +++ b/src/test/java/com/splunk/kafka/connect/SplunkSinkConnecterTest.java @@ -72,7 +72,19 @@ public void config() { ConfigDef config = connector.config(); Assert.assertNotNull(config); } - + @Test + public void testErrorWithoutUriConf() { + final Map configs = new HashMap<>(); + SinkConnector connector = new SplunkSinkConnector(); + configs.put("topics", "b"); + configs.put("splunk.indexes", "b"); + MockHecClientWrapper clientInstance = new MockHecClientWrapper(); + ((SplunkSinkConnector) connector).setHecInstance(clientInstance); + Config result = connector.validate(configs); + assertHasErrorMessage(result, URI_CONF, "Either one of 'splunk.hec.token' or 'splunk.hec.uri' must be set for Splunk validation check.", 1); + assertHasErrorMessage(result, TOKEN_CONF, "Either one of 'splunk.hec.token' or 'splunk.hec.uri' must be set for Splunk validation check.", 1); + } + @Test public void testValidKerberosBothEmpty() { final Map configs = new HashMap<>(); @@ -304,12 +316,15 @@ private void addNecessaryConfigs(Map configs) { configs.put(URI_CONF, TEST_URI); configs.put(TOKEN_CONF, "blah"); } - private void assertHasErrorMessage(Config config, String property, String msg) { + assertHasErrorMessage(config, property, msg, 0); + } + private void assertHasErrorMessage(Config config, String property, String msg, int idx) { for (ConfigValue configValue : config.configValues()) { if (configValue.name().equals(property)) { + System.out.print(configValue.errorMessages()); assertFalse(configValue.errorMessages().isEmpty()); - assertTrue(configValue.errorMessages().get(0).contains(msg)); + assertTrue(configValue.errorMessages().get(idx).contains(msg)); } } }