Ray Milkey
Committed by Gerrit Code Review

Unit tests for network config REST API.

Change-Id: I1b09035ff7aaa463889f62bbc9709a37a920d800
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 +
18 +import java.net.HttpURLConnection;
19 +import java.util.HashSet;
20 +import java.util.Set;
21 +
22 +import org.junit.Assert;
23 +import org.junit.Before;
24 +import org.junit.Test;
25 +import org.onlab.osgi.ServiceDirectory;
26 +import org.onlab.osgi.TestServiceDirectory;
27 +import org.onlab.rest.BaseResource;
28 +import org.onosproject.net.DefaultDevice;
29 +import org.onosproject.net.Device;
30 +import org.onosproject.net.Link;
31 +import org.onosproject.net.config.Config;
32 +import org.onosproject.net.config.NetworkConfigService;
33 +import org.onosproject.net.config.NetworkConfigServiceAdapter;
34 +import org.onosproject.net.config.SubjectFactory;
35 +import org.onosproject.rest.ResourceTest;
36 +
37 +import com.eclipsesource.json.JsonObject;
38 +import com.eclipsesource.json.JsonValue;
39 +import com.fasterxml.jackson.databind.JsonNode;
40 +import com.fasterxml.jackson.databind.ObjectMapper;
41 +import com.google.common.collect.ImmutableSet;
42 +import com.sun.jersey.api.client.UniformInterfaceException;
43 +import com.sun.jersey.api.client.WebResource;
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 +
192 + /**
193 + * Sets up mocked config service.
194 + */
195 + @Before
196 + public void setUp() {
197 + mockNetworkConfigService = new MockNetworkConfigService();
198 + ServiceDirectory testDirectory =
199 + new TestServiceDirectory()
200 + .add(NetworkConfigService.class, mockNetworkConfigService);
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() {
218 + final WebResource rs = resource();
219 + final String response = rs.path("network/configuration").get(String.class);
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() {
230 + final WebResource rs = resource();
231 + final String response = rs.path("network/configuration/devices").get(String.class);
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() {
242 + final WebResource rs = resource();
243 +
244 + try {
245 + final String response = rs.path("network/configuration/nosuchkey").get(String.class);
246 + fail("GET of non-existent key does not produce an exception " + response);
247 + } catch (UniformInterfaceException e) {
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
261 +
262 + public void testConfigs() {
263 + setUpConfigData();
264 + final WebResource rs = resource();
265 + final String response = rs.path("network/configuration").get(String.class);
266 +
267 + final JsonObject result = JsonObject.readFrom(response);
268 + Assert.assertThat(result, notNullValue());
269 +
270 + Assert.assertThat(result.names(), hasSize(2));
271 +
272 + JsonValue devices = result.get("devices");
273 + Assert.assertThat(devices, notNullValue());
274 +
275 + JsonValue device1 = devices.asObject().get("device1");
276 + Assert.assertThat(device1, notNullValue());
277 +
278 + JsonValue basic = device1.asObject().get("basic");
279 + Assert.assertThat(basic, notNullValue());
280 +
281 + checkBasicAttributes(basic);
282 + }
283 +
284 + /**
285 + * Tests the result of the rest api single subject key GET when
286 + * there is a config.
287 + */
288 + @Test
289 + public void testSingleSubjectKeyConfig() {
290 + setUpConfigData();
291 + final WebResource rs = resource();
292 + final String response = rs.path("network/configuration/devices").get(String.class);
293 +
294 + final JsonObject result = JsonObject.readFrom(response);
295 + Assert.assertThat(result, notNullValue());
296 +
297 + Assert.assertThat(result.names(), hasSize(1));
298 +
299 + JsonValue device1 = result.asObject().get("device1");
300 + Assert.assertThat(device1, notNullValue());
301 +
302 + JsonValue basic = device1.asObject().get("basic");
303 + Assert.assertThat(basic, notNullValue());
304 +
305 + checkBasicAttributes(basic);
306 + }
307 +
308 + /**
309 + * Tests the result of the rest api single subject GET when
310 + * there is a config.
311 + */
312 + @Test
313 + public void testSingleSubjectConfig() {
314 + setUpConfigData();
315 + final WebResource rs = resource();
316 + final String response =
317 + rs.path("network/configuration/devices/device1")
318 + .get(String.class);
319 +
320 + final JsonObject result = JsonObject.readFrom(response);
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();
338 + final WebResource rs = resource();
339 + final String response =
340 + rs.path("network/configuration/devices/device1/basic")
341 + .get(String.class);
342 +
343 + final JsonObject result = JsonObject.readFrom(response);
344 + 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 +}