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