blob: 21b7ca420600695e9b971175085dd89de609d328 [file] [log] [blame]
Ray Milkey37a5d8c2015-11-23 09:40:57 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey37a5d8c2015-11-23 09:40:57 -08003 *
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
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonObject;
20import com.eclipsesource.json.JsonValue;
21import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.google.common.collect.ImmutableSet;
Ray Milkey37a5d8c2015-11-23 09:40:57 -080024import org.junit.Assert;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.osgi.ServiceDirectory;
28import org.onlab.osgi.TestServiceDirectory;
29import org.onlab.rest.BaseResource;
30import org.onosproject.net.DefaultDevice;
31import org.onosproject.net.Device;
32import org.onosproject.net.Link;
33import org.onosproject.net.config.Config;
34import org.onosproject.net.config.NetworkConfigService;
35import org.onosproject.net.config.NetworkConfigServiceAdapter;
36import org.onosproject.net.config.SubjectFactory;
Ray Milkey37a5d8c2015-11-23 09:40:57 -080037
Jian Li9d616492016-03-09 10:52:49 -080038import javax.ws.rs.NotFoundException;
39import javax.ws.rs.client.WebTarget;
40import java.net.HttpURLConnection;
41import java.util.HashSet;
42import java.util.Set;
Ray Milkey37a5d8c2015-11-23 09:40:57 -080043
44import static org.easymock.EasyMock.createMock;
45import static org.easymock.EasyMock.replay;
46import static org.hamcrest.MatcherAssert.assertThat;
47import static org.hamcrest.Matchers.containsString;
48import static org.hamcrest.Matchers.hasSize;
49import static org.hamcrest.Matchers.is;
50import static org.hamcrest.Matchers.notNullValue;
51import static org.junit.Assert.fail;
52
53/**
54 * Unit tests for network config web resource.
55 */
56public 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 Milkey37a5d8c2015-11-23 09:40:57 -0800191 /**
192 * Sets up mocked config service.
193 */
194 @Before
Jian Li9d616492016-03-09 10:52:49 -0800195 public void setUpMocks() {
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800196 mockNetworkConfigService = new MockNetworkConfigService();
197 ServiceDirectory testDirectory =
198 new TestServiceDirectory()
Thomas Vachuska6f350ed2016-01-08 09:53:03 -0800199 .add(NetworkConfigService.class, mockNetworkConfigService);
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800200 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 Li9d616492016-03-09 10:52:49 -0800217 final WebTarget wt = target();
218 final String response = wt.path("network/configuration").request().get(String.class);
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800219
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 Li9d616492016-03-09 10:52:49 -0800229 final WebTarget wt = target();
230 final String response = wt.path("network/configuration/devices").request().get(String.class);
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800231
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 Li9d616492016-03-09 10:52:49 -0800241 final WebTarget wt = target();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800242
243 try {
Jian Li9d616492016-03-09 10:52:49 -0800244 final String response = wt.path("network/configuration/nosuchkey").request().get(String.class);
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800245 fail("GET of non-existent key does not produce an exception " + response);
Jian Li9d616492016-03-09 10:52:49 -0800246 } catch (NotFoundException e) {
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800247 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 Milkey37a5d8c2015-11-23 09:40:57 -0800260 public void testConfigs() {
261 setUpConfigData();
Jian Li9d616492016-03-09 10:52:49 -0800262 final WebTarget wt = target();
263 final String response = wt.path("network/configuration").request().get(String.class);
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800264
Jian Li80cfe452016-01-14 16:04:58 -0800265 final JsonObject result = Json.parse(response).asObject();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800266 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 Li9d616492016-03-09 10:52:49 -0800289 final WebTarget wt = target();
290 final String response = wt.path("network/configuration/devices").request().get(String.class);
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800291
Jian Li80cfe452016-01-14 16:04:58 -0800292 final JsonObject result = Json.parse(response).asObject();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800293 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 Li9d616492016-03-09 10:52:49 -0800313 final WebTarget wt = target();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800314 final String response =
Jian Li9d616492016-03-09 10:52:49 -0800315 wt.path("network/configuration/devices/device1")
316 .request()
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800317 .get(String.class);
318
Jian Li80cfe452016-01-14 16:04:58 -0800319 final JsonObject result = Json.parse(response).asObject();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800320 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 Li9d616492016-03-09 10:52:49 -0800337 final WebTarget wt = target();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800338 final String response =
Jian Li9d616492016-03-09 10:52:49 -0800339 wt.path("network/configuration/devices/device1/basic")
340 .request()
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800341 .get(String.class);
342
Jian Li80cfe452016-01-14 16:04:58 -0800343 final JsonObject result = Json.parse(response).asObject();
Ray Milkey37a5d8c2015-11-23 09:40:57 -0800344 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}