Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 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 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 17 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonArray; |
| 20 | import com.eclipsesource.json.JsonObject; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 23 | import org.hamcrest.Description; |
| 24 | import org.hamcrest.TypeSafeMatcher; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.osgi.ServiceDirectory; |
| 29 | import org.onlab.osgi.TestServiceDirectory; |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 30 | import org.onosproject.codec.CodecService; |
| 31 | import org.onosproject.codec.impl.CodecManager; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Link; |
| 35 | import org.onosproject.net.link.LinkService; |
| 36 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 37 | import javax.ws.rs.client.WebTarget; |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 38 | |
| 39 | import static org.easymock.EasyMock.createMock; |
| 40 | import static org.easymock.EasyMock.expect; |
| 41 | import static org.easymock.EasyMock.isA; |
| 42 | import static org.easymock.EasyMock.replay; |
| 43 | import static org.easymock.EasyMock.verify; |
| 44 | import static org.hamcrest.Matchers.containsString; |
| 45 | import static org.hamcrest.Matchers.hasSize; |
| 46 | import static org.hamcrest.Matchers.is; |
| 47 | import static org.hamcrest.Matchers.notNullValue; |
| 48 | import static org.junit.Assert.assertThat; |
| 49 | import static org.onosproject.net.NetTestTools.link; |
| 50 | |
| 51 | /** |
| 52 | * Unit tests for links REST APIs. |
| 53 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 54 | public class LinksResourceTest extends ResourceTest { |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 55 | 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 Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 62 | * 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 Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 126 | 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 Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Initializes test mocks and environment. |
| 151 | */ |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 152 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 153 | public void setUpTest() { |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 154 | 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 Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 164 | setServiceDirectory(testDirectory); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 167 | /** |
| 168 | * Tears down and verifies test mocks and environment. |
| 169 | */ |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 170 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 171 | public void tearDownTest() { |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 172 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 183 | WebTarget wt = target(); |
| 184 | String response = wt.path("links").request().get(String.class); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 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 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 199 | WebTarget wt = target(); |
| 200 | String response = wt.path("links").request().get(String.class); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 201 | assertThat(response, containsString("{\"links\":[")); |
| 202 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 203 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 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 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 229 | WebTarget wt = target(); |
| 230 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 231 | .path("links") |
| 232 | .queryParam("device", "src2") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 233 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 234 | .get(String.class); |
| 235 | assertThat(response, containsString("{\"links\":[")); |
| 236 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 237 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 238 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 263 | WebTarget wt = target(); |
| 264 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 265 | .path("links") |
| 266 | .queryParam("device", "src2") |
| 267 | .queryParam("port", "2") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 268 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 269 | .get(String.class); |
| 270 | assertThat(response, containsString("{\"links\":[")); |
| 271 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 272 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 273 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 298 | WebTarget wt = target(); |
| 299 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 300 | .path("links") |
| 301 | .queryParam("device", "src2") |
| 302 | .queryParam("port", "2") |
| 303 | .queryParam("direction", "INGRESS") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 304 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 305 | .get(String.class); |
| 306 | assertThat(response, containsString("{\"links\":[")); |
| 307 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 308 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 309 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 334 | WebTarget wt = target(); |
| 335 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 336 | .path("links") |
| 337 | .queryParam("device", "src2") |
| 338 | .queryParam("direction", "INGRESS") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 339 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 340 | .get(String.class); |
| 341 | assertThat(response, containsString("{\"links\":[")); |
| 342 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 343 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 344 | 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 | } |