blob: ec9c4c045062611aa21637a14aa081844778201f [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;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonArray;
20import com.eclipsesource.json.JsonObject;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableSet;
Ray Milkey8fd68ca2015-01-27 15:19:09 -080023import org.hamcrest.Description;
24import org.hamcrest.TypeSafeMatcher;
25import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.onlab.osgi.ServiceDirectory;
29import org.onlab.osgi.TestServiceDirectory;
30import org.onlab.rest.BaseResource;
31import org.onosproject.codec.CodecService;
32import org.onosproject.codec.impl.CodecManager;
33import org.onosproject.net.ConnectPoint;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Link;
36import org.onosproject.net.link.LinkService;
37
Jian Li9d616492016-03-09 10:52:49 -080038import javax.ws.rs.client.WebTarget;
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
Jian Li9d616492016-03-09 10:52:49 -0800184 WebTarget wt = target();
185 String response = wt.path("links").request().get(String.class);
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800186 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
Jian Li9d616492016-03-09 10:52:49 -0800200 WebTarget wt = target();
201 String response = wt.path("links").request().get(String.class);
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800202 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
Jian Li9d616492016-03-09 10:52:49 -0800230 WebTarget wt = target();
231 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800232 .path("links")
233 .queryParam("device", "src2")
Jian Li9d616492016-03-09 10:52:49 -0800234 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800235 .get(String.class);
236 assertThat(response, containsString("{\"links\":["));
237
Jian Li80cfe452016-01-14 16:04:58 -0800238 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800239 assertThat(result, notNullValue());
240
241 assertThat(result.names(), hasSize(1));
242 assertThat(result.names().get(0), is("links"));
243
244 JsonArray jsonLinks = result.get("links").asArray();
245 assertThat(jsonLinks, notNullValue());
246 assertThat(jsonLinks.size(), is(1));
247
248 assertThat(jsonLinks, hasLink(link2));
249 }
250
251 /**
252 * Tests the result of the rest api GET of links for a specific device
253 * and port.
254 */
255 @Test
256 public void testLinksByDevicePort() {
257
258 expect(mockLinkService.getLinks(isA(ConnectPoint.class)))
259 .andReturn(ImmutableSet.of(link2))
260 .anyTimes();
261
262 replay(mockLinkService);
263
Jian Li9d616492016-03-09 10:52:49 -0800264 WebTarget wt = target();
265 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800266 .path("links")
267 .queryParam("device", "src2")
268 .queryParam("port", "2")
Jian Li9d616492016-03-09 10:52:49 -0800269 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800270 .get(String.class);
271 assertThat(response, containsString("{\"links\":["));
272
Jian Li80cfe452016-01-14 16:04:58 -0800273 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800274 assertThat(result, notNullValue());
275
276 assertThat(result.names(), hasSize(1));
277 assertThat(result.names().get(0), is("links"));
278
279 JsonArray jsonLinks = result.get("links").asArray();
280 assertThat(jsonLinks, notNullValue());
281 assertThat(jsonLinks.size(), is(1));
282
283 assertThat(jsonLinks, hasLink(link2));
284 }
285
286 /**
287 * Tests the result of the rest api GET of links for a specific
288 * device, port, and direction.
289 */
290 @Test
291 public void testLinksByDevicePortDirection() {
292
293 expect(mockLinkService.getIngressLinks(isA(ConnectPoint.class)))
294 .andReturn(ImmutableSet.of(link2))
295 .anyTimes();
296
297 replay(mockLinkService);
298
Jian Li9d616492016-03-09 10:52:49 -0800299 WebTarget wt = target();
300 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800301 .path("links")
302 .queryParam("device", "src2")
303 .queryParam("port", "2")
304 .queryParam("direction", "INGRESS")
Jian Li9d616492016-03-09 10:52:49 -0800305 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800306 .get(String.class);
307 assertThat(response, containsString("{\"links\":["));
308
Jian Li80cfe452016-01-14 16:04:58 -0800309 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800310 assertThat(result, notNullValue());
311
312 assertThat(result.names(), hasSize(1));
313 assertThat(result.names().get(0), is("links"));
314
315 JsonArray jsonLinks = result.get("links").asArray();
316 assertThat(jsonLinks, notNullValue());
317 assertThat(jsonLinks.size(), is(1));
318
319 assertThat(jsonLinks, hasLink(link2));
320 }
321
322 /**
323 * Tests the result of the rest api GET of links for a specific
324 * device and direction.
325 */
326 @Test
327 public void testLinksByDeviceDirection() {
328
329 expect(mockLinkService.getDeviceIngressLinks(isA(DeviceId.class)))
330 .andReturn(ImmutableSet.of(link2))
331 .anyTimes();
332
333 replay(mockLinkService);
334
Jian Li9d616492016-03-09 10:52:49 -0800335 WebTarget wt = target();
336 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800337 .path("links")
338 .queryParam("device", "src2")
339 .queryParam("direction", "INGRESS")
Jian Li9d616492016-03-09 10:52:49 -0800340 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800341 .get(String.class);
342 assertThat(response, containsString("{\"links\":["));
343
Jian Li80cfe452016-01-14 16:04:58 -0800344 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800345 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}