blob: e26bceec68256d778478a7a70986ea03fc0aab99 [file] [log] [blame]
Ray Milkey8fd68ca2015-01-27 15:19:09 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey8fd68ca2015-01-27 15:19:09 -08003 *
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 */
Jian Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Ray Milkey8fd68ca2015-01-27 15:19:09 -080017
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;
Ray Milkey8fd68ca2015-01-27 15:19:09 -080030import org.onosproject.codec.CodecService;
31import org.onosproject.codec.impl.CodecManager;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DeviceId;
34import org.onosproject.net.Link;
35import org.onosproject.net.link.LinkService;
36
Jian Li9d616492016-03-09 10:52:49 -080037import javax.ws.rs.client.WebTarget;
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
Ray Milkey094a1352018-01-22 14:03:54 -0800164 setServiceDirectory(testDirectory);
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800165 }
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
Jian Li9d616492016-03-09 10:52:49 -0800183 WebTarget wt = target();
184 String response = wt.path("links").request().get(String.class);
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800185 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
Jian Li9d616492016-03-09 10:52:49 -0800199 WebTarget wt = target();
200 String response = wt.path("links").request().get(String.class);
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800201 assertThat(response, containsString("{\"links\":["));
202
Jian Li80cfe452016-01-14 16:04:58 -0800203 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800204 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
Jian Li9d616492016-03-09 10:52:49 -0800229 WebTarget wt = target();
230 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800231 .path("links")
232 .queryParam("device", "src2")
Jian Li9d616492016-03-09 10:52:49 -0800233 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800234 .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
Jian Li9d616492016-03-09 10:52:49 -0800263 WebTarget wt = target();
264 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800265 .path("links")
266 .queryParam("device", "src2")
267 .queryParam("port", "2")
Jian Li9d616492016-03-09 10:52:49 -0800268 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800269 .get(String.class);
270 assertThat(response, containsString("{\"links\":["));
271
Jian Li80cfe452016-01-14 16:04:58 -0800272 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800273 assertThat(result, notNullValue());
274
275 assertThat(result.names(), hasSize(1));
276 assertThat(result.names().get(0), is("links"));
277
278 JsonArray jsonLinks = result.get("links").asArray();
279 assertThat(jsonLinks, notNullValue());
280 assertThat(jsonLinks.size(), is(1));
281
282 assertThat(jsonLinks, hasLink(link2));
283 }
284
285 /**
286 * Tests the result of the rest api GET of links for a specific
287 * device, port, and direction.
288 */
289 @Test
290 public void testLinksByDevicePortDirection() {
291
292 expect(mockLinkService.getIngressLinks(isA(ConnectPoint.class)))
293 .andReturn(ImmutableSet.of(link2))
294 .anyTimes();
295
296 replay(mockLinkService);
297
Jian Li9d616492016-03-09 10:52:49 -0800298 WebTarget wt = target();
299 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800300 .path("links")
301 .queryParam("device", "src2")
302 .queryParam("port", "2")
303 .queryParam("direction", "INGRESS")
Jian Li9d616492016-03-09 10:52:49 -0800304 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800305 .get(String.class);
306 assertThat(response, containsString("{\"links\":["));
307
Jian Li80cfe452016-01-14 16:04:58 -0800308 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800309 assertThat(result, notNullValue());
310
311 assertThat(result.names(), hasSize(1));
312 assertThat(result.names().get(0), is("links"));
313
314 JsonArray jsonLinks = result.get("links").asArray();
315 assertThat(jsonLinks, notNullValue());
316 assertThat(jsonLinks.size(), is(1));
317
318 assertThat(jsonLinks, hasLink(link2));
319 }
320
321 /**
322 * Tests the result of the rest api GET of links for a specific
323 * device and direction.
324 */
325 @Test
326 public void testLinksByDeviceDirection() {
327
328 expect(mockLinkService.getDeviceIngressLinks(isA(DeviceId.class)))
329 .andReturn(ImmutableSet.of(link2))
330 .anyTimes();
331
332 replay(mockLinkService);
333
Jian Li9d616492016-03-09 10:52:49 -0800334 WebTarget wt = target();
335 String response = wt
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800336 .path("links")
337 .queryParam("device", "src2")
338 .queryParam("direction", "INGRESS")
Jian Li9d616492016-03-09 10:52:49 -0800339 .request()
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800340 .get(String.class);
341 assertThat(response, containsString("{\"links\":["));
342
Jian Li80cfe452016-01-14 16:04:58 -0800343 JsonObject result = Json.parse(response).asObject();
Ray Milkey8fd68ca2015-01-27 15:19:09 -0800344 assertThat(result, notNullValue());
345
346 assertThat(result.names(), hasSize(1));
347 assertThat(result.names().get(0), is("links"));
348
349 JsonArray jsonLinks = result.get("links").asArray();
350 assertThat(jsonLinks, notNullValue());
351 assertThat(jsonLinks.size(), is(1));
352
353 assertThat(jsonLinks, hasLink(link2));
354 }
355}