Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -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 | 19ffea3 | 2015-01-28 10:03:06 -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 | 19ffea3 | 2015-01-28 10:03:06 -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.ImmutableSet; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 22 | import org.hamcrest.Description; |
| 23 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.osgi.ServiceDirectory; |
| 28 | import org.onlab.osgi.TestServiceDirectory; |
| 29 | import org.onlab.rest.BaseResource; |
| 30 | import org.onosproject.codec.CodecService; |
| 31 | import org.onosproject.codec.impl.CodecManager; |
| 32 | import org.onosproject.net.ElementId; |
| 33 | import org.onosproject.net.Link; |
| 34 | import org.onosproject.net.Path; |
| 35 | import org.onosproject.net.topology.PathService; |
| 36 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 37 | import javax.ws.rs.client.WebTarget; |
| 38 | import java.io.UnsupportedEncodingException; |
| 39 | import java.net.URLEncoder; |
| 40 | import java.nio.charset.StandardCharsets; |
| 41 | import java.util.Set; |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 42 | |
| 43 | import static org.easymock.EasyMock.createMock; |
| 44 | import static org.easymock.EasyMock.expect; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.easymock.EasyMock.verify; |
| 47 | import static org.hamcrest.Matchers.containsString; |
| 48 | import static org.hamcrest.Matchers.hasSize; |
| 49 | import static org.hamcrest.Matchers.is; |
| 50 | import static org.hamcrest.Matchers.notNullValue; |
| 51 | import static org.junit.Assert.assertThat; |
| 52 | import static org.onosproject.net.NetTestTools.createPath; |
| 53 | import static org.onosproject.net.NetTestTools.did; |
| 54 | import static org.onosproject.net.NetTestTools.hid; |
| 55 | |
| 56 | /** |
| 57 | * Unit tests for paths REST APIs. |
| 58 | */ |
| 59 | public class PathsResourceTest extends ResourceTest { |
| 60 | Path path1 = createPath("dev1", "dev2"); |
| 61 | Path path2 = createPath("dev2", "dev3"); |
| 62 | Set<Path> paths = ImmutableSet.of(path1, path2); |
| 63 | |
| 64 | final PathService mockPathService = createMock(PathService.class); |
| 65 | |
| 66 | /** |
| 67 | * Hamcrest matcher for a path and its JSON representation. |
| 68 | */ |
| 69 | private final class PathJsonMatcher extends TypeSafeDiagnosingMatcher<JsonObject> { |
| 70 | |
| 71 | private final Path path; |
| 72 | |
| 73 | /** |
| 74 | * Creates the matcher. |
| 75 | * |
| 76 | * @param pathValue the path object to match |
| 77 | */ |
| 78 | private PathJsonMatcher(Path pathValue) { |
| 79 | path = pathValue; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean matchesSafely(JsonObject pathJson, Description description) { |
| 84 | |
| 85 | double jsonCost = pathJson.get("cost").asDouble(); |
| 86 | if (jsonCost != path.cost()) { |
| 87 | description.appendText("src device was " + jsonCost); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | JsonArray jsonLinks = pathJson.get("links").asArray(); |
| 92 | assertThat(jsonLinks.size(), is(path.links().size())); |
| 93 | |
| 94 | for (int linkIndex = 0; linkIndex < jsonLinks.size(); linkIndex++) { |
| 95 | Link link = path.links().get(linkIndex); |
| 96 | JsonObject jsonLink = jsonLinks.get(0).asObject(); |
| 97 | |
| 98 | JsonObject jsonLinkSrc = jsonLink.get("src").asObject(); |
| 99 | String srcDevice = jsonLinkSrc.get("device").asString(); |
| 100 | if (!srcDevice.equals(link.src().deviceId().toString())) { |
| 101 | description.appendText("src device was " + jsonLinkSrc); |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | JsonObject jsonLinkDst = jsonLink.get("dst").asObject(); |
| 106 | String dstDevice = jsonLinkDst.get("device").asString(); |
| 107 | if (!dstDevice.equals(link.dst().deviceId().toString())) { |
| 108 | description.appendText("dst device was " + jsonLinkDst); |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return true; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void describeTo(Description description) { |
| 118 | description.appendText(path.toString()); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Factory to allocate an connect point matcher. |
| 124 | * |
| 125 | * @param path path object we are looking for |
| 126 | * @return matcher |
| 127 | */ |
| 128 | private PathJsonMatcher matchesPath(Path path) { |
| 129 | return new PathJsonMatcher(path); |
| 130 | } |
| 131 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 132 | /** |
| 133 | * Initializes test mocks and environment. |
| 134 | */ |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 135 | @Before |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 136 | public void setUpTest() { |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 137 | |
| 138 | // Register the services needed for the test |
| 139 | CodecManager codecService = new CodecManager(); |
| 140 | codecService.activate(); |
| 141 | ServiceDirectory testDirectory = |
| 142 | new TestServiceDirectory() |
| 143 | .add(PathService.class, mockPathService) |
| 144 | .add(CodecService.class, codecService); |
| 145 | |
| 146 | BaseResource.setServiceDirectory(testDirectory); |
| 147 | } |
| 148 | |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 149 | /** |
| 150 | * Tears down test mocks and environment. |
| 151 | */ |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 152 | @After |
Ray Milkey | ed0b166 | 2015-02-05 09:34:29 -0800 | [diff] [blame] | 153 | public void tearDownTest() { |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 154 | verify(mockPathService); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Tests a REST path GET for the given endpoints. |
| 159 | * |
| 160 | * @param srcElement source element of the path |
| 161 | * @param dstElement destination element of the path |
| 162 | * |
| 163 | * @throws UnsupportedEncodingException |
| 164 | */ |
| 165 | private void runTest(ElementId srcElement, ElementId dstElement) |
| 166 | throws UnsupportedEncodingException { |
| 167 | expect(mockPathService.getPaths(srcElement, dstElement)) |
| 168 | .andReturn(paths) |
| 169 | .once(); |
| 170 | replay(mockPathService); |
| 171 | |
| 172 | String srcId = URLEncoder.encode(srcElement.toString(), |
| 173 | StandardCharsets.UTF_8.name()); |
| 174 | String dstId = URLEncoder.encode(dstElement.toString(), |
| 175 | StandardCharsets.UTF_8.name()); |
| 176 | |
| 177 | String url = "paths/" + srcId + "/" + dstId; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 178 | WebTarget wt = target(); |
| 179 | String response = wt.path(url).request().get(String.class); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 180 | assertThat(response, containsString("{\"paths\":[")); |
| 181 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 182 | JsonObject result = Json.parse(response).asObject(); |
Ray Milkey | 19ffea3 | 2015-01-28 10:03:06 -0800 | [diff] [blame] | 183 | assertThat(result, notNullValue()); |
| 184 | |
| 185 | assertThat(result.names(), hasSize(1)); |
| 186 | assertThat(result.names().get(0), is("paths")); |
| 187 | |
| 188 | JsonArray jsonPaths = result.get("paths").asArray(); |
| 189 | assertThat(jsonPaths, notNullValue()); |
| 190 | assertThat(jsonPaths.size(), is(2)); |
| 191 | |
| 192 | JsonObject path1Json = jsonPaths.get(0).asObject(); |
| 193 | assertThat(path1Json, matchesPath(path1)); |
| 194 | |
| 195 | JsonObject path2Json = jsonPaths.get(1).asObject(); |
| 196 | assertThat(path2Json, matchesPath(path2)); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Tests a path between two hosts. |
| 201 | * |
| 202 | * @throws UnsupportedEncodingException if UTF-8 not found |
| 203 | */ |
| 204 | @Test |
| 205 | public void hostToHost() throws UnsupportedEncodingException { |
| 206 | runTest(hid("01:23:45:67:89:AB/2"), hid("AB:89:67:45:23:01/4")); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Tests a path with a host as the source and a switch as the destination. |
| 211 | * |
| 212 | * @throws UnsupportedEncodingException if UTF-8 not found |
| 213 | */ |
| 214 | @Test |
| 215 | public void hostToDevice() throws UnsupportedEncodingException { |
| 216 | runTest(hid("01:23:45:67:89:AB/2"), did("switch1")); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Tests a path with a switch as the source and a host as the destination. |
| 221 | * |
| 222 | * @throws UnsupportedEncodingException if UTF-8 not found |
| 223 | */ |
| 224 | @Test |
| 225 | public void deviceToHost() throws UnsupportedEncodingException { |
| 226 | runTest(did("switch1"), hid("01:23:45:67:89:AB/2")); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Tests a path between two switches. |
| 231 | * |
| 232 | * @throws UnsupportedEncodingException if UTF-8 not found |
| 233 | */ |
| 234 | @Test |
| 235 | public void deviceToDevice() throws UnsupportedEncodingException { |
| 236 | runTest(did("switch1"), did("switch2")); |
| 237 | } |
| 238 | } |