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