blob: efbcc9b29056c90d79b62c8e91410617b6690ae8 [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
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Ray Milkey8fd68ca2015-01-27 15:19:09 -080019import org.hamcrest.Description;
20import org.hamcrest.TypeSafeMatcher;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.osgi.TestServiceDirectory;
26import org.onlab.rest.BaseResource;
27import org.onosproject.codec.CodecService;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.Link;
32import org.onosproject.net.link.LinkService;
33
34import com.eclipsesource.json.JsonArray;
35import com.eclipsesource.json.JsonObject;
36import com.google.common.collect.ImmutableList;
37import com.google.common.collect.ImmutableSet;
38import com.sun.jersey.api.client.WebResource;
Ray Milkey8fd68ca2015-01-27 15:19:09 -080039
40import static org.easymock.EasyMock.createMock;
41import static org.easymock.EasyMock.expect;
42import static org.easymock.EasyMock.isA;
43import static org.easymock.EasyMock.replay;
44import static org.easymock.EasyMock.verify;
45import static org.hamcrest.Matchers.containsString;
46import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.is;
48import static org.hamcrest.Matchers.notNullValue;
49import static org.junit.Assert.assertThat;
50import static org.onosproject.net.NetTestTools.link;
51
52/**
53 * Unit tests for links REST APIs.
54 */
Ray Milkey9c3d3362015-01-28 10:39:56 -080055public class LinksResourceTest extends ResourceTest {
Ray Milkey8fd68ca2015-01-27 15:19:09 -080056 LinkService mockLinkService;
57
58 Link link1 = link("src1", 1, "dst1", 1);
59 Link link2 = link("src2", 2, "dst2", 2);
60 Link link3 = link("src3", 3, "dst3", 3);
61
62 /**
Ray Milkey8fd68ca2015-01-27 15:19:09 -080063 * Hamcrest matcher to check that an link representation in JSON matches
64 * the actual link.
65 */
66 public static class LinkJsonMatcher extends TypeSafeMatcher<JsonObject> {
67 private final Link link;
68 private String reason = "";
69
70 public LinkJsonMatcher(Link linkValue) {
71 link = linkValue;
72 }
73
74 @Override
75 public boolean matchesSafely(JsonObject jsonLink) {
76 JsonObject jsonSrc = jsonLink.get("src").asObject();
77 String jsonSrcDevice = jsonSrc.get("device").asString();
78 String jsonSrcPort = jsonSrc.get("port").asString();
79
80 JsonObject jsonDst = jsonLink.get("dst").asObject();
81 String jsonDstDevice = jsonDst.get("device").asString();
82 String jsonDstPort = jsonDst.get("port").asString();
83
84 return jsonSrcDevice.equals(link.src().deviceId().toString()) &&
85 jsonSrcPort.equals(link.src().port().toString()) &&
86 jsonDstDevice.equals(link.dst().deviceId().toString()) &&
87 jsonDstPort.equals(link.dst().port().toString());
88 }
89
90 @Override
91 public void describeTo(Description description) {
92 description.appendText(reason);
93 }
94 }
95
96 /**
97 * Factory to allocate an link matcher.
98 *
99 * @param link link object we are looking for
100 * @return matcher
101 */
102 private static LinkJsonMatcher matchesLink(Link link) {
103 return new LinkJsonMatcher(link);
104 }
105
106 /**
107 * Hamcrest matcher to check that an link is represented properly in a JSON
108 * array of links.
109 */
110 private static class LinkJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
111 private final Link link;
112 private String reason = "";
113
114 public LinkJsonArrayMatcher(Link linkValue) {
115 link = linkValue;
116 }
117
118 @Override
119 public boolean matchesSafely(JsonArray json) {
120 final int expectedAttributes = 2;
121
122 for (int jsonLinkIndex = 0; jsonLinkIndex < json.size();
123 jsonLinkIndex++) {
124
125 JsonObject jsonLink = json.get(jsonLinkIndex).asObject();
126
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800127 if (matchesLink(link).matchesSafely(jsonLink)) {
128 return true;
129 }
130 }
131 return false;
132 }
133
134 @Override
135 public void describeTo(Description description) {
136 description.appendText(reason);
137 }
138 }
139
140 /**
141 * Factory to allocate an link array matcher.
142 *
143 * @param link link object we are looking for
144 * @return matcher
145 */
146 private static LinkJsonArrayMatcher hasLink(Link link) {
147 return new LinkJsonArrayMatcher(link);
148 }
149
Ray Milkeyed0b1662015-02-05 09:34:29 -0800150 /**
151 * Initializes test mocks and environment.
152 */
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800153 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800154 public void setUpTest() {
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800155 mockLinkService = createMock(LinkService.class);
156
157 // Register the services needed for the test
158 CodecManager codecService = new CodecManager();
159 codecService.activate();
160 ServiceDirectory testDirectory =
161 new TestServiceDirectory()
162 .add(LinkService.class, mockLinkService)
163 .add(CodecService.class, codecService);
164
165 BaseResource.setServiceDirectory(testDirectory);
166 }
167
Ray Milkeyed0b1662015-02-05 09:34:29 -0800168 /**
169 * Tears down and verifies test mocks and environment.
170 */
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800171 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800172 public void tearDownTest() {
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800173 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
Jian Li80cfe452016-01-14 16:04:58 -0800204 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800205 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
Jian Li80cfe452016-01-14 16:04:58 -0800237 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800238 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
Jian Li80cfe452016-01-14 16:04:58 -0800271 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800272 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
Jian Li80cfe452016-01-14 16:04:58 -0800306 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800307 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
Jian Li80cfe452016-01-14 16:04:58 -0800340 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800341 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}