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