blob: e4dc795d08215da84b868474a8a5eba86a7078f2 [file] [log] [blame]
Jian Li52c11222018-06-07 11:39:17 +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;
Jian Li52c11222018-06-07 11:39:17 +090019import com.google.common.collect.ImmutableMap;
Jian Li69600e02018-12-24 13:21:18 +090020import com.google.common.collect.Maps;
Jian Li52c11222018-06-07 11:39:17 +090021import com.google.common.testing.EqualsTester;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.openstacktelemetry.api.config.GrpcTelemetryConfig;
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;
Jian Li52c11222018-06-07 11:39:17 +090027
28import java.util.Map;
29
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.is;
Jian Liae3fcff2018-07-30 11:55:44 +090032import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
Jian Li69600e02018-12-24 13:21:18 +090033import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType.GRPC;
Jian Li667c6eb2019-01-07 23:01:12 +090034import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.ENABLED;
Jian Li69600e02018-12-24 13:21:18 +090035import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.ADDRESS;
36import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.MAX_INBOUND_MSG_SIZE;
37import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.PORT;
38import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.USE_PLAINTEXT;
39import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.fromTelemetryConfig;
Jian Li52c11222018-06-07 11:39:17 +090040
41/**
42 * Unit tests for DefaultGrpcTelemetryConfig class.
43 */
Jian Li3db90852018-06-10 22:29:16 +090044public final class DefaultGrpcTelemetryConfigTest {
Jian Li52c11222018-06-07 11:39:17 +090045
46 private static final String IP_ADDRESS_1 = "10.10.10.1";
47 private static final String IP_ADDRESS_2 = "20.20.20.1";
48
49 private static final int PORT_1 = 80;
50 private static final int PORT_2 = 8080;
51
52 private static final int MSG_SIZE_1 = 1000;
53 private static final int MSG_SIZE_2 = 2000;
54
55 private static final boolean USE_PLAIN_TEXT_1 = true;
56 private static final boolean USE_PLAIN_TEXT_2 = true;
57
58 private static final Map<String, Object> CONFIG_MAP_1 =
Jian Li69600e02018-12-24 13:21:18 +090059 ImmutableMap.of("key1", "value1");
Jian Li52c11222018-06-07 11:39:17 +090060 private static final Map<String, Object> CONFIG_MAP_2 =
Jian Li69600e02018-12-24 13:21:18 +090061 ImmutableMap.of("key2", "value2");
62
63 private static final String DUMMY = "dummy";
Jian Li52c11222018-06-07 11:39:17 +090064
65 private GrpcTelemetryConfig config1;
66 private GrpcTelemetryConfig sameAsConfig1;
67 private GrpcTelemetryConfig config2;
68
Jian Liae3fcff2018-07-30 11:55:44 +090069 /**
70 * Initial setup for this unit test.
71 */
Jian Li52c11222018-06-07 11:39:17 +090072 @Before
73 public void setup() {
74
75 GrpcTelemetryConfig.Builder builder1 =
76 new DefaultGrpcTelemetryConfig.DefaultBuilder();
77 GrpcTelemetryConfig.Builder builder2 =
78 new DefaultGrpcTelemetryConfig.DefaultBuilder();
79 GrpcTelemetryConfig.Builder builder3 =
80 new DefaultGrpcTelemetryConfig.DefaultBuilder();
81
82 config1 = builder1
83 .withAddress(IP_ADDRESS_1)
84 .withPort(PORT_1)
85 .withMaxInboundMsgSize(MSG_SIZE_1)
86 .withUsePlaintext(USE_PLAIN_TEXT_1)
87 .withConfigMap(CONFIG_MAP_1)
88 .build();
89
90 sameAsConfig1 = builder2
91 .withAddress(IP_ADDRESS_1)
92 .withPort(PORT_1)
93 .withMaxInboundMsgSize(MSG_SIZE_1)
94 .withUsePlaintext(USE_PLAIN_TEXT_1)
95 .withConfigMap(CONFIG_MAP_1)
96 .build();
97
98 config2 = builder3
99 .withAddress(IP_ADDRESS_2)
100 .withPort(PORT_2)
101 .withMaxInboundMsgSize(MSG_SIZE_2)
102 .withUsePlaintext(USE_PLAIN_TEXT_2)
103 .withConfigMap(CONFIG_MAP_2)
104 .build();
105 }
106
Jian Liae3fcff2018-07-30 11:55:44 +0900107 /**
108 * Tests class immutability.
109 */
110 @Test
111 public void testImmutability() {
112 assertThatClassIsImmutable(DefaultGrpcTelemetryConfig.class);
113 }
114
115 /**
116 * Tests object equality.
117 */
Jian Li52c11222018-06-07 11:39:17 +0900118 @Test
119 public void testEquality() {
120 new EqualsTester()
121 .addEqualityGroup(config1, sameAsConfig1)
122 .addEqualityGroup(config2).testEquals();
123 }
124
Jian Liae3fcff2018-07-30 11:55:44 +0900125 /**
126 * Tests object construction.
127 */
Jian Li52c11222018-06-07 11:39:17 +0900128 @Test
129 public void testConstruction() {
130 GrpcTelemetryConfig config = config1;
131
132 assertThat(config.address(), is(IP_ADDRESS_1));
133 assertThat(config.port(), is(PORT_1));
134 assertThat(config.maxInboundMsgSize(), is(MSG_SIZE_1));
135 assertThat(config.usePlaintext(), is(USE_PLAIN_TEXT_1));
136 assertThat(config.configMap(), is(CONFIG_MAP_1));
137 }
Jian Li69600e02018-12-24 13:21:18 +0900138
139 /**
140 * Tests props extraction.
141 */
142 @Test
143 public void testPropsExtraction() {
144 Map<String, String> props = Maps.newConcurrentMap();
145 props.put(ADDRESS, IP_ADDRESS_1);
146 props.put(PORT, String.valueOf(PORT_1));
147 props.put(MAX_INBOUND_MSG_SIZE, String.valueOf(MSG_SIZE_1));
148 props.put(USE_PLAINTEXT, String.valueOf(USE_PLAIN_TEXT_1));
149 TelemetryConfig config = new DefaultTelemetryConfig(DUMMY, GRPC,
Jian Li667c6eb2019-01-07 23:01:12 +0900150 ImmutableList.of(), DUMMY, DUMMY, ENABLED, props);
Jian Li69600e02018-12-24 13:21:18 +0900151
152 GrpcTelemetryConfig grpcConfig = fromTelemetryConfig(config);
153 assertThat(grpcConfig.address(), is(IP_ADDRESS_1));
154 assertThat(grpcConfig.port(), is(PORT_1));
155 assertThat(grpcConfig.maxInboundMsgSize(), is(MSG_SIZE_1));
156 assertThat(grpcConfig.usePlaintext(), is(USE_PLAIN_TEXT_1));
157 }
158}