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