boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +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; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +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; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +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.PrometheusTelemetryConfig; |
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; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
| 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.PROMETHEUS; |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 34 | import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.DISABLED; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 35 | import static org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig.ADDRESS; |
| 36 | import static org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig.PORT; |
| 37 | import static org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig.fromTelemetryConfig; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Unit tests for DefaultPrometheusTelemetryConfig class. |
| 41 | */ |
| 42 | public class DefaultPrometheusTelemetryConfigTest { |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 43 | private static final String IP_ADDRESS_1 = "10.10.1.1"; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 44 | private static final String IP_ADDRESS_2 = "10.10.1.2"; |
| 45 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 46 | private static final int PORT_1 = 50050; |
| 47 | private static final int PORT_2 = 50051; |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 48 | |
| 49 | private static final Map<String, Object> CONFIG_MAP_1 = |
| 50 | ImmutableMap.of("key1", "value1"); |
| 51 | private static final Map<String, Object> CONFIG_MAP_2 = |
| 52 | ImmutableMap.of("key2", "value2"); |
| 53 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 54 | private static final String DUMMY = "dummy"; |
| 55 | |
boyoung2 | 1c5f5f4 | 2018-09-27 20:29:41 +0900 | [diff] [blame] | 56 | private PrometheusTelemetryConfig config1; |
| 57 | private PrometheusTelemetryConfig sameAsConfig1; |
| 58 | private PrometheusTelemetryConfig config2; |
| 59 | |
| 60 | /** |
| 61 | * Initial setup for this unit test. |
| 62 | */ |
| 63 | @Before |
| 64 | public void setup() { |
| 65 | |
| 66 | PrometheusTelemetryConfig.Builder builder1 = |
| 67 | new DefaultPrometheusTelemetryConfig.DefaultBuilder(); |
| 68 | PrometheusTelemetryConfig.Builder builder2 = |
| 69 | new DefaultPrometheusTelemetryConfig.DefaultBuilder(); |
| 70 | PrometheusTelemetryConfig.Builder builder3 = |
| 71 | new DefaultPrometheusTelemetryConfig.DefaultBuilder(); |
| 72 | |
| 73 | config1 = builder1 |
| 74 | .withAddress(IP_ADDRESS_1) |
| 75 | .withPort(PORT_1) |
| 76 | .withConfigMap(CONFIG_MAP_1) |
| 77 | .build(); |
| 78 | |
| 79 | sameAsConfig1 = builder2 |
| 80 | .withAddress(IP_ADDRESS_1) |
| 81 | .withPort(PORT_1) |
| 82 | .withConfigMap(CONFIG_MAP_1) |
| 83 | .build(); |
| 84 | |
| 85 | config2 = builder3 |
| 86 | .withAddress(IP_ADDRESS_2) |
| 87 | .withPort(PORT_2) |
| 88 | .withConfigMap(CONFIG_MAP_2) |
| 89 | .build(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests class immutability. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testImmutability() { |
| 97 | assertThatClassIsImmutable(DefaultPrometheusTelemetryConfig.class); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Tests object equality. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testEquality() { |
| 105 | new EqualsTester() |
| 106 | .addEqualityGroup(config1, sameAsConfig1) |
| 107 | .addEqualityGroup(config2).testEquals(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests object construction. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testConstruction() { |
| 115 | PrometheusTelemetryConfig config = config1; |
| 116 | |
| 117 | assertThat(config.address(), is(IP_ADDRESS_1)); |
| 118 | assertThat(config.port(), is(PORT_1)); |
| 119 | assertThat(config.configMap(), is(CONFIG_MAP_1)); |
| 120 | } |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 121 | |
| 122 | /** |
| 123 | * Tests props extraction. |
| 124 | */ |
| 125 | @Test |
| 126 | public void testPropsExtraction() { |
| 127 | Map<String, String> props = Maps.newConcurrentMap(); |
| 128 | props.put(ADDRESS, IP_ADDRESS_1); |
| 129 | props.put(PORT, String.valueOf(PORT_1)); |
| 130 | |
| 131 | TelemetryConfig config = new DefaultTelemetryConfig(DUMMY, PROMETHEUS, |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 132 | ImmutableList.of(), DUMMY, DUMMY, DISABLED, props); |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 133 | |
| 134 | PrometheusTelemetryConfig prometheusConfig = fromTelemetryConfig(config); |
| 135 | assertThat(prometheusConfig.address(), is(IP_ADDRESS_1)); |
| 136 | assertThat(prometheusConfig.port(), is(PORT_1)); |
| 137 | } |
| 138 | } |