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