Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 30 | import org.onlab.rest.BaseResource; |
| 31 | import org.onosproject.codec.CodecService; |
| 32 | import org.onosproject.codec.impl.CodecManager; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.link.LinkService; |
| 37 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 38 | import javax.ws.rs.client.WebTarget; |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 39 | |
| 40 | import static org.easymock.EasyMock.createMock; |
| 41 | import static org.easymock.EasyMock.expect; |
| 42 | import static org.easymock.EasyMock.isA; |
| 43 | import static org.easymock.EasyMock.replay; |
| 44 | import static org.easymock.EasyMock.verify; |
| 45 | import static org.hamcrest.Matchers.containsString; |
| 46 | import static org.hamcrest.Matchers.hasSize; |
| 47 | import static org.hamcrest.Matchers.is; |
| 48 | import static org.hamcrest.Matchers.notNullValue; |
| 49 | import static org.junit.Assert.assertThat; |
| 50 | import static org.onosproject.net.NetTestTools.link; |
| 51 | |
| 52 | /** |
| 53 | * Unit tests for links REST APIs. |
| 54 | */ |
Ray Milkey | 9c3d336 | 2015-01-28 10:39:56 -0800 | [diff] [blame] | 55 | public class LinksResourceTest extends ResourceTest { |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 56 | 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 Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 63 | * 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 Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 127 | 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 Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Initializes test mocks and environment. |
| 152 | */ |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 153 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 154 | public void setUpTest() { |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 155 | 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 Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 168 | /** |
| 169 | * Tears down and verifies test mocks and environment. |
| 170 | */ |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 171 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 172 | public void tearDownTest() { |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 173 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 184 | WebTarget wt = target(); |
| 185 | String response = wt.path("links").request().get(String.class); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 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 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 200 | WebTarget wt = target(); |
| 201 | String response = wt.path("links").request().get(String.class); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 202 | assertThat(response, containsString("{\"links\":[")); |
| 203 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 204 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 205 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 230 | WebTarget wt = target(); |
| 231 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 232 | .path("links") |
| 233 | .queryParam("device", "src2") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 234 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 235 | .get(String.class); |
| 236 | assertThat(response, containsString("{\"links\":[")); |
| 237 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 238 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 239 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 264 | WebTarget wt = target(); |
| 265 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 266 | .path("links") |
| 267 | .queryParam("device", "src2") |
| 268 | .queryParam("port", "2") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 269 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 270 | .get(String.class); |
| 271 | assertThat(response, containsString("{\"links\":[")); |
| 272 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 273 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 274 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 299 | WebTarget wt = target(); |
| 300 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 301 | .path("links") |
| 302 | .queryParam("device", "src2") |
| 303 | .queryParam("port", "2") |
| 304 | .queryParam("direction", "INGRESS") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 305 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 306 | .get(String.class); |
| 307 | assertThat(response, containsString("{\"links\":[")); |
| 308 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 309 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 310 | 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 Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 335 | WebTarget wt = target(); |
| 336 | String response = wt |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 337 | .path("links") |
| 338 | .queryParam("device", "src2") |
| 339 | .queryParam("direction", "INGRESS") |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 340 | .request() |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 341 | .get(String.class); |
| 342 | assertThat(response, containsString("{\"links\":[")); |
| 343 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 344 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 8fd68ca | 2015-01-27 15:19:09 -0800 | [diff] [blame] | 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 | } |