Jian Li | 52c1122 | 2018-06-07 11:39:17 +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; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +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; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +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.GrpcTelemetryConfig; |
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; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 27 | |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 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.GRPC; |
| 34 | import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.ADDRESS; |
| 35 | import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.MAX_INBOUND_MSG_SIZE; |
| 36 | import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.PORT; |
| 37 | import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.USE_PLAINTEXT; |
| 38 | import static org.onosproject.openstacktelemetry.config.DefaultGrpcTelemetryConfig.fromTelemetryConfig; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Unit tests for DefaultGrpcTelemetryConfig class. |
| 42 | */ |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 43 | public final class DefaultGrpcTelemetryConfigTest { |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 44 | |
| 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 Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 58 | ImmutableMap.of("key1", "value1"); |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 59 | private static final Map<String, Object> CONFIG_MAP_2 = |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 60 | ImmutableMap.of("key2", "value2"); |
| 61 | |
| 62 | private static final String DUMMY = "dummy"; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 63 | |
| 64 | private GrpcTelemetryConfig config1; |
| 65 | private GrpcTelemetryConfig sameAsConfig1; |
| 66 | private GrpcTelemetryConfig config2; |
| 67 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 68 | /** |
| 69 | * Initial setup for this unit test. |
| 70 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 71 | @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 Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 106 | /** |
| 107 | * Tests class immutability. |
| 108 | */ |
| 109 | @Test |
| 110 | public void testImmutability() { |
| 111 | assertThatClassIsImmutable(DefaultGrpcTelemetryConfig.class); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Tests object equality. |
| 116 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 117 | @Test |
| 118 | public void testEquality() { |
| 119 | new EqualsTester() |
| 120 | .addEqualityGroup(config1, sameAsConfig1) |
| 121 | .addEqualityGroup(config2).testEquals(); |
| 122 | } |
| 123 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 124 | /** |
| 125 | * Tests object construction. |
| 126 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 127 | @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 Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 137 | |
| 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 | } |