blob: 1ebd53dc598839a536c1b6126d450a5bb05435db [file] [log] [blame]
boyoung21c5f5f42018-09-27 20:29:41 +09001/*
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 */
16package org.onosproject.openstacktelemetry.config;
17
Jian Li69600e02018-12-24 13:21:18 +090018import com.google.common.collect.ImmutableList;
boyoung21c5f5f42018-09-27 20:29:41 +090019import com.google.common.collect.ImmutableMap;
Jian Li69600e02018-12-24 13:21:18 +090020import com.google.common.collect.Maps;
boyoung21c5f5f42018-09-27 20:29:41 +090021import com.google.common.testing.EqualsTester;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.openstacktelemetry.api.config.PrometheusTelemetryConfig;
Jian Li69600e02018-12-24 13:21:18 +090025import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
Jian Li7fe7eaf2018-12-31 17:00:33 +090026import org.onosproject.openstacktelemetry.api.DefaultTelemetryConfig;
boyoung21c5f5f42018-09-27 20:29:41 +090027
28import java.util.Map;
29
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.is;
32import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Jian Li69600e02018-12-24 13:21:18 +090033import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.PROMETHEUS;
Jian Li667c6eb2019-01-07 23:01:12 +090034import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.DISABLED;
Jian Li69600e02018-12-24 13:21:18 +090035import static org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig.ADDRESS;
36import static org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig.PORT;
37import static org.onosproject.openstacktelemetry.config.DefaultPrometheusTelemetryConfig.fromTelemetryConfig;
boyoung21c5f5f42018-09-27 20:29:41 +090038
39/**
40 * Unit tests for DefaultPrometheusTelemetryConfig class.
41 */
42public class DefaultPrometheusTelemetryConfigTest {
Jian Li69600e02018-12-24 13:21:18 +090043 private static final String IP_ADDRESS_1 = "10.10.1.1";
boyoung21c5f5f42018-09-27 20:29:41 +090044 private static final String IP_ADDRESS_2 = "10.10.1.2";
45
Jian Li69600e02018-12-24 13:21:18 +090046 private static final int PORT_1 = 50050;
47 private static final int PORT_2 = 50051;
boyoung21c5f5f42018-09-27 20:29:41 +090048
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 Li69600e02018-12-24 13:21:18 +090054 private static final String DUMMY = "dummy";
55
boyoung21c5f5f42018-09-27 20:29:41 +090056 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 Li69600e02018-12-24 13:21:18 +0900121
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 Li667c6eb2019-01-07 23:01:12 +0900132 ImmutableList.of(), DUMMY, DUMMY, DISABLED, props);
Jian Li69600e02018-12-24 13:21:18 +0900133
134 PrometheusTelemetryConfig prometheusConfig = fromTelemetryConfig(config);
135 assertThat(prometheusConfig.address(), is(IP_ADDRESS_1));
136 assertThat(prometheusConfig.port(), is(PORT_1));
137 }
138}