blob: 40470d97f170a8a4e5c96b0bf0adf876ed0fe379 [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
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
Thomas Vachuskaa026be72015-12-07 16:00:37 -0800192 public NetworkConfigWebResourceTest() {
193 super(CoreWebApplication.class);
194 }
195
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800196 /**
197 * Sets up mocked config service.
198 */
199 @Before
200 public void setUp() {
201 mockNetworkConfigService = new MockNetworkConfigService();
202 ServiceDirectory testDirectory =
203 new TestServiceDirectory()
204 .add(NetworkConfigService.class, mockNetworkConfigService);
205 BaseResource.setServiceDirectory(testDirectory);
206 }
207
208 /**
209 * Sets up test config data.
210 */
211 @SuppressWarnings("unchecked")
212 public void setUpConfigData() {
213 mockNetworkConfigService.devicesSubjects.add("device1");
214 mockNetworkConfigService.devicesConfigs.add(new MockDeviceConfig("v1", "v2"));
215 }
216
217 /**
218 * Tests the result of the rest api GET when there are no configs.
219 */
220 @Test
221 public void testEmptyConfigs() {
222 final WebResource rs = resource();
223 final String response = rs.path("network/configuration").get(String.class);
224
225 assertThat(response, containsString("\"devices\":{}"));
226 assertThat(response, containsString("\"links\":{}"));
227 }
228
229 /**
230 * Tests the result of the rest api GET for a single subject with no configs.
231 */
232 @Test
233 public void testEmptyConfig() {
234 final WebResource rs = resource();
235 final String response = rs.path("network/configuration/devices").get(String.class);
236
237 assertThat(response, is("{}"));
238 }
239
240 /**
241 * Tests the result of the rest api GET for a single subject that
242 * is undefined.
243 */
244 @Test
245 public void testNonExistentConfig() {
246 final WebResource rs = resource();
247
248 try {
249 final String response = rs.path("network/configuration/nosuchkey").get(String.class);
250 fail("GET of non-existent key does not produce an exception " + response);
251 } catch (UniformInterfaceException e) {
252 assertThat(e.getResponse().getStatus(), is(HttpURLConnection.HTTP_NOT_FOUND));
253 }
254 }
255
256 private void checkBasicAttributes(JsonValue basic) {
257 Assert.assertThat(basic.asObject().get("field1").asString(), is("v1"));
258 Assert.assertThat(basic.asObject().get("field2").asString(), is("v2"));
259 }
260
261 /**
262 * Tests the result of the rest api GET when there is a config.
263 */
264 @Test
265
266 public void testConfigs() {
267 setUpConfigData();
268 final WebResource rs = resource();
269 final String response = rs.path("network/configuration").get(String.class);
270
271 final JsonObject result = JsonObject.readFrom(response);
272 Assert.assertThat(result, notNullValue());
273
274 Assert.assertThat(result.names(), hasSize(2));
275
276 JsonValue devices = result.get("devices");
277 Assert.assertThat(devices, notNullValue());
278
279 JsonValue device1 = devices.asObject().get("device1");
280 Assert.assertThat(device1, notNullValue());
281
282 JsonValue basic = device1.asObject().get("basic");
283 Assert.assertThat(basic, notNullValue());
284
285 checkBasicAttributes(basic);
286 }
287
288 /**
289 * Tests the result of the rest api single subject key GET when
290 * there is a config.
291 */
292 @Test
293 public void testSingleSubjectKeyConfig() {
294 setUpConfigData();
295 final WebResource rs = resource();
296 final String response = rs.path("network/configuration/devices").get(String.class);
297
298 final JsonObject result = JsonObject.readFrom(response);
299 Assert.assertThat(result, notNullValue());
300
301 Assert.assertThat(result.names(), hasSize(1));
302
303 JsonValue device1 = result.asObject().get("device1");
304 Assert.assertThat(device1, notNullValue());
305
306 JsonValue basic = device1.asObject().get("basic");
307 Assert.assertThat(basic, notNullValue());
308
309 checkBasicAttributes(basic);
310 }
311
312 /**
313 * Tests the result of the rest api single subject GET when
314 * there is a config.
315 */
316 @Test
317 public void testSingleSubjectConfig() {
318 setUpConfigData();
319 final WebResource rs = resource();
320 final String response =
321 rs.path("network/configuration/devices/device1")
322 .get(String.class);
323
324 final JsonObject result = JsonObject.readFrom(response);
325 Assert.assertThat(result, notNullValue());
326
327 Assert.assertThat(result.names(), hasSize(1));
328
329 JsonValue basic = result.asObject().get("basic");
330 Assert.assertThat(basic, notNullValue());
331
332 checkBasicAttributes(basic);
333 }
334
335 /**
336 * Tests the result of the rest api single subject single config GET when
337 * there is a config.
338 */
339 @Test
340 public void testSingleSubjectSingleConfig() {
341 setUpConfigData();
342 final WebResource rs = resource();
343 final String response =
344 rs.path("network/configuration/devices/device1/basic")
345 .get(String.class);
346
347 final JsonObject result = JsonObject.readFrom(response);
348 Assert.assertThat(result, notNullValue());
349
350 Assert.assertThat(result.names(), hasSize(2));
351
352 checkBasicAttributes(result);
353 }
354
355 // TODO: Add test for DELETE and POST
356}