blob: bf0565674592a6c3fcdaf499b2deb2cdcee035f4 [file] [log] [blame]
Ray Milkey19ffea32015-01-28 10:03:06 -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.ImmutableSet;
Ray Milkey19ffea32015-01-28 10:03:06 -080022import org.hamcrest.Description;
23import org.hamcrest.TypeSafeDiagnosingMatcher;
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.osgi.ServiceDirectory;
28import org.onlab.osgi.TestServiceDirectory;
29import org.onlab.rest.BaseResource;
30import org.onosproject.codec.CodecService;
31import org.onosproject.codec.impl.CodecManager;
32import org.onosproject.net.ElementId;
33import org.onosproject.net.Link;
34import org.onosproject.net.Path;
35import org.onosproject.net.topology.PathService;
36
Jian Li9d616492016-03-09 10:52:49 -080037import javax.ws.rs.client.WebTarget;
38import java.io.UnsupportedEncodingException;
39import java.net.URLEncoder;
40import java.nio.charset.StandardCharsets;
41import java.util.Set;
Ray Milkey19ffea32015-01-28 10:03:06 -080042
43import static org.easymock.EasyMock.createMock;
44import static org.easymock.EasyMock.expect;
45import static org.easymock.EasyMock.replay;
46import static org.easymock.EasyMock.verify;
47import static org.hamcrest.Matchers.containsString;
48import static org.hamcrest.Matchers.hasSize;
49import static org.hamcrest.Matchers.is;
50import static org.hamcrest.Matchers.notNullValue;
51import static org.junit.Assert.assertThat;
52import static org.onosproject.net.NetTestTools.createPath;
53import static org.onosproject.net.NetTestTools.did;
54import static org.onosproject.net.NetTestTools.hid;
55
56/**
57 * Unit tests for paths REST APIs.
58 */
59public 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 Milkeyed0b1662015-02-05 09:34:29 -0800132 /**
133 * Initializes test mocks and environment.
134 */
Ray Milkey19ffea32015-01-28 10:03:06 -0800135 @Before
Ray Milkeyed0b1662015-02-05 09:34:29 -0800136 public void setUpTest() {
Ray Milkey19ffea32015-01-28 10:03:06 -0800137
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 Milkeyed0b1662015-02-05 09:34:29 -0800149 /**
150 * Tears down test mocks and environment.
151 */
Ray Milkey19ffea32015-01-28 10:03:06 -0800152 @After
Ray Milkeyed0b1662015-02-05 09:34:29 -0800153 public void tearDownTest() {
Ray Milkey19ffea32015-01-28 10:03:06 -0800154 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 Li9d616492016-03-09 10:52:49 -0800178 WebTarget wt = target();
179 String response = wt.path(url).request().get(String.class);
Ray Milkey19ffea32015-01-28 10:03:06 -0800180 assertThat(response, containsString("{\"paths\":["));
181
Jian Li80cfe452016-01-14 16:04:58 -0800182 JsonObject result = Json.parse(response).asObject();
Ray Milkey19ffea32015-01-28 10:03:06 -0800183 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}