blob: 274a8f224712665783f968a08bd74109c1cc4d79 [file] [log] [blame]
Ray Milkey8fd68ca2015-01-27 15:19:09 -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;
17
18import org.hamcrest.Description;
19import org.hamcrest.TypeSafeMatcher;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.rest.BaseResource;
26import org.onosproject.codec.CodecService;
27import org.onosproject.codec.impl.CodecManager;
28import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Link;
31import org.onosproject.net.link.LinkService;
32
33import com.eclipsesource.json.JsonArray;
34import com.eclipsesource.json.JsonObject;
35import com.google.common.collect.ImmutableList;
36import com.google.common.collect.ImmutableSet;
37import com.sun.jersey.api.client.WebResource;
Ray Milkey8fd68ca2015-01-27 15:19:09 -080038
39import static org.easymock.EasyMock.createMock;
40import static org.easymock.EasyMock.expect;
41import static org.easymock.EasyMock.isA;
42import static org.easymock.EasyMock.replay;
43import static org.easymock.EasyMock.verify;
44import static org.hamcrest.Matchers.containsString;
45import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.is;
47import static org.hamcrest.Matchers.notNullValue;
48import static org.junit.Assert.assertThat;
49import static org.onosproject.net.NetTestTools.link;
50
51/**
52 * Unit tests for links REST APIs.
53 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080054public class LinksResourceTest extends ResourceTest {
Ray Milkey8fd68ca2015-01-27 15:19:09 -080055 LinkService mockLinkService;
56
57 Link link1 = link("src1", 1, "dst1", 1);
58 Link link2 = link("src2", 2, "dst2", 2);
59 Link link3 = link("src3", 3, "dst3", 3);
60
61 /**
Ray Milkey8fd68ca2015-01-27 15:19:09 -080062 * Hamcrest matcher to check that an link representation in JSON matches
63 * the actual link.
64 */
65 public static class LinkJsonMatcher extends TypeSafeMatcher<JsonObject> {
66 private final Link link;
67 private String reason = "";
68
69 public LinkJsonMatcher(Link linkValue) {
70 link = linkValue;
71 }
72
73 @Override
74 public boolean matchesSafely(JsonObject jsonLink) {
75 JsonObject jsonSrc = jsonLink.get("src").asObject();
76 String jsonSrcDevice = jsonSrc.get("device").asString();
77 String jsonSrcPort = jsonSrc.get("port").asString();
78
79 JsonObject jsonDst = jsonLink.get("dst").asObject();
80 String jsonDstDevice = jsonDst.get("device").asString();
81 String jsonDstPort = jsonDst.get("port").asString();
82
83 return jsonSrcDevice.equals(link.src().deviceId().toString()) &&
84 jsonSrcPort.equals(link.src().port().toString()) &&
85 jsonDstDevice.equals(link.dst().deviceId().toString()) &&
86 jsonDstPort.equals(link.dst().port().toString());
87 }
88
89 @Override
90 public void describeTo(Description description) {
91 description.appendText(reason);
92 }
93 }
94
95 /**
96 * Factory to allocate an link matcher.
97 *
98 * @param link link object we are looking for
99 * @return matcher
100 */
101 private static LinkJsonMatcher matchesLink(Link link) {
102 return new LinkJsonMatcher(link);
103 }
104
105 /**
106 * Hamcrest matcher to check that an link is represented properly in a JSON
107 * array of links.
108 */
109 private static class LinkJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
110 private final Link link;
111 private String reason = "";
112
113 public LinkJsonArrayMatcher(Link linkValue) {
114 link = linkValue;
115 }
116
117 @Override
118 public boolean matchesSafely(JsonArray json) {
119 final int expectedAttributes = 2;
120
121 for (int jsonLinkIndex = 0; jsonLinkIndex < json.size();
122 jsonLinkIndex++) {
123
124 JsonObject jsonLink = json.get(jsonLinkIndex).asObject();
125
126 if (jsonLink.names().size() != expectedAttributes) {
127 reason = "Found a link with the wrong number of attributes";
128 return false;
129 }
130
131 if (matchesLink(link).matchesSafely(jsonLink)) {
132 return true;
133 }
134 }
135 return false;
136 }
137
138 @Override
139 public void describeTo(Description description) {
140 description.appendText(reason);
141 }
142 }
143
144 /**
145 * Factory to allocate an link array matcher.
146 *
147 * @param link link object we are looking for
148 * @return matcher
149 */
150 private static LinkJsonArrayMatcher hasLink(Link link) {
151 return new LinkJsonArrayMatcher(link);
152 }
153
154
155 @Before
156 public void setUp() {
157 mockLinkService = createMock(LinkService.class);
158
159 // Register the services needed for the test
160 CodecManager codecService = new CodecManager();
161 codecService.activate();
162 ServiceDirectory testDirectory =
163 new TestServiceDirectory()
164 .add(LinkService.class, mockLinkService)
165 .add(CodecService.class, codecService);
166
167 BaseResource.setServiceDirectory(testDirectory);
168 }
169
170 @After
171 public void tearDown() throws Exception {
172 super.tearDown();
173 verify(mockLinkService);
174 }
175
176 /**
177 * Tests the result of the rest api GET when there are no links.
178 */
179 @Test
180 public void testLinksEmptyArray() {
181 expect(mockLinkService.getLinks()).andReturn(ImmutableList.of());
182 replay(mockLinkService);
183
184 WebResource rs = resource();
185 String response = rs.path("links").get(String.class);
186 assertThat(response, is("{\"links\":[]}"));
187 }
188
189 /**
190 * Tests the result of the rest api GET when there are links present.
191 */
192 @Test
193 public void testLinks() {
194 expect(mockLinkService.getLinks())
195 .andReturn(ImmutableList.of(link1, link2, link3))
196 .anyTimes();
197
198 replay(mockLinkService);
199
200 WebResource rs = resource();
201 String response = rs.path("links").get(String.class);
202 assertThat(response, containsString("{\"links\":["));
203
204 JsonObject result = JsonObject.readFrom(response);
205 assertThat(result, notNullValue());
206
207 assertThat(result.names(), hasSize(1));
208 assertThat(result.names().get(0), is("links"));
209
210 JsonArray jsonLinks = result.get("links").asArray();
211 assertThat(jsonLinks, notNullValue());
212 assertThat(jsonLinks.size(), is(3));
213
214 assertThat(jsonLinks, hasLink(link1));
215 assertThat(jsonLinks, hasLink(link2));
216 assertThat(jsonLinks, hasLink(link3));
217 }
218
219 /**
220 * Tests the result of the rest api GET of links for a specific device.
221 */
222 @Test
223 public void testLinksByDevice() {
224 expect(mockLinkService.getDeviceLinks(isA(DeviceId.class)))
225 .andReturn(ImmutableSet.of(link2))
226 .anyTimes();
227
228 replay(mockLinkService);
229
230 WebResource rs = resource();
231 String response = rs
232 .path("links")
233 .queryParam("device", "src2")
234 .get(String.class);
235 assertThat(response, containsString("{\"links\":["));
236
237 JsonObject result = JsonObject.readFrom(response);
238 assertThat(result, notNullValue());
239
240 assertThat(result.names(), hasSize(1));
241 assertThat(result.names().get(0), is("links"));
242
243 JsonArray jsonLinks = result.get("links").asArray();
244 assertThat(jsonLinks, notNullValue());
245 assertThat(jsonLinks.size(), is(1));
246
247 assertThat(jsonLinks, hasLink(link2));
248 }
249
250 /**
251 * Tests the result of the rest api GET of links for a specific device
252 * and port.
253 */
254 @Test
255 public void testLinksByDevicePort() {
256
257 expect(mockLinkService.getLinks(isA(ConnectPoint.class)))
258 .andReturn(ImmutableSet.of(link2))
259 .anyTimes();
260
261 replay(mockLinkService);
262
263 WebResource rs = resource();
264 String response = rs
265 .path("links")
266 .queryParam("device", "src2")
267 .queryParam("port", "2")
268 .get(String.class);
269 assertThat(response, containsString("{\"links\":["));
270
271 JsonObject result = JsonObject.readFrom(response);
272 assertThat(result, notNullValue());
273
274 assertThat(result.names(), hasSize(1));
275 assertThat(result.names().get(0), is("links"));
276
277 JsonArray jsonLinks = result.get("links").asArray();
278 assertThat(jsonLinks, notNullValue());
279 assertThat(jsonLinks.size(), is(1));
280
281 assertThat(jsonLinks, hasLink(link2));
282 }
283
284 /**
285 * Tests the result of the rest api GET of links for a specific
286 * device, port, and direction.
287 */
288 @Test
289 public void testLinksByDevicePortDirection() {
290
291 expect(mockLinkService.getIngressLinks(isA(ConnectPoint.class)))
292 .andReturn(ImmutableSet.of(link2))
293 .anyTimes();
294
295 replay(mockLinkService);
296
297 WebResource rs = resource();
298 String response = rs
299 .path("links")
300 .queryParam("device", "src2")
301 .queryParam("port", "2")
302 .queryParam("direction", "INGRESS")
303 .get(String.class);
304 assertThat(response, containsString("{\"links\":["));
305
306 JsonObject result = JsonObject.readFrom(response);
307 assertThat(result, notNullValue());
308
309 assertThat(result.names(), hasSize(1));
310 assertThat(result.names().get(0), is("links"));
311
312 JsonArray jsonLinks = result.get("links").asArray();
313 assertThat(jsonLinks, notNullValue());
314 assertThat(jsonLinks.size(), is(1));
315
316 assertThat(jsonLinks, hasLink(link2));
317 }
318
319 /**
320 * Tests the result of the rest api GET of links for a specific
321 * device and direction.
322 */
323 @Test
324 public void testLinksByDeviceDirection() {
325
326 expect(mockLinkService.getDeviceIngressLinks(isA(DeviceId.class)))
327 .andReturn(ImmutableSet.of(link2))
328 .anyTimes();
329
330 replay(mockLinkService);
331
332 WebResource rs = resource();
333 String response = rs
334 .path("links")
335 .queryParam("device", "src2")
336 .queryParam("direction", "INGRESS")
337 .get(String.class);
338 assertThat(response, containsString("{\"links\":["));
339
340 JsonObject result = JsonObject.readFrom(response);
341 assertThat(result, notNullValue());
342
343 assertThat(result.names(), hasSize(1));
344 assertThat(result.names().get(0), is("links"));
345
346 JsonArray jsonLinks = result.get("links").asArray();
347 assertThat(jsonLinks, notNullValue());
348 assertThat(jsonLinks.size(), is(1));
349
350 assertThat(jsonLinks, hasLink(link2));
351 }
352}