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.RestTelemetryConfig; |
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.REST; |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 34 | import static org.onosproject.openstacktelemetry.api.config.TelemetryConfig.Status.DISABLED; |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 35 | import static org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig.ADDRESS; |
| 36 | import static org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig.ENDPOINT; |
| 37 | import static org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig.METHOD; |
| 38 | import static org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig.PORT; |
| 39 | import static org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig.REQUEST_MEDIA_TYPE; |
| 40 | import static org.onosproject.openstacktelemetry.config.DefaultRestTelemetryConfig.RESPONSE_MEDIA_TYPE; |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 41 | |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 42 | public final class DefaultRestTelemetryConfigTest { |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 43 | |
| 44 | private static final String IP_ADDRESS_1 = "10.10.10.1"; |
| 45 | private static final String IP_ADDRESS_2 = "20.20.20.1"; |
| 46 | |
| 47 | private static final int PORT_1 = 80; |
| 48 | private static final int PORT_2 = 8080; |
| 49 | |
| 50 | private static final String ENDPOINT_1 = "telemetry"; |
| 51 | private static final String ENDPOINT_2 = "configuration"; |
| 52 | |
| 53 | private static final String METHOD_1 = "POST"; |
| 54 | private static final String METHOD_2 = "GET"; |
| 55 | |
| 56 | private static final String REQUEST_MEDIA_TYPE_1 = "JSON"; |
| 57 | private static final String REQUEST_MEDIA_TYPE_2 = "XML"; |
| 58 | |
| 59 | private static final String RESPONSE_MEDIA_TYPE_1 = "JSON"; |
| 60 | private static final String RESPONSE_MEDIA_TYPE_2 = "XML"; |
| 61 | |
| 62 | private static final Map<String, Object> CONFIG_MAP_1 = |
| 63 | ImmutableMap.of("key1", "value1"); |
| 64 | private static final Map<String, Object> CONFIG_MAP_2 = |
| 65 | ImmutableMap.of("key2", "value2"); |
| 66 | |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 67 | private static final String DUMMY = "dummy"; |
| 68 | |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 69 | private RestTelemetryConfig config1; |
| 70 | private RestTelemetryConfig sameAsConfig1; |
| 71 | private RestTelemetryConfig config2; |
| 72 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 73 | /** |
| 74 | * Initial setup for this unit test. |
| 75 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 76 | @Before |
| 77 | public void setup() { |
| 78 | |
| 79 | RestTelemetryConfig.Builder builder1 = |
| 80 | new DefaultRestTelemetryConfig.DefaultBuilder(); |
| 81 | RestTelemetryConfig.Builder builder2 = |
| 82 | new DefaultRestTelemetryConfig.DefaultBuilder(); |
| 83 | RestTelemetryConfig.Builder builder3 = |
| 84 | new DefaultRestTelemetryConfig.DefaultBuilder(); |
| 85 | |
| 86 | config1 = builder1 |
| 87 | .withAddress(IP_ADDRESS_1) |
| 88 | .withPort(PORT_1) |
| 89 | .withEndpoint(ENDPOINT_1) |
| 90 | .withMethod(METHOD_1) |
| 91 | .withRequestMediaType(REQUEST_MEDIA_TYPE_1) |
| 92 | .withResponseMediaType(RESPONSE_MEDIA_TYPE_1) |
| 93 | .withConfigMap(CONFIG_MAP_1) |
| 94 | .build(); |
| 95 | |
| 96 | sameAsConfig1 = builder2 |
| 97 | .withAddress(IP_ADDRESS_1) |
| 98 | .withPort(PORT_1) |
| 99 | .withEndpoint(ENDPOINT_1) |
| 100 | .withMethod(METHOD_1) |
| 101 | .withRequestMediaType(REQUEST_MEDIA_TYPE_1) |
| 102 | .withResponseMediaType(RESPONSE_MEDIA_TYPE_1) |
| 103 | .withConfigMap(CONFIG_MAP_1) |
| 104 | .build(); |
| 105 | |
| 106 | config2 = builder3 |
| 107 | .withAddress(IP_ADDRESS_2) |
| 108 | .withPort(PORT_2) |
| 109 | .withEndpoint(ENDPOINT_2) |
| 110 | .withMethod(METHOD_2) |
| 111 | .withRequestMediaType(REQUEST_MEDIA_TYPE_2) |
| 112 | .withResponseMediaType(RESPONSE_MEDIA_TYPE_2) |
| 113 | .withConfigMap(CONFIG_MAP_2) |
| 114 | .build(); |
| 115 | } |
| 116 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 117 | /** |
| 118 | * Tests class immutability. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testImmutability() { |
| 122 | assertThatClassIsImmutable(DefaultRestTelemetryConfig.class); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Tests object equality. |
| 127 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 128 | @Test |
| 129 | public void testEquality() { |
| 130 | new EqualsTester() |
| 131 | .addEqualityGroup(config1, sameAsConfig1) |
| 132 | .addEqualityGroup(config2).testEquals(); |
| 133 | } |
| 134 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame] | 135 | /** |
| 136 | * Tests object construction. |
| 137 | */ |
Jian Li | 52c1122 | 2018-06-07 11:39:17 +0900 | [diff] [blame] | 138 | @Test |
| 139 | public void testConstruction() { |
| 140 | RestTelemetryConfig config = config1; |
| 141 | |
| 142 | assertThat(config.address(), is(IP_ADDRESS_1)); |
| 143 | assertThat(config.port(), is(PORT_1)); |
| 144 | assertThat(config.endpoint(), is(ENDPOINT_1)); |
| 145 | assertThat(config.method(), is(METHOD_1)); |
| 146 | assertThat(config.requestMediaType(), is(REQUEST_MEDIA_TYPE_1)); |
| 147 | assertThat(config.responseMediaType(), is(RESPONSE_MEDIA_TYPE_1)); |
| 148 | assertThat(config.configMap(), is(CONFIG_MAP_1)); |
| 149 | } |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Tests props extraction. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testPropsExtraction() { |
| 156 | Map<String, String> props = Maps.newConcurrentMap(); |
| 157 | props.put(ADDRESS, IP_ADDRESS_1); |
| 158 | props.put(PORT, String.valueOf(PORT_1)); |
| 159 | props.put(ENDPOINT, ENDPOINT_1); |
| 160 | props.put(METHOD, METHOD_1); |
| 161 | props.put(REQUEST_MEDIA_TYPE, REQUEST_MEDIA_TYPE_1); |
| 162 | props.put(RESPONSE_MEDIA_TYPE, RESPONSE_MEDIA_TYPE_1); |
| 163 | |
| 164 | TelemetryConfig config = new DefaultTelemetryConfig(DUMMY, REST, |
Jian Li | 667c6eb | 2019-01-07 23:01:12 +0900 | [diff] [blame^] | 165 | ImmutableList.of(), DUMMY, DUMMY, DISABLED, props); |
Jian Li | 69600e0 | 2018-12-24 13:21:18 +0900 | [diff] [blame] | 166 | RestTelemetryConfig restConfig = DefaultRestTelemetryConfig.fromTelemetryConfig(config); |
| 167 | assertThat(restConfig.address(), is(IP_ADDRESS_1)); |
| 168 | assertThat(restConfig.port(), is(PORT_1)); |
| 169 | assertThat(restConfig.endpoint(), is(ENDPOINT_1)); |
| 170 | assertThat(restConfig.method(), is(METHOD_1)); |
| 171 | assertThat(restConfig.requestMediaType(), is(REQUEST_MEDIA_TYPE_1)); |
| 172 | assertThat(restConfig.responseMediaType(), is(RESPONSE_MEDIA_TYPE_1)); |
| 173 | } |
| 174 | } |