|
| 1 | +/** |
| 2 | + * Copyright 2020 Confluent Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + **/ |
| 16 | + |
| 17 | +package io.confluent.connect.elasticsearch; |
| 18 | + |
| 19 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.BATCH_SIZE_CONFIG; |
| 20 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.CONNECTION_PASSWORD_CONFIG; |
| 21 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.CONNECTION_URL_CONFIG; |
| 22 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.CONNECTION_USERNAME_CONFIG; |
| 23 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.FLUSH_TIMEOUT_MS_CONFIG; |
| 24 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.IGNORE_KEY_CONFIG; |
| 25 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.IGNORE_KEY_TOPICS_CONFIG; |
| 26 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_CONFIG; |
| 27 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.IGNORE_SCHEMA_TOPICS_CONFIG; |
| 28 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.LINGER_MS_CONFIG; |
| 29 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.MAX_BUFFERED_RECORDS_CONFIG; |
| 30 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.MAX_IN_FLIGHT_REQUESTS_CONFIG; |
| 31 | +import static io.confluent.connect.elasticsearch.ElasticsearchSinkConnectorConfig.TYPE_NAME_CONFIG; |
| 32 | +import static org.junit.Assert.assertFalse; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | + |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.Map; |
| 37 | +import org.apache.kafka.common.config.Config; |
| 38 | +import org.apache.kafka.common.config.ConfigValue; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | + |
| 42 | +public class ValidatorTest { |
| 43 | + |
| 44 | + private Map<String, String> props; |
| 45 | + private Validator validator; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() { |
| 49 | + props = new HashMap<>(); |
| 50 | + props.put(TYPE_NAME_CONFIG, "type"); |
| 51 | + props.put(CONNECTION_URL_CONFIG, "localhost:8080"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testInvalidCredentials() { |
| 56 | + props.put(CONNECTION_USERNAME_CONFIG, "username"); |
| 57 | + validator = new Validator(props); |
| 58 | + |
| 59 | + Config result = validator.validate(); |
| 60 | + assertHasErrorMessage(result, CONNECTION_USERNAME_CONFIG, "must be set"); |
| 61 | + assertHasErrorMessage(result, CONNECTION_PASSWORD_CONFIG, "must be set"); |
| 62 | + props.remove(CONNECTION_USERNAME_CONFIG); |
| 63 | + |
| 64 | + props.put(CONNECTION_PASSWORD_CONFIG, "password"); |
| 65 | + validator = new Validator(props); |
| 66 | + result = validator.validate(); |
| 67 | + assertHasErrorMessage(result, CONNECTION_USERNAME_CONFIG, "must be set"); |
| 68 | + assertHasErrorMessage(result, CONNECTION_PASSWORD_CONFIG, "must be set"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testValidCredentials() { |
| 73 | + // username and password not set |
| 74 | + validator = new Validator(props); |
| 75 | + |
| 76 | + Config result = validator.validate(); |
| 77 | + assertNoErrors(result); |
| 78 | + |
| 79 | + // both set |
| 80 | + props.put(CONNECTION_USERNAME_CONFIG, "username"); |
| 81 | + props.put(CONNECTION_PASSWORD_CONFIG, "password"); |
| 82 | + validator = new Validator(props); |
| 83 | + |
| 84 | + result = validator.validate(); |
| 85 | + assertNoErrors(result); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testInvalidIgnoreConfigs() { |
| 90 | + props.put(IGNORE_KEY_CONFIG, "true"); |
| 91 | + props.put(IGNORE_KEY_TOPICS_CONFIG, "some,topics"); |
| 92 | + props.put(IGNORE_SCHEMA_CONFIG, "true"); |
| 93 | + props.put(IGNORE_SCHEMA_TOPICS_CONFIG, "some,other,topics"); |
| 94 | + validator = new Validator(props); |
| 95 | + |
| 96 | + Config result = validator.validate(); |
| 97 | + assertHasErrorMessage(result, IGNORE_KEY_CONFIG, "is true"); |
| 98 | + assertHasErrorMessage(result, IGNORE_KEY_TOPICS_CONFIG, "is true"); |
| 99 | + assertHasErrorMessage(result, IGNORE_SCHEMA_CONFIG, "is true"); |
| 100 | + assertHasErrorMessage(result, IGNORE_SCHEMA_TOPICS_CONFIG, "is true"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testValidIgnoreConfigs() { |
| 105 | + // topics configs not set |
| 106 | + props.put(IGNORE_KEY_CONFIG, "true"); |
| 107 | + props.put(IGNORE_SCHEMA_CONFIG, "true"); |
| 108 | + validator = new Validator(props); |
| 109 | + |
| 110 | + Config result = validator.validate(); |
| 111 | + assertNoErrors(result); |
| 112 | + |
| 113 | + // ignore configs are false |
| 114 | + props.put(IGNORE_KEY_CONFIG, "false"); |
| 115 | + props.put(IGNORE_KEY_TOPICS_CONFIG, "some,topics"); |
| 116 | + props.put(IGNORE_SCHEMA_CONFIG, "false"); |
| 117 | + props.put(IGNORE_SCHEMA_TOPICS_CONFIG, "some,other,topics"); |
| 118 | + validator = new Validator(props); |
| 119 | + |
| 120 | + result = validator.validate(); |
| 121 | + assertNoErrors(result); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testInvalidLingerMs() { |
| 126 | + props.put(LINGER_MS_CONFIG, "2"); |
| 127 | + props.put(FLUSH_TIMEOUT_MS_CONFIG, "1"); |
| 128 | + validator = new Validator(props); |
| 129 | + |
| 130 | + Config result = validator.validate(); |
| 131 | + assertHasErrorMessage(result, LINGER_MS_CONFIG, "can not be larger than"); |
| 132 | + assertHasErrorMessage(result, FLUSH_TIMEOUT_MS_CONFIG, "can not be larger than"); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testValidLingerMs() { |
| 137 | + props.put(LINGER_MS_CONFIG, "1"); |
| 138 | + props.put(FLUSH_TIMEOUT_MS_CONFIG, "2"); |
| 139 | + validator = new Validator(props); |
| 140 | + |
| 141 | + Config result = validator.validate(); |
| 142 | + assertNoErrors(result); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testInvalidMaxBufferedRecords() { |
| 147 | + props.put(MAX_BUFFERED_RECORDS_CONFIG, "1"); |
| 148 | + props.put(BATCH_SIZE_CONFIG, "2"); |
| 149 | + props.put(MAX_IN_FLIGHT_REQUESTS_CONFIG, "2"); |
| 150 | + validator = new Validator(props); |
| 151 | + |
| 152 | + Config result = validator.validate(); |
| 153 | + assertHasErrorMessage(result, MAX_BUFFERED_RECORDS_CONFIG, "must be larger than or equal to"); |
| 154 | + assertHasErrorMessage(result, BATCH_SIZE_CONFIG, "must be larger than or equal to"); |
| 155 | + assertHasErrorMessage(result, MAX_IN_FLIGHT_REQUESTS_CONFIG, "must be larger than or equal to"); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testValidMaxBufferedRecords() { |
| 160 | + props.put(MAX_BUFFERED_RECORDS_CONFIG, "5"); |
| 161 | + props.put(BATCH_SIZE_CONFIG, "2"); |
| 162 | + props.put(MAX_IN_FLIGHT_REQUESTS_CONFIG, "2"); |
| 163 | + validator = new Validator(props); |
| 164 | + |
| 165 | + Config result = validator.validate(); |
| 166 | + assertNoErrors(result); |
| 167 | + } |
| 168 | + |
| 169 | + private static void assertHasErrorMessage(Config config, String property, String msg) { |
| 170 | + for (ConfigValue configValue : config.configValues()) { |
| 171 | + if (configValue.name().equals(property)) { |
| 172 | + assertFalse(configValue.errorMessages().isEmpty()); |
| 173 | + assertTrue(configValue.errorMessages().get(0).contains(msg)); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static void assertNoErrors(Config config) { |
| 179 | + config.configValues().forEach(c -> assertTrue(c.errorMessages().isEmpty())); |
| 180 | + } |
| 181 | +} |
0 commit comments