Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 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.rest.resources; |
| 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonObject; |
| 20 | import com.eclipsesource.json.JsonValue; |
| 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 24 | import org.junit.Assert; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.osgi.ServiceDirectory; |
| 28 | import org.onlab.osgi.TestServiceDirectory; |
| 29 | import org.onlab.rest.BaseResource; |
| 30 | import org.onosproject.net.DefaultDevice; |
| 31 | import org.onosproject.net.Device; |
| 32 | import org.onosproject.net.Link; |
| 33 | import org.onosproject.net.config.Config; |
| 34 | import org.onosproject.net.config.NetworkConfigService; |
| 35 | import org.onosproject.net.config.NetworkConfigServiceAdapter; |
| 36 | import org.onosproject.net.config.SubjectFactory; |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 37 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 38 | import javax.ws.rs.NotFoundException; |
| 39 | import javax.ws.rs.client.WebTarget; |
| 40 | import java.net.HttpURLConnection; |
| 41 | import java.util.HashSet; |
| 42 | import java.util.Set; |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 43 | |
| 44 | import static org.easymock.EasyMock.createMock; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | import static org.hamcrest.Matchers.containsString; |
| 48 | import static org.hamcrest.Matchers.hasSize; |
| 49 | import static org.hamcrest.Matchers.is; |
| 50 | import static org.hamcrest.Matchers.notNullValue; |
| 51 | import static org.junit.Assert.fail; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for network config web resource. |
| 55 | */ |
| 56 | public class NetworkConfigWebResourceTest extends ResourceTest { |
| 57 | |
| 58 | MockNetworkConfigService mockNetworkConfigService; |
| 59 | |
| 60 | public class MockDeviceConfig extends Config<Device> { |
| 61 | |
| 62 | final String field1Value; |
| 63 | final String field2Value; |
| 64 | |
| 65 | MockDeviceConfig(String value1, String value2) { |
| 66 | field1Value = value1; |
| 67 | field2Value = value2; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public String key() { |
| 72 | return "basic"; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public JsonNode node() { |
| 77 | return new ObjectMapper() |
| 78 | .createObjectNode() |
| 79 | .put("field1", field1Value) |
| 80 | .put("field2", field2Value); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Mock config factory for devices. |
| 86 | */ |
| 87 | private final SubjectFactory<Device> mockDevicesSubjectFactory = |
| 88 | new SubjectFactory<Device>(Device.class, "devices") { |
| 89 | @Override |
| 90 | public Device createSubject(String subjectKey) { |
| 91 | DefaultDevice device = createMock(DefaultDevice.class); |
| 92 | replay(device); |
| 93 | return device; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Class<Device> subjectClass() { |
| 98 | return Device.class; |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * Mock config factory for links. |
| 104 | */ |
| 105 | private final SubjectFactory<Link> mockLinksSubjectFactory = |
| 106 | new SubjectFactory<Link>(Link.class, "links") { |
| 107 | @Override |
| 108 | public Link createSubject(String subjectKey) { |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Class<Link> subjectClass() { |
| 114 | return Link.class; |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | /** |
| 119 | * Mocked config service. |
| 120 | */ |
| 121 | class MockNetworkConfigService extends NetworkConfigServiceAdapter { |
| 122 | |
| 123 | Set devicesSubjects = new HashSet<>(); |
| 124 | Set devicesConfigs = new HashSet<>(); |
| 125 | Set linksSubjects = new HashSet(); |
| 126 | Set linksConfigs = new HashSet<>(); |
| 127 | |
| 128 | @Override |
| 129 | public Set<Class> getSubjectClasses() { |
| 130 | return ImmutableSet.of(Device.class, Link.class); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public SubjectFactory getSubjectFactory(Class subjectClass) { |
| 135 | if (subjectClass == Device.class) { |
| 136 | return mockDevicesSubjectFactory; |
| 137 | } else if (subjectClass == Link.class) { |
| 138 | return mockLinksSubjectFactory; |
| 139 | } |
| 140 | return null; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public SubjectFactory getSubjectFactory(String subjectClassKey) { |
| 145 | if (subjectClassKey.equals("devices")) { |
| 146 | return mockDevicesSubjectFactory; |
| 147 | } else if (subjectClassKey.equals("links")) { |
| 148 | return mockLinksSubjectFactory; |
| 149 | } |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | @SuppressWarnings("unchecked") |
| 154 | @Override |
| 155 | public <S> Set<S> getSubjects(Class<S> subjectClass) { |
| 156 | if (subjectClass == Device.class) { |
| 157 | return devicesSubjects; |
| 158 | } else if (subjectClass == Link.class) { |
| 159 | return linksSubjects; |
| 160 | } |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | @SuppressWarnings("unchecked") |
| 165 | @Override |
| 166 | public <S> Set<? extends Config<S>> getConfigs(S subject) { |
| 167 | if (subject instanceof Device || subject.toString().contains("device")) { |
| 168 | return devicesConfigs; |
| 169 | } else if (subject.toString().contains("link")) { |
| 170 | return linksConfigs; |
| 171 | } |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | @SuppressWarnings("unchecked") |
| 176 | @Override |
| 177 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 178 | |
| 179 | if (configClass == MockDeviceConfig.class) { |
| 180 | return (C) devicesConfigs.toArray()[0]; |
| 181 | } |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public Class<? extends Config> getConfigClass(String subjectClassKey, String configKey) { |
| 187 | return MockDeviceConfig.class; |
| 188 | } |
| 189 | } |
| 190 | |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 191 | /** |
| 192 | * Sets up mocked config service. |
| 193 | */ |
| 194 | @Before |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 195 | public void setUpMocks() { |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 196 | mockNetworkConfigService = new MockNetworkConfigService(); |
| 197 | ServiceDirectory testDirectory = |
| 198 | new TestServiceDirectory() |
Thomas Vachuska | 6f350ed | 2016-01-08 09:53:03 -0800 | [diff] [blame] | 199 | .add(NetworkConfigService.class, mockNetworkConfigService); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 200 | BaseResource.setServiceDirectory(testDirectory); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Sets up test config data. |
| 205 | */ |
| 206 | @SuppressWarnings("unchecked") |
| 207 | public void setUpConfigData() { |
| 208 | mockNetworkConfigService.devicesSubjects.add("device1"); |
| 209 | mockNetworkConfigService.devicesConfigs.add(new MockDeviceConfig("v1", "v2")); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Tests the result of the rest api GET when there are no configs. |
| 214 | */ |
| 215 | @Test |
| 216 | public void testEmptyConfigs() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 217 | final WebTarget wt = target(); |
| 218 | final String response = wt.path("network/configuration").request().get(String.class); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 219 | |
| 220 | assertThat(response, containsString("\"devices\":{}")); |
| 221 | assertThat(response, containsString("\"links\":{}")); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Tests the result of the rest api GET for a single subject with no configs. |
| 226 | */ |
| 227 | @Test |
| 228 | public void testEmptyConfig() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 229 | final WebTarget wt = target(); |
| 230 | final String response = wt.path("network/configuration/devices").request().get(String.class); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 231 | |
| 232 | assertThat(response, is("{}")); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Tests the result of the rest api GET for a single subject that |
| 237 | * is undefined. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testNonExistentConfig() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 241 | final WebTarget wt = target(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 242 | |
| 243 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 244 | final String response = wt.path("network/configuration/nosuchkey").request().get(String.class); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 245 | fail("GET of non-existent key does not produce an exception " + response); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 246 | } catch (NotFoundException e) { |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 247 | assertThat(e.getResponse().getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND)); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | private void checkBasicAttributes(JsonValue basic) { |
| 252 | Assert.assertThat(basic.asObject().get("field1").asString(), is("v1")); |
| 253 | Assert.assertThat(basic.asObject().get("field2").asString(), is("v2")); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Tests the result of the rest api GET when there is a config. |
| 258 | */ |
| 259 | @Test |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 260 | public void testConfigs() { |
| 261 | setUpConfigData(); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 262 | final WebTarget wt = target(); |
| 263 | final String response = wt.path("network/configuration").request().get(String.class); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 264 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 265 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 266 | Assert.assertThat(result, notNullValue()); |
| 267 | |
| 268 | Assert.assertThat(result.names(), hasSize(2)); |
| 269 | |
| 270 | JsonValue devices = result.get("devices"); |
| 271 | Assert.assertThat(devices, notNullValue()); |
| 272 | |
| 273 | JsonValue device1 = devices.asObject().get("device1"); |
| 274 | Assert.assertThat(device1, notNullValue()); |
| 275 | |
| 276 | JsonValue basic = device1.asObject().get("basic"); |
| 277 | Assert.assertThat(basic, notNullValue()); |
| 278 | |
| 279 | checkBasicAttributes(basic); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Tests the result of the rest api single subject key GET when |
| 284 | * there is a config. |
| 285 | */ |
| 286 | @Test |
| 287 | public void testSingleSubjectKeyConfig() { |
| 288 | setUpConfigData(); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 289 | final WebTarget wt = target(); |
| 290 | final String response = wt.path("network/configuration/devices").request().get(String.class); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 291 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 292 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 293 | Assert.assertThat(result, notNullValue()); |
| 294 | |
| 295 | Assert.assertThat(result.names(), hasSize(1)); |
| 296 | |
| 297 | JsonValue device1 = result.asObject().get("device1"); |
| 298 | Assert.assertThat(device1, notNullValue()); |
| 299 | |
| 300 | JsonValue basic = device1.asObject().get("basic"); |
| 301 | Assert.assertThat(basic, notNullValue()); |
| 302 | |
| 303 | checkBasicAttributes(basic); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Tests the result of the rest api single subject GET when |
| 308 | * there is a config. |
| 309 | */ |
| 310 | @Test |
| 311 | public void testSingleSubjectConfig() { |
| 312 | setUpConfigData(); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 313 | final WebTarget wt = target(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 314 | final String response = |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 315 | wt.path("network/configuration/devices/device1") |
| 316 | .request() |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 317 | .get(String.class); |
| 318 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 319 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 320 | Assert.assertThat(result, notNullValue()); |
| 321 | |
| 322 | Assert.assertThat(result.names(), hasSize(1)); |
| 323 | |
| 324 | JsonValue basic = result.asObject().get("basic"); |
| 325 | Assert.assertThat(basic, notNullValue()); |
| 326 | |
| 327 | checkBasicAttributes(basic); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Tests the result of the rest api single subject single config GET when |
| 332 | * there is a config. |
| 333 | */ |
| 334 | @Test |
| 335 | public void testSingleSubjectSingleConfig() { |
| 336 | setUpConfigData(); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 337 | final WebTarget wt = target(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 338 | final String response = |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 339 | wt.path("network/configuration/devices/device1/basic") |
| 340 | .request() |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 341 | .get(String.class); |
| 342 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 343 | final JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 37a5d8c | 2015-11-23 09:40:57 -0800 | [diff] [blame] | 344 | Assert.assertThat(result, notNullValue()); |
| 345 | |
| 346 | Assert.assertThat(result.names(), hasSize(2)); |
| 347 | |
| 348 | checkBasicAttributes(result); |
| 349 | } |
| 350 | |
| 351 | // TODO: Add test for DELETE and POST |
| 352 | } |