blob: c56247f8e0035879e1e4e1b690600f5b93dad7f8 [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;
26import org.onosproject.openstacktelemetry.impl.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;
34import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.ADDRESS;
35import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.MAX_INBOUND_MSG_SIZE;
36import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.PORT;
37import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.USE_PLAINTEXT;
38import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.fromTelemetryConfig;
Jian Li52c11222018-06-07 11:39:17 +090039
40/**
41 * Unit tests for DefaultGrpcTelemetryConfig class.
42 */
Jian Li3db90852018-06-10 22:29:16 +090043public final class DefaultGrpcTelemetryConfigTest {
Jian Li52c11222018-06-07 11:39:17 +090044
45 private static final String IP_ADDRESS_1 = "10.10.10.1";
46 private static final String IP_ADDRESS_2 = "20.20.20.1";
47
48 private static final int PORT_1 = 80;
49 private static final int PORT_2 = 8080;
50
51 private static final int MSG_SIZE_1 = 1000;
52 private static final int MSG_SIZE_2 = 2000;
53
54 private static final boolean USE_PLAIN_TEXT_1 = true;
55 private static final boolean USE_PLAIN_TEXT_2 = true;
56
57 private static final Map<String, Object> CONFIG_MAP_1 =
Jian Li69600e02018-12-24 13:21:18 +090058 ImmutableMap.of("key1", "value1");
Jian Li52c11222018-06-07 11:39:17 +090059 private static final Map<String, Object> CONFIG_MAP_2 =
Jian Li69600e02018-12-24 13:21:18 +090060 ImmutableMap.of("key2", "value2");
61
62 private static final String DUMMY = "dummy";
Jian Li52c11222018-06-07 11:39:17 +090063
64 private GrpcTelemetryConfig config1;
65 private GrpcTelemetryConfig sameAsConfig1;
66 private GrpcTelemetryConfig config2;
67
Jian Liae3fcff2018-07-30 11:55:44 +090068 /**
69 * Initial setup for this unit test.
70 */
Jian Li52c11222018-06-07 11:39:17 +090071 @Before
72 public void setup() {
73
74 GrpcTelemetryConfig.Builder builder1 =
75 new DefaultGrpcTelemetryConfig.DefaultBuilder();
76 GrpcTelemetryConfig.Builder builder2 =
77 new DefaultGrpcTelemetryConfig.DefaultBuilder();
78 GrpcTelemetryConfig.Builder builder3 =
79 new DefaultGrpcTelemetryConfig.DefaultBuilder();
80
81 config1 = builder1
82 .withAddress(IP_ADDRESS_1)
83 .withPort(PORT_1)
84 .withMaxInboundMsgSize(MSG_SIZE_1)
85 .withUsePlaintext(USE_PLAIN_TEXT_1)
86 .withConfigMap(CONFIG_MAP_1)
87 .build();
88
89 sameAsConfig1 = builder2
90 .withAddress(IP_ADDRESS_1)
91 .withPort(PORT_1)
92 .withMaxInboundMsgSize(MSG_SIZE_1)
93 .withUsePlaintext(USE_PLAIN_TEXT_1)
94 .withConfigMap(CONFIG_MAP_1)
95 .build();
96
97 config2 = builder3
98 .withAddress(IP_ADDRESS_2)
99 .withPort(PORT_2)
100 .withMaxInboundMsgSize(MSG_SIZE_2)
101 .withUsePlaintext(USE_PLAIN_TEXT_2)
102 .withConfigMap(CONFIG_MAP_2)
103 .build();
104 }
105
Jian Liae3fcff2018-07-30 11:55:44 +0900106 /**
107 * Tests class immutability.
108 */
109 @Test
110 public void testImmutability() {
111 assertThatClassIsImmutable(DefaultGrpcTelemetryConfig.class);
112 }
113
114 /**
115 * Tests object equality.
116 */
Jian Li52c11222018-06-07 11:39:17 +0900117 @Test
118 public void testEquality() {
119 new EqualsTester()
120 .addEqualityGroup(config1, sameAsConfig1)
121 .addEqualityGroup(config2).testEquals();
122 }
123
Jian Liae3fcff2018-07-30 11:55:44 +0900124 /**
125 * Tests object construction.
126 */
Jian Li52c11222018-06-07 11:39:17 +0900127 @Test
128 public void testConstruction() {
129 GrpcTelemetryConfig config = config1;
130
131 assertThat(config.address(), is(IP_ADDRESS_1));
132 assertThat(config.port(), is(PORT_1));
133 assertThat(config.maxInboundMsgSize(), is(MSG_SIZE_1));
134 assertThat(config.usePlaintext(), is(USE_PLAIN_TEXT_1));
135 assertThat(config.configMap(), is(CONFIG_MAP_1));
136 }
Jian Li69600e02018-12-24 13:21:18 +0900137
138 /**
139 * Tests props extraction.
140 */
141 @Test
142 public void testPropsExtraction() {
143 Map<String, String> props = Maps.newConcurrentMap();
144 props.put(ADDRESS, IP_ADDRESS_1);
145 props.put(PORT, String.valueOf(PORT_1));
146 props.put(MAX_INBOUND_MSG_SIZE, String.valueOf(MSG_SIZE_1));
147 props.put(USE_PLAINTEXT, String.valueOf(USE_PLAIN_TEXT_1));
148 TelemetryConfig config = new DefaultTelemetryConfig(DUMMY, GRPC,
149 ImmutableList.of(), DUMMY, DUMMY, false, props);
150
151 GrpcTelemetryConfig grpcConfig = fromTelemetryConfig(config);
152 assertThat(grpcConfig.address(), is(IP_ADDRESS_1));
153 assertThat(grpcConfig.port(), is(PORT_1));
154 assertThat(grpcConfig.maxInboundMsgSize(), is(MSG_SIZE_1));
155 assertThat(grpcConfig.usePlaintext(), is(USE_PLAIN_TEXT_1));
156 }
157}