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