Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openstacktelemetry.config; |
| 17 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.ImmutableList; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 20 | import com.google.common.collect.Maps; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 21 | import com.google.common.testing.EqualsTester; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.openstacktelemetry.api.config.KafkaTelemetryConfig; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 25 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
| 26 | import org.onosproject.openstacktelemetry.impl.DefaultTelemetryConfig; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 32 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 33 | import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.KAFKA; |
| 34 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.ADDRESS; |
| 35 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.BATCH_SIZE; |
| 36 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.CODEC; |
| 37 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.KEY; |
| 38 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.KEY_SERIALIZER; |
| 39 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.LINGER_MS; |
| 40 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.MEMORY_BUFFER; |
| 41 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.PORT; |
| 42 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.REQUIRED_ACKS; |
| 43 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.RETRIES; |
| 44 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.TOPIC; |
| 45 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.VALUE_SERIALIZER; |
| 46 | import static org.onosproject.openstacktelemetry.config.DefaultKafkaTelemetryConfig.fromTelemetryConfig; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 47 | |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 48 | public final class DefaultKafkaTelemetryConfigTest { |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 49 | |
| 50 | private static final String IP_ADDRESS_1 = "10.10.10.1"; |
| 51 | private static final String IP_ADDRESS_2 = "20.20.20.1"; |
| 52 | |
| 53 | private static final int PORT_1 = 80; |
| 54 | private static final int PORT_2 = 8080; |
| 55 | |
| 56 | private static final int RETRIES_1 = 1; |
| 57 | private static final int RETRIES_2 = 2; |
| 58 | |
| 59 | private static final int BATCH_SIZE_1 = 100; |
| 60 | private static final int BATCH_SIZE_2 = 200; |
| 61 | |
| 62 | private static final int MEMORY_BUFFER_1 = 1000; |
| 63 | private static final int MEMORY_BUFFER_2 = 2000; |
| 64 | |
| 65 | private static final String REQUIRED_ACKS_1 = "all"; |
| 66 | private static final String REQUIRED_ACKS_2 = "none"; |
| 67 | |
| 68 | private static final int LINGER_MS_1 = 1; |
| 69 | private static final int LINGER_MS_2 = 2; |
| 70 | |
| 71 | private static final String KEY_SERIALIZER_1 = "keyserializer1"; |
| 72 | private static final String KEY_SERIALIZER_2 = "keyserializer2"; |
| 73 | private static final String VALUE_SERIALIZER_1 = "valueserializer1"; |
| 74 | private static final String VALUE_SERIALIZER_2 = "valueserializer2"; |
| 75 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 76 | private static final String KEY_1 = "key1"; |
| 77 | private static final String KEY_2 = "key2"; |
| 78 | |
| 79 | private static final String TOPIC_1 = "topic1"; |
| 80 | private static final String TOPIC_2 = "topic2"; |
| 81 | |
| 82 | private static final String CODEC_1 = "codec1"; |
| 83 | private static final String CODEC_2 = "codec2"; |
| 84 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 85 | private static final Map<String, Object> CONFIG_MAP_1 = |
| 86 | ImmutableMap.of("key1", "value1"); |
| 87 | private static final Map<String, Object> CONFIG_MAP_2 = |
| 88 | ImmutableMap.of("key2", "value2"); |
| 89 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 90 | private static final String DUMMY = "dummy"; |
| 91 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 92 | private KafkaTelemetryConfig config1; |
| 93 | private KafkaTelemetryConfig sameAsConfig1; |
| 94 | private KafkaTelemetryConfig config2; |
| 95 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 96 | /** |
| 97 | * Initial setup for this unit test. |
| 98 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 99 | @Before |
| 100 | public void setup() { |
| 101 | |
| 102 | KafkaTelemetryConfig.Builder builder1 = |
| 103 | new DefaultKafkaTelemetryConfig.DefaultBuilder(); |
| 104 | KafkaTelemetryConfig.Builder builder2 = |
| 105 | new DefaultKafkaTelemetryConfig.DefaultBuilder(); |
| 106 | KafkaTelemetryConfig.Builder builder3 = |
| 107 | new DefaultKafkaTelemetryConfig.DefaultBuilder(); |
| 108 | |
| 109 | config1 = builder1 |
| 110 | .withAddress(IP_ADDRESS_1) |
| 111 | .withPort(PORT_1) |
| 112 | .withRetries(RETRIES_1) |
| 113 | .withBatchSize(BATCH_SIZE_1) |
| 114 | .withMemoryBuffer(MEMORY_BUFFER_1) |
| 115 | .withRequiredAcks(REQUIRED_ACKS_1) |
| 116 | .withLingerMs(LINGER_MS_1) |
| 117 | .withKeySerializer(KEY_SERIALIZER_1) |
| 118 | .withValueSerializer(VALUE_SERIALIZER_1) |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 119 | .withKey(KEY_1) |
| 120 | .withTopic(TOPIC_1) |
| 121 | .withCodec(CODEC_1) |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 122 | .withConfigMap(CONFIG_MAP_1) |
| 123 | .build(); |
| 124 | |
| 125 | sameAsConfig1 = builder2 |
| 126 | .withAddress(IP_ADDRESS_1) |
| 127 | .withPort(PORT_1) |
| 128 | .withRetries(RETRIES_1) |
| 129 | .withBatchSize(BATCH_SIZE_1) |
| 130 | .withMemoryBuffer(MEMORY_BUFFER_1) |
| 131 | .withRequiredAcks(REQUIRED_ACKS_1) |
| 132 | .withLingerMs(LINGER_MS_1) |
| 133 | .withKeySerializer(KEY_SERIALIZER_1) |
| 134 | .withValueSerializer(VALUE_SERIALIZER_1) |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 135 | .withKey(KEY_1) |
| 136 | .withTopic(TOPIC_1) |
| 137 | .withCodec(CODEC_1) |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 138 | .withConfigMap(CONFIG_MAP_1) |
| 139 | .build(); |
| 140 | |
| 141 | config2 = builder3 |
| 142 | .withAddress(IP_ADDRESS_2) |
| 143 | .withPort(PORT_2) |
| 144 | .withRetries(RETRIES_2) |
| 145 | .withBatchSize(BATCH_SIZE_2) |
| 146 | .withMemoryBuffer(MEMORY_BUFFER_2) |
| 147 | .withRequiredAcks(REQUIRED_ACKS_2) |
| 148 | .withLingerMs(LINGER_MS_2) |
| 149 | .withKeySerializer(KEY_SERIALIZER_2) |
| 150 | .withValueSerializer(VALUE_SERIALIZER_2) |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 151 | .withKey(KEY_2) |
| 152 | .withTopic(TOPIC_2) |
| 153 | .withCodec(CODEC_2) |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 154 | .withConfigMap(CONFIG_MAP_2) |
| 155 | .build(); |
| 156 | } |
| 157 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 158 | /** |
| 159 | * Tests class immutability. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testImmutability() { |
| 163 | assertThatClassIsImmutable(DefaultKafkaTelemetryConfig.class); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Tests object equality. |
| 168 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 169 | @Test |
| 170 | public void testEquality() { |
| 171 | new EqualsTester() |
| 172 | .addEqualityGroup(config1, sameAsConfig1) |
| 173 | .addEqualityGroup(config2).testEquals(); |
| 174 | } |
| 175 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 176 | /** |
| 177 | * Tests object construction. |
| 178 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 179 | @Test |
| 180 | public void testConstruction() { |
| 181 | KafkaTelemetryConfig config = config1; |
| 182 | |
| 183 | assertThat(config.address(), is(IP_ADDRESS_1)); |
| 184 | assertThat(config.port(), is(PORT_1)); |
| 185 | assertThat(config.retries(), is(RETRIES_1)); |
| 186 | assertThat(config.batchSize(), is(BATCH_SIZE_1)); |
| 187 | assertThat(config.memoryBuffer(), is(MEMORY_BUFFER_1)); |
| 188 | assertThat(config.requiredAcks(), is(REQUIRED_ACKS_1)); |
| 189 | assertThat(config.lingerMs(), is(LINGER_MS_1)); |
| 190 | assertThat(config.keySerializer(), is(KEY_SERIALIZER_1)); |
| 191 | assertThat(config.valueSerializer(), is(VALUE_SERIALIZER_1)); |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 192 | assertThat(config.key(), is(KEY_1)); |
| 193 | assertThat(config.topic(), is(TOPIC_1)); |
| 194 | assertThat(config.codec(), is(CODEC_1)); |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 195 | assertThat(config.configMap(), is(CONFIG_MAP_1)); |
| 196 | } |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame^] | 197 | |
| 198 | /** |
| 199 | * Tests props extraction. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testPropsExtraction() { |
| 203 | Map<String, String> props = Maps.newConcurrentMap(); |
| 204 | props.put(ADDRESS, IP_ADDRESS_1); |
| 205 | props.put(PORT, String.valueOf(PORT_1)); |
| 206 | props.put(RETRIES, String.valueOf(RETRIES_1)); |
| 207 | props.put(BATCH_SIZE, String.valueOf(BATCH_SIZE_1)); |
| 208 | props.put(MEMORY_BUFFER, String.valueOf(MEMORY_BUFFER_1)); |
| 209 | props.put(REQUIRED_ACKS, REQUIRED_ACKS_1); |
| 210 | props.put(LINGER_MS, String.valueOf(LINGER_MS_1)); |
| 211 | props.put(KEY_SERIALIZER, KEY_SERIALIZER_1); |
| 212 | props.put(VALUE_SERIALIZER, VALUE_SERIALIZER_1); |
| 213 | props.put(KEY, KEY_1); |
| 214 | props.put(TOPIC, TOPIC_1); |
| 215 | props.put(CODEC, CODEC_1); |
| 216 | |
| 217 | TelemetryConfig config = new DefaultTelemetryConfig(DUMMY, KAFKA, |
| 218 | ImmutableList.of(), DUMMY, DUMMY, false, props); |
| 219 | |
| 220 | KafkaTelemetryConfig kafkaConfig = fromTelemetryConfig(config); |
| 221 | assertThat(kafkaConfig.address(), is(IP_ADDRESS_1)); |
| 222 | assertThat(kafkaConfig.port(), is(PORT_1)); |
| 223 | assertThat(kafkaConfig.retries(), is(RETRIES_1)); |
| 224 | assertThat(kafkaConfig.batchSize(), is(BATCH_SIZE_1)); |
| 225 | assertThat(kafkaConfig.memoryBuffer(), is(MEMORY_BUFFER_1)); |
| 226 | assertThat(kafkaConfig.requiredAcks(), is(REQUIRED_ACKS_1)); |
| 227 | assertThat(kafkaConfig.lingerMs(), is(LINGER_MS_1)); |
| 228 | assertThat(kafkaConfig.keySerializer(), is(KEY_SERIALIZER_1)); |
| 229 | assertThat(kafkaConfig.valueSerializer(), is(VALUE_SERIALIZER_1)); |
| 230 | assertThat(kafkaConfig.key(), is(KEY_1)); |
| 231 | assertThat(kafkaConfig.topic(), is(TOPIC_1)); |
| 232 | assertThat(kafkaConfig.codec(), is(CODEC_1)); |
| 233 | } |
| 234 | } |