blob: feff0ed03609fba50f87432b3d8d3148b0629c3a [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
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800126 if (matchesLink(link).matchesSafely(jsonLink)) {
127 return true;
128 }
129 }
130 return false;
131 }
132
133 @Override
134 public void describeTo(Description description) {
135 description.appendText(reason);
136 }
137 }
138
139 /**
140 * Factory to allocate an link array matcher.
141 *
142 * @param link link object we are looking for
143 * @return matcher
144 */
145 private static LinkJsonArrayMatcher hasLink(Link link) {
146 return new LinkJsonArrayMatcher(link);
147 }
148
Ray Milkeyed0b1662015-02-05 09:34:29 -0800149 /**
150 * Initializes test mocks and environment.
151 */
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800152 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800153 public void setUpTest() {
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800154 mockLinkService = createMock(LinkService.class);
155
156 // Register the services needed for the test
157 CodecManager codecService = new CodecManager();
158 codecService.activate();
159 ServiceDirectory testDirectory =
160 new TestServiceDirectory()
161 .add(LinkService.class, mockLinkService)
162 .add(CodecService.class, codecService);
163
164 BaseResource.setServiceDirectory(testDirectory);
165 }
166
Ray Milkeyed0b1662015-02-05 09:34:29 -0800167 /**
168 * Tears down and verifies test mocks and environment.
169 */
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800170 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800171 public void tearDownTest() {
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800172 verify(mockLinkService);
173 }
174
175 /**
176 * Tests the result of the rest api GET when there are no links.
177 */
178 @Test
179 public void testLinksEmptyArray() {
180 expect(mockLinkService.getLinks()).andReturn(ImmutableList.of());
181 replay(mockLinkService);
182
183 WebResource rs = resource();
184 String response = rs.path("links").get(String.class);
185 assertThat(response, is("{\"links\":[]}"));
186 }
187
188 /**
189 * Tests the result of the rest api GET when there are links present.
190 */
191 @Test
192 public void testLinks() {
193 expect(mockLinkService.getLinks())
194 .andReturn(ImmutableList.of(link1, link2, link3))
195 .anyTimes();
196
197 replay(mockLinkService);
198
199 WebResource rs = resource();
200 String response = rs.path("links").get(String.class);
201 assertThat(response, containsString("{\"links\":["));
202
203 JsonObject result = JsonObject.readFrom(response);
204 assertThat(result, notNullValue());
205
206 assertThat(result.names(), hasSize(1));
207 assertThat(result.names().get(0), is("links"));
208
209 JsonArray jsonLinks = result.get("links").asArray();
210 assertThat(jsonLinks, notNullValue());
211 assertThat(jsonLinks.size(), is(3));
212
213 assertThat(jsonLinks, hasLink(link1));
214 assertThat(jsonLinks, hasLink(link2));
215 assertThat(jsonLinks, hasLink(link3));
216 }
217
218 /**
219 * Tests the result of the rest api GET of links for a specific device.
220 */
221 @Test
222 public void testLinksByDevice() {
223 expect(mockLinkService.getDeviceLinks(isA(DeviceId.class)))
224 .andReturn(ImmutableSet.of(link2))
225 .anyTimes();
226
227 replay(mockLinkService);
228
229 WebResource rs = resource();
230 String response = rs
231 .path("links")
232 .queryParam("device", "src2")
233 .get(String.class);
234 assertThat(response, containsString("{\"links\":["));
235
236 JsonObject result = JsonObject.readFrom(response);
237 assertThat(result, notNullValue());
238
239 assertThat(result.names(), hasSize(1));
240 assertThat(result.names().get(0), is("links"));
241
242 JsonArray jsonLinks = result.get("links").asArray();
243 assertThat(jsonLinks, notNullValue());
244 assertThat(jsonLinks.size(), is(1));
245
246 assertThat(jsonLinks, hasLink(link2));
247 }
248
249 /**
250 * Tests the result of the rest api GET of links for a specific device
251 * and port.
252 */
253 @Test
254 public void testLinksByDevicePort() {
255
256 expect(mockLinkService.getLinks(isA(ConnectPoint.class)))
257 .andReturn(ImmutableSet.of(link2))
258 .anyTimes();
259
260 replay(mockLinkService);
261
262 WebResource rs = resource();
263 String response = rs
264 .path("links")
265 .queryParam("device", "src2")
266 .queryParam("port", "2")
267 .get(String.class);
268 assertThat(response, containsString("{\"links\":["));
269
270 JsonObject result = JsonObject.readFrom(response);
271 assertThat(result, notNullValue());
272
273 assertThat(result.names(), hasSize(1));
274 assertThat(result.names().get(0), is("links"));
275
276 JsonArray jsonLinks = result.get("links").asArray();
277 assertThat(jsonLinks, notNullValue());
278 assertThat(jsonLinks.size(), is(1));
279
280 assertThat(jsonLinks, hasLink(link2));
281 }
282
283 /**
284 * Tests the result of the rest api GET of links for a specific
285 * device, port, and direction.
286 */
287 @Test
288 public void testLinksByDevicePortDirection() {
289
290 expect(mockLinkService.getIngressLinks(isA(ConnectPoint.class)))
291 .andReturn(ImmutableSet.of(link2))
292 .anyTimes();
293
294 replay(mockLinkService);
295
296 WebResource rs = resource();
297 String response = rs
298 .path("links")
299 .queryParam("device", "src2")
300 .queryParam("port", "2")
301 .queryParam("direction", "INGRESS")
302 .get(String.class);
303 assertThat(response, containsString("{\"links\":["));
304
305 JsonObject result = JsonObject.readFrom(response);
306 assertThat(result, notNullValue());
307
308 assertThat(result.names(), hasSize(1));
309 assertThat(result.names().get(0), is("links"));
310
311 JsonArray jsonLinks = result.get("links").asArray();
312 assertThat(jsonLinks, notNullValue());
313 assertThat(jsonLinks.size(), is(1));
314
315 assertThat(jsonLinks, hasLink(link2));
316 }
317
318 /**
319 * Tests the result of the rest api GET of links for a specific
320 * device and direction.
321 */
322 @Test
323 public void testLinksByDeviceDirection() {
324
325 expect(mockLinkService.getDeviceIngressLinks(isA(DeviceId.class)))
326 .andReturn(ImmutableSet.of(link2))
327 .anyTimes();
328
329 replay(mockLinkService);
330
331 WebResource rs = resource();
332 String response = rs
333 .path("links")
334 .queryParam("device", "src2")
335 .queryParam("direction", "INGRESS")
336 .get(String.class);
337 assertThat(response, containsString("{\"links\":["));
338
339 JsonObject result = JsonObject.readFrom(response);
340 assertThat(result, notNullValue());
341
342 assertThat(result.names(), hasSize(1));
343 assertThat(result.names().get(0), is("links"));
344
345 JsonArray jsonLinks = result.get("links").asArray();
346 assertThat(jsonLinks, notNullValue());
347 assertThat(jsonLinks.size(), is(1));
348
349 assertThat(jsonLinks, hasLink(link2));
350 }
351}