ONOS-792 Unit tests for Topology REST GET APIs
Change-Id: I2e82b9b9b6133ba80aa8f02df582f867b924a641
Showing
2 changed files
with
325 additions
and
4 deletions
... | @@ -127,7 +127,7 @@ public class TopologyWebResource extends AbstractWebResource { | ... | @@ -127,7 +127,7 @@ public class TopologyWebResource extends AbstractWebResource { |
127 | for (DeviceId deviceId : deviceIds) { | 127 | for (DeviceId deviceId : deviceIds) { |
128 | devicesNode.add(deviceId.toString()); | 128 | devicesNode.add(deviceId.toString()); |
129 | } | 129 | } |
130 | - return ok(root).build(); | 130 | + return ok(root.toString()).build(); |
131 | } | 131 | } |
132 | 132 | ||
133 | /** | 133 | /** |
... | @@ -150,7 +150,7 @@ public class TopologyWebResource extends AbstractWebResource { | ... | @@ -150,7 +150,7 @@ public class TopologyWebResource extends AbstractWebResource { |
150 | Lists.newArrayList(get(TopologyService.class) | 150 | Lists.newArrayList(get(TopologyService.class) |
151 | .getClusterLinks(topology, cluster)); | 151 | .getClusterLinks(topology, cluster)); |
152 | 152 | ||
153 | - return ok(encodeArray(Link.class, "links", links)).build(); | 153 | + return ok(encodeArray(Link.class, "links", links).toString()).build(); |
154 | } | 154 | } |
155 | 155 | ||
156 | /** | 156 | /** |
... | @@ -201,7 +201,11 @@ public class TopologyWebResource extends AbstractWebResource { | ... | @@ -201,7 +201,11 @@ public class TopologyWebResource extends AbstractWebResource { |
201 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); | 201 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
202 | boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint); | 202 | boolean isBroadcast = get(TopologyService.class).isBroadcastPoint(topology, connectPoint); |
203 | 203 | ||
204 | - return ok(mapper().createObjectNode().put("broadcast", isBroadcast)).build(); | 204 | + return ok(mapper() |
205 | + .createObjectNode() | ||
206 | + .put("broadcast", isBroadcast) | ||
207 | + .toString()) | ||
208 | + .build(); | ||
205 | } | 209 | } |
206 | 210 | ||
207 | /** | 211 | /** |
... | @@ -224,7 +228,10 @@ public class TopologyWebResource extends AbstractWebResource { | ... | @@ -224,7 +228,10 @@ public class TopologyWebResource extends AbstractWebResource { |
224 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); | 228 | ConnectPoint connectPoint = new ConnectPoint(deviceId, portNumber); |
225 | boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint); | 229 | boolean isInfrastructure = get(TopologyService.class).isInfrastructure(topology, connectPoint); |
226 | 230 | ||
227 | - return ok(mapper().createObjectNode().put("infrastructure", isInfrastructure)).build(); | 231 | + return ok(mapper() |
232 | + .createObjectNode() | ||
233 | + .put("infrastructure", isInfrastructure).toString()) | ||
234 | + .build(); | ||
228 | } | 235 | } |
229 | 236 | ||
230 | } | 237 | } | ... | ... |
1 | +/* | ||
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 | + */ | ||
16 | +package org.onosproject.rest; | ||
17 | + | ||
18 | +import java.util.Set; | ||
19 | + | ||
20 | +import org.junit.Before; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onlab.osgi.ServiceDirectory; | ||
23 | +import org.onlab.osgi.TestServiceDirectory; | ||
24 | +import org.onlab.rest.BaseResource; | ||
25 | +import org.onosproject.codec.CodecService; | ||
26 | +import org.onosproject.codec.impl.CodecManager; | ||
27 | +import org.onosproject.net.ConnectPoint; | ||
28 | +import org.onosproject.net.DeviceId; | ||
29 | +import org.onosproject.net.Link; | ||
30 | +import org.onosproject.net.Path; | ||
31 | +import org.onosproject.net.provider.ProviderId; | ||
32 | +import org.onosproject.net.topology.ClusterId; | ||
33 | +import org.onosproject.net.topology.DefaultTopologyCluster; | ||
34 | +import org.onosproject.net.topology.DefaultTopologyVertex; | ||
35 | +import org.onosproject.net.topology.LinkWeight; | ||
36 | +import org.onosproject.net.topology.Topology; | ||
37 | +import org.onosproject.net.topology.TopologyCluster; | ||
38 | +import org.onosproject.net.topology.TopologyGraph; | ||
39 | +import org.onosproject.net.topology.TopologyListener; | ||
40 | +import org.onosproject.net.topology.TopologyService; | ||
41 | + | ||
42 | +import com.eclipsesource.json.JsonArray; | ||
43 | +import com.eclipsesource.json.JsonObject; | ||
44 | +import com.google.common.collect.ImmutableSet; | ||
45 | +import com.sun.jersey.api.client.WebResource; | ||
46 | +import com.sun.jersey.test.framework.JerseyTest; | ||
47 | + | ||
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.hamcrest.Matchers.containsString; | ||
52 | +import static org.junit.Assert.assertThat; | ||
53 | +import static org.onosproject.net.NetTestTools.did; | ||
54 | +import static org.onosproject.net.NetTestTools.link; | ||
55 | + | ||
56 | +/** | ||
57 | + * Unit tests for Topology REST APIs. | ||
58 | + */ | ||
59 | +public class TopologyResourceTest extends JerseyTest { | ||
60 | + | ||
61 | + private static class MockTopology implements Topology { | ||
62 | + @Override | ||
63 | + public long time() { | ||
64 | + return 11111L; | ||
65 | + } | ||
66 | + | ||
67 | + @Override | ||
68 | + public long computeCost() { | ||
69 | + return 0; | ||
70 | + } | ||
71 | + | ||
72 | + @Override | ||
73 | + public int clusterCount() { | ||
74 | + return 2; | ||
75 | + } | ||
76 | + | ||
77 | + @Override | ||
78 | + public int deviceCount() { | ||
79 | + return 6; | ||
80 | + } | ||
81 | + | ||
82 | + @Override | ||
83 | + public int linkCount() { | ||
84 | + return 4; | ||
85 | + } | ||
86 | + | ||
87 | + @Override | ||
88 | + public ProviderId providerId() { | ||
89 | + return ProviderId.NONE; | ||
90 | + } | ||
91 | + } | ||
92 | + | ||
93 | + private static class MockTopologyService implements TopologyService { | ||
94 | + final DefaultTopologyVertex root = new DefaultTopologyVertex(did("rootnode")); | ||
95 | + final Topology topology = new MockTopology(); | ||
96 | + final TopologyCluster cluster1 = | ||
97 | + new DefaultTopologyCluster(ClusterId.clusterId(0), | ||
98 | + 2, 1, root); | ||
99 | + final TopologyCluster cluster2 = | ||
100 | + new DefaultTopologyCluster(ClusterId.clusterId(1), | ||
101 | + 4, 3, root); | ||
102 | + | ||
103 | + @Override | ||
104 | + public Topology currentTopology() { | ||
105 | + return topology; | ||
106 | + } | ||
107 | + | ||
108 | + @Override | ||
109 | + public boolean isLatest(Topology topology) { | ||
110 | + return true; | ||
111 | + } | ||
112 | + | ||
113 | + @Override | ||
114 | + public TopologyGraph getGraph(Topology topology) { | ||
115 | + return null; | ||
116 | + } | ||
117 | + | ||
118 | + @Override | ||
119 | + public Set<TopologyCluster> getClusters(Topology topology) { | ||
120 | + return ImmutableSet.of(cluster1, cluster2); | ||
121 | + } | ||
122 | + | ||
123 | + @Override | ||
124 | + public TopologyCluster getCluster(Topology topology, ClusterId clusterId) { | ||
125 | + return cluster1; | ||
126 | + } | ||
127 | + | ||
128 | + @Override | ||
129 | + public Set<DeviceId> getClusterDevices(Topology topology, TopologyCluster cluster) { | ||
130 | + DeviceId device1 = did("dev1"); | ||
131 | + DeviceId device2 = did("dev2"); | ||
132 | + | ||
133 | + return ImmutableSet.of(device1, device2); | ||
134 | + } | ||
135 | + | ||
136 | + @Override | ||
137 | + public Set<Link> getClusterLinks(Topology topology, TopologyCluster cluster) { | ||
138 | + Link link1 = link("src1", 1, "dst1", 1); | ||
139 | + Link link2 = link("src2", 1, "dst2", 1); | ||
140 | + Link link3 = link("src3", 1, "dst3", 1); | ||
141 | + return ImmutableSet.of(link1, link2, link3); | ||
142 | + } | ||
143 | + | ||
144 | + @Override | ||
145 | + public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst) { | ||
146 | + return null; | ||
147 | + } | ||
148 | + | ||
149 | + @Override | ||
150 | + public Set<Path> getPaths(Topology topology, DeviceId src, DeviceId dst, LinkWeight weight) { | ||
151 | + return null; | ||
152 | + } | ||
153 | + | ||
154 | + @Override | ||
155 | + public boolean isInfrastructure(Topology topology, ConnectPoint connectPoint) { | ||
156 | + return connectPoint.elementId().toString().equals("dev2"); | ||
157 | + } | ||
158 | + | ||
159 | + @Override | ||
160 | + public boolean isBroadcastPoint(Topology topology, ConnectPoint connectPoint) { | ||
161 | + return connectPoint.elementId().toString().equals("dev1"); | ||
162 | + } | ||
163 | + | ||
164 | + @Override | ||
165 | + public void addListener(TopologyListener listener) { | ||
166 | + | ||
167 | + } | ||
168 | + | ||
169 | + @Override | ||
170 | + public void removeListener(TopologyListener listener) { | ||
171 | + | ||
172 | + } | ||
173 | + } | ||
174 | + | ||
175 | + public TopologyResourceTest() { | ||
176 | + super("org.onosproject.rest"); | ||
177 | + } | ||
178 | + | ||
179 | + | ||
180 | + /** | ||
181 | + * Initializes the test harness. | ||
182 | + */ | ||
183 | + @Before | ||
184 | + public void setUp() { | ||
185 | + TopologyService topologyService = new MockTopologyService(); | ||
186 | + CodecManager codecService = new CodecManager(); | ||
187 | + codecService.activate(); | ||
188 | + | ||
189 | + ServiceDirectory testDirectory = | ||
190 | + new TestServiceDirectory() | ||
191 | + .add(TopologyService.class, topologyService) | ||
192 | + .add(CodecService.class, codecService); | ||
193 | + BaseResource.setServiceDirectory(testDirectory); | ||
194 | + } | ||
195 | + | ||
196 | + /** | ||
197 | + * Tests the topology overview. | ||
198 | + */ | ||
199 | + @Test | ||
200 | + public void getTopology() { | ||
201 | + WebResource rs = resource(); | ||
202 | + String response = rs.path("topology").get(String.class); | ||
203 | + JsonObject result = JsonObject.readFrom(response); | ||
204 | + assertThat(result, notNullValue()); | ||
205 | + | ||
206 | + assertThat(result.names(), hasSize(4)); | ||
207 | + | ||
208 | + assertThat(result.get("time").asLong(), is(11111L)); | ||
209 | + assertThat(result.get("clusters").asLong(), is(2L)); | ||
210 | + assertThat(result.get("devices").asLong(), is(6L)); | ||
211 | + assertThat(result.get("links").asLong(), is(4L)); | ||
212 | + } | ||
213 | + | ||
214 | + /** | ||
215 | + * Tests the clusters overview. | ||
216 | + */ | ||
217 | + @Test | ||
218 | + public void getTopologyClusters() { | ||
219 | + WebResource rs = resource(); | ||
220 | + String response = rs.path("topology/clusters").get(String.class); | ||
221 | + JsonObject result = JsonObject.readFrom(response); | ||
222 | + assertThat(result, notNullValue()); | ||
223 | + | ||
224 | + assertThat(result.names(), hasSize(1)); | ||
225 | + JsonArray clusters = result.get("clusters").asArray(); | ||
226 | + assertThat(clusters, notNullValue()); | ||
227 | + assertThat(clusters.size(), is(2)); | ||
228 | + } | ||
229 | + | ||
230 | + /** | ||
231 | + * Tests an individual cluster overview. | ||
232 | + */ | ||
233 | + @Test | ||
234 | + public void getCluster() { | ||
235 | + WebResource rs = resource(); | ||
236 | + String response = rs.path("topology/clusters/0").get(String.class); | ||
237 | + JsonObject result = JsonObject.readFrom(response); | ||
238 | + assertThat(result, notNullValue()); | ||
239 | + | ||
240 | + assertThat(result.get("id").asLong(), is(0L)); | ||
241 | + assertThat(result.get("deviceCount").asLong(), is(2L)); | ||
242 | + assertThat(result.get("linkCount").asLong(), is(1L)); | ||
243 | + assertThat(result.get("root").asString(), containsString("rootnode")); | ||
244 | + | ||
245 | + assertThat(result.names(), hasSize(4)); | ||
246 | + } | ||
247 | + | ||
248 | + /** | ||
249 | + * Tests an individual cluster's devices list. | ||
250 | + */ | ||
251 | + @Test | ||
252 | + public void getClusterDevices() { | ||
253 | + WebResource rs = resource(); | ||
254 | + String response = rs.path("topology/clusters/0/devices").get(String.class); | ||
255 | + JsonObject result = JsonObject.readFrom(response); | ||
256 | + assertThat(result, notNullValue()); | ||
257 | + | ||
258 | + JsonArray devices = result.get("devices").asArray(); | ||
259 | + assertThat(devices.size(), is(2)); | ||
260 | + | ||
261 | + assertThat(devices.get(0).asString(), is("of:dev1")); | ||
262 | + assertThat(devices.get(1).asString(), is("of:dev2")); | ||
263 | + } | ||
264 | + | ||
265 | + /** | ||
266 | + * Tests an individual cluster's links list. | ||
267 | + */ | ||
268 | + @Test | ||
269 | + public void getClusterLinks() { | ||
270 | + WebResource rs = resource(); | ||
271 | + String response = rs.path("topology/clusters/1/links").get(String.class); | ||
272 | + JsonObject result = JsonObject.readFrom(response); | ||
273 | + assertThat(result, notNullValue()); | ||
274 | + | ||
275 | + JsonArray links = result.get("links").asArray(); | ||
276 | + assertThat(links.size(), is(3)); | ||
277 | + | ||
278 | + JsonObject link0 = links.get(0).asObject(); | ||
279 | + JsonObject src0 = link0.get("src").asObject(); | ||
280 | + String device0 = src0.get("device").asString(); | ||
281 | + assertThat(device0, is("of:src1")); | ||
282 | + | ||
283 | + JsonObject link2 = links.get(2).asObject(); | ||
284 | + JsonObject src2 = link2.get("src").asObject(); | ||
285 | + String device2 = src2.get("device").asString(); | ||
286 | + assertThat(device2, is("of:src3")); | ||
287 | + } | ||
288 | + | ||
289 | + /** | ||
290 | + * Tests a broadcast query. | ||
291 | + */ | ||
292 | + @Test | ||
293 | + public void getBroadcast() { | ||
294 | + WebResource rs = resource(); | ||
295 | + String response = rs.path("topology/broadcast/dev1:1").get(String.class); | ||
296 | + JsonObject result = JsonObject.readFrom(response); | ||
297 | + assertThat(result, notNullValue()); | ||
298 | + | ||
299 | + assertThat(result.get("broadcast").asBoolean(), is(true)); | ||
300 | + } | ||
301 | + | ||
302 | + /** | ||
303 | + * Tests an infrastructure query. | ||
304 | + */ | ||
305 | + @Test | ||
306 | + public void getInfrastructure() { | ||
307 | + WebResource rs = resource(); | ||
308 | + String response = rs.path("topology/infrastructure/dev2:1").get(String.class); | ||
309 | + JsonObject result = JsonObject.readFrom(response); | ||
310 | + assertThat(result, notNullValue()); | ||
311 | + | ||
312 | + assertThat(result.get("infrastructure").asBoolean(), is(true)); | ||
313 | + } | ||
314 | +} |
-
Please register or login to post a comment