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