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.InfluxDbTelemetryConfig; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 25 | import org.onosproject.openstacktelemetry.api.config.TelemetryConfig; |
Jian Li | 7fe7eaf | 2018-12-31 17:00:33 +0900 | [diff] [blame^] | 26 | import org.onosproject.openstacktelemetry.api.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.INFLUXDB; |
| 34 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.ADDRESS; |
| 35 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.DATABASE; |
| 36 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.ENABLE_BATCH; |
| 37 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.MEASUREMENT; |
| 38 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.PASSWORD; |
| 39 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.PORT; |
| 40 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.USERNAME; |
| 41 | import static org.onosproject.openstacktelemetry.config.DefaultInfluxDbTelemetryConfig.fromTelemetryConfig; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Unit tests for DefaultInfluxDbTelemetryConfig class. |
| 45 | */ |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 46 | public final class DefaultInfluxDbTelemetryConfigTest { |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 47 | |
| 48 | private static final String IP_ADDRESS_1 = "10.10.10.1"; |
| 49 | private static final String IP_ADDRESS_2 = "20.20.20.1"; |
| 50 | |
| 51 | private static final int PORT_1 = 80; |
| 52 | private static final int PORT_2 = 8080; |
| 53 | |
| 54 | private static final String DATABASE_1 = "database1"; |
| 55 | private static final String DATABASE_2 = "database2"; |
| 56 | |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 57 | private static final String MEASUREMENT_1 = "measurement1"; |
| 58 | private static final String MEASUREMENT_2 = "measurement2"; |
| 59 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 60 | private static final String USERNAME_1 = "username1"; |
| 61 | private static final String USERNAME_2 = "username2"; |
| 62 | |
| 63 | private static final String PASSWORD_1 = "password1"; |
| 64 | private static final String PASSWORD_2 = "password2"; |
| 65 | |
| 66 | private static final boolean ENABLE_BATCH_1 = true; |
| 67 | private static final boolean ENABLE_BATCH_2 = false; |
| 68 | |
| 69 | private static final Map<String, Object> CONFIG_MAP_1 = |
| 70 | ImmutableMap.of("key1", "value1"); |
| 71 | private static final Map<String, Object> CONFIG_MAP_2 = |
| 72 | ImmutableMap.of("key2", "value2"); |
| 73 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 74 | private static final String DUMMY = "dummy"; |
| 75 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 76 | private InfluxDbTelemetryConfig config1; |
| 77 | private InfluxDbTelemetryConfig sameAsConfig1; |
| 78 | private InfluxDbTelemetryConfig config2; |
| 79 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 80 | /** |
| 81 | * Initial setup for this unit test. |
| 82 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 83 | @Before |
| 84 | public void setup() { |
| 85 | |
| 86 | InfluxDbTelemetryConfig.Builder builder1 = |
| 87 | new DefaultInfluxDbTelemetryConfig.DefaultBuilder(); |
| 88 | InfluxDbTelemetryConfig.Builder builder2 = |
| 89 | new DefaultInfluxDbTelemetryConfig.DefaultBuilder(); |
| 90 | InfluxDbTelemetryConfig.Builder builder3 = |
| 91 | new DefaultInfluxDbTelemetryConfig.DefaultBuilder(); |
| 92 | |
| 93 | config1 = builder1 |
| 94 | .withAddress(IP_ADDRESS_1) |
| 95 | .withPort(PORT_1) |
| 96 | .withDatabase(DATABASE_1) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 97 | .withMeasurement(MEASUREMENT_1) |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 98 | .withUsername(USERNAME_1) |
| 99 | .withPassword(PASSWORD_1) |
| 100 | .withEnableBatch(ENABLE_BATCH_1) |
| 101 | .withConfigMap(CONFIG_MAP_1) |
| 102 | .build(); |
| 103 | |
| 104 | sameAsConfig1 = builder2 |
| 105 | .withAddress(IP_ADDRESS_1) |
| 106 | .withPort(PORT_1) |
| 107 | .withDatabase(DATABASE_1) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 108 | .withMeasurement(MEASUREMENT_1) |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 109 | .withUsername(USERNAME_1) |
| 110 | .withPassword(PASSWORD_1) |
| 111 | .withEnableBatch(ENABLE_BATCH_1) |
| 112 | .withConfigMap(CONFIG_MAP_1) |
| 113 | .build(); |
| 114 | |
| 115 | config2 = builder3 |
| 116 | .withAddress(IP_ADDRESS_2) |
| 117 | .withPort(PORT_2) |
| 118 | .withDatabase(DATABASE_2) |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 119 | .withMeasurement(MEASUREMENT_2) |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 120 | .withUsername(USERNAME_2) |
| 121 | .withPassword(PASSWORD_2) |
| 122 | .withEnableBatch(ENABLE_BATCH_2) |
| 123 | .withConfigMap(CONFIG_MAP_2) |
| 124 | .build(); |
| 125 | } |
| 126 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 127 | /** |
| 128 | * Tests class immutability. |
| 129 | */ |
| 130 | @Test |
| 131 | public void testImmutability() { |
| 132 | assertThatClassIsImmutable(DefaultInfluxDbTelemetryConfig.class); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Tests object equality. |
| 137 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 138 | @Test |
| 139 | public void testEquality() { |
| 140 | new EqualsTester() |
| 141 | .addEqualityGroup(config1, sameAsConfig1) |
| 142 | .addEqualityGroup(config2).testEquals(); |
| 143 | } |
| 144 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 145 | /** |
| 146 | * Tests object construction. |
| 147 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 148 | @Test |
| 149 | public void testConstruction() { |
| 150 | InfluxDbTelemetryConfig config = config1; |
| 151 | |
| 152 | assertThat(config.address(), is(IP_ADDRESS_1)); |
| 153 | assertThat(config.port(), is(PORT_1)); |
| 154 | assertThat(config.database(), is(DATABASE_1)); |
Boyoung Jeong | 4d1c9d1 | 2018-07-20 17:09:20 +0900 | [diff] [blame] | 155 | assertThat(config.measurement(), is(MEASUREMENT_1)); |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 156 | assertThat(config.username(), is(USERNAME_1)); |
| 157 | assertThat(config.password(), is(PASSWORD_1)); |
| 158 | assertThat(config.enableBatch(), is(ENABLE_BATCH_1)); |
| 159 | assertThat(config.configMap(), is(CONFIG_MAP_1)); |
| 160 | } |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Tests props extraction. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testPropsExtraction() { |
| 167 | Map<String, String> props = Maps.newConcurrentMap(); |
| 168 | props.put(ADDRESS, IP_ADDRESS_1); |
| 169 | props.put(PORT, String.valueOf(PORT_1)); |
| 170 | props.put(USERNAME, USERNAME_1); |
| 171 | props.put(PASSWORD, PASSWORD_1); |
| 172 | props.put(DATABASE, DATABASE_1); |
| 173 | props.put(ENABLE_BATCH, String.valueOf(ENABLE_BATCH_1)); |
| 174 | props.put(MEASUREMENT, MEASUREMENT_1); |
| 175 | |
| 176 | TelemetryConfig config = new DefaultTelemetryConfig(DUMMY, INFLUXDB, |
| 177 | ImmutableList.of(), DUMMY, DUMMY, false, props); |
| 178 | |
| 179 | InfluxDbTelemetryConfig influxDbConfig = fromTelemetryConfig(config); |
| 180 | assertThat(influxDbConfig.address(), is(IP_ADDRESS_1)); |
| 181 | assertThat(influxDbConfig.port(), is(PORT_1)); |
| 182 | assertThat(influxDbConfig.database(), is(DATABASE_1)); |
| 183 | assertThat(influxDbConfig.measurement(), is(MEASUREMENT_1)); |
| 184 | assertThat(influxDbConfig.username(), is(USERNAME_1)); |
| 185 | assertThat(influxDbConfig.password(), is(PASSWORD_1)); |
| 186 | assertThat(influxDbConfig.enableBatch(), is(ENABLE_BATCH_1)); |
| 187 | } |
| 188 | } |