blob: 2a7a162a7855401ba0122b8ed1cd71e07e600703 [file] [log] [blame]
Ray Milkey1f95bd32014-12-10 11:11:00 -08001/*
2 * Copyright 2014 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
18
19import java.util.HashSet;
20import java.util.Set;
21
22import org.hamcrest.Description;
23import org.hamcrest.TypeSafeMatcher;
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.packet.IpAddress;
30import org.onlab.packet.MacAddress;
31import org.onlab.rest.BaseResource;
32import org.onosproject.codec.CodecService;
33import org.onosproject.codec.impl.CodecManager;
34import org.onosproject.net.DefaultHost;
35import org.onosproject.net.DeviceId;
36import org.onosproject.net.Host;
37import org.onosproject.net.HostId;
38import org.onosproject.net.HostLocation;
39import org.onosproject.net.host.HostService;
40import org.onosproject.net.provider.ProviderId;
41
42import com.eclipsesource.json.JsonArray;
43import com.eclipsesource.json.JsonObject;
44import com.google.common.collect.ImmutableSet;
45import com.sun.jersey.api.client.WebResource;
46import com.sun.jersey.test.framework.JerseyTest;
47
48import static org.easymock.EasyMock.createMock;
49import static org.easymock.EasyMock.expect;
50import static org.easymock.EasyMock.replay;
51import static org.easymock.EasyMock.verify;
52import static org.hamcrest.Matchers.containsString;
53import static org.hamcrest.Matchers.hasSize;
54import static org.hamcrest.Matchers.is;
55import static org.hamcrest.Matchers.notNullValue;
56import static org.junit.Assert.assertThat;
57import static org.onlab.packet.MacAddress.valueOf;
58import static org.onlab.packet.VlanId.vlanId;
59import static org.onosproject.net.PortNumber.portNumber;
60
61/**
62 * Simple example on how to write a JAX-RS unit test using Jersey test framework.
63 * A base class should/will be created to provide further assistance for testing.
64 */
65public class HostResourceTest extends JerseyTest {
66 final HostService mockHostService = createMock(HostService.class);
67 final HashSet<Host> hosts = new HashSet<>();
68
69 public HostResourceTest() {
70 super("org.onosproject.rest");
71 }
72
73 @Before
74 public void setUp() {
75 expect(mockHostService.getHosts()).andReturn(hosts).anyTimes();
76
77 // Register the services needed for the test
78 final CodecManager codecService = new CodecManager();
79 codecService.activate();
80 ServiceDirectory testDirectory =
81 new TestServiceDirectory()
82 .add(HostService.class, mockHostService)
83 .add(CodecService.class, codecService);
84
85 BaseResource.setServiceDirectory(testDirectory);
86 }
87
88 @After
89 public void tearDown() throws Exception {
90 super.tearDown();
91 verify(mockHostService);
92 }
93
94 /**
95 * Hamcrest matcher to check that a host representation in JSON matches
96 * the actual host.
97 */
98 public static class HostJsonMatcher extends TypeSafeMatcher<JsonObject> {
99 private final Host host;
100 private String reason = "";
101
102 public HostJsonMatcher(Host hostValue) {
103 host = hostValue;
104 }
105
106 @Override
107 public boolean matchesSafely(JsonObject jsonHost) {
108 // Check id
109 final String jsonId = jsonHost.get("id").asString();
110 if (!jsonId.equals(host.id().toString())) {
111 reason = "id " + host.id().toString();
112 return false;
113 }
114
115 // Check vlan id
116 final String jsonVlanId = jsonHost.get("vlan").asString();
117 if (!jsonVlanId.equals(host.vlan().toString())) {
118 reason = "vlan id " + host.vlan().toString();
119 return false;
120 }
121
122 // Check mac address
123 final String jsonMacAddress = jsonHost.get("mac").asString();
124 if (!jsonMacAddress.equals(host.mac().toString())) {
125 reason = "mac address " + host.mac().toString();
126 return false;
127 }
128
129 // Check location element id
130 final JsonObject jsonLocation = jsonHost.get("location").asObject();
131 final String jsonLocationElementId = jsonLocation.get("elementId").asString();
132 if (!jsonLocationElementId.equals(host.location().elementId().toString())) {
133 reason = "location element id " + host.location().elementId().toString();
134 return false;
135 }
136
137 // Check location port number
138 final String jsonLocationPortNumber = jsonLocation.get("port").asString();
139 if (!jsonLocationPortNumber.equals(host.location().port().toString())) {
140 reason = "location portNumber " + host.location().port().toString();
141 return false;
142 }
143
144 // Check Ip Addresses
145 final JsonArray jsonHostIps = jsonHost.get("ipAddresses").asArray();
146 final Set<IpAddress> expectedHostIps = host.ipAddresses();
147 if (jsonHostIps.size() != expectedHostIps.size()) {
148 reason = "IP address arrays differ in size";
149 return false;
150 }
151
152 return true;
153 }
154
155 @Override
156 public void describeTo(Description description) {
157 description.appendText(reason);
158 }
159 }
160
161 /**
162 * Factory to allocate a host array matcher.
163 *
164 * @param host host object we are looking for
165 * @return matcher
166 */
167 private static HostJsonMatcher matchesHost(Host host) {
168 return new HostJsonMatcher(host);
169 }
170
171 /**
172 * Hamcrest matcher to check that a host is represented properly in a JSON
173 * array of hosts.
174 */
175 public static class HostJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
176 private final Host host;
177 private String reason = "";
178
179 public HostJsonArrayMatcher(Host hostValue) {
180 host = hostValue;
181 }
182
183 @Override
184 public boolean matchesSafely(JsonArray json) {
185 boolean hostFound = false;
186 final int expectedAttributes = 5;
187 for (int jsonHostIndex = 0; jsonHostIndex < json.size();
188 jsonHostIndex++) {
189
190 final JsonObject jsonHost = json.get(jsonHostIndex).asObject();
191
192 if (jsonHost.names().size() != expectedAttributes) {
193 reason = "Found a host with the wrong number of attributes";
194 return false;
195 }
196
197 final String jsonHostId = jsonHost.get("id").asString();
198 if (jsonHostId.equals(host.id().toString())) {
199 hostFound = true;
200
201 // We found the correct host, check attribute values
202 assertThat(jsonHost, matchesHost(host));
203 }
204 }
205 if (!hostFound) {
206 reason = "Host with id " + host.id().toString() + " not found";
207 return false;
208 } else {
209 return true;
210 }
211 }
212
213 @Override
214 public void describeTo(Description description) {
215 description.appendText(reason);
216 }
217 }
218
219 /**
220 * Factory to allocate a host array matcher.
221 *
222 * @param host host object we are looking for
223 * @return matcher
224 */
225 private static HostJsonArrayMatcher hasHost(Host host) {
226 return new HostJsonArrayMatcher(host);
227 }
228
229 /**
230 * Tests the result of the rest api GET when there are no hosts.
231 */
232 @Test
233 public void testHostsEmptyArray() {
234 replay(mockHostService);
235 WebResource rs = resource();
236 String response = rs.path("hosts").get(String.class);
237 assertThat(response, is("{\"hosts\":[]}"));
238 }
239
240 /**
241 * Tests the result of the rest api GET when hosts are defined.
242 */
243 @Test
244 public void testHostsArray() {
245 replay(mockHostService);
246 final ProviderId pid = new ProviderId("of", "foo");
247 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
248 final Set<IpAddress> ips1 = ImmutableSet.of(IpAddress.valueOf("1111:1111:1111:1::"));
249 final Host host1 =
250 new DefaultHost(pid, HostId.hostId(mac1), valueOf(1), vlanId((short) 1),
251 new HostLocation(DeviceId.deviceId("1"), portNumber(11), 1),
252 ips1);
253 final MacAddress mac2 = MacAddress.valueOf("00:00:11:00:00:02");
254 final Set<IpAddress> ips2 = ImmutableSet.of(
255 IpAddress.valueOf("2222:2222:2222:1::"),
256 IpAddress.valueOf("2222:2222:2222:2::"));
257 final Host host2 =
258 new DefaultHost(pid, HostId.hostId(mac2), valueOf(2), vlanId((short) 2),
259 new HostLocation(DeviceId.deviceId("2"), portNumber(22), 2),
260 ips2);
261 hosts.add(host1);
262 hosts.add(host2);
263 WebResource rs = resource();
264 String response = rs.path("hosts").get(String.class);
265 assertThat(response, containsString("{\"hosts\":["));
266
267 final JsonObject result = JsonObject.readFrom(response);
268 assertThat(result, notNullValue());
269
270 assertThat(result.names(), hasSize(1));
271 assertThat(result.names().get(0), is("hosts"));
272
273 final JsonArray hosts = result.get("hosts").asArray();
274 assertThat(hosts, notNullValue());
275
276 assertThat(hosts, hasHost(host1));
277 assertThat(hosts, hasHost(host2));
278 }
279
280 /**
281 * Tests fetch of one host by Id.
282 */
283 @Test
284 public void testSingleHostByIdFetch() {
285 final ProviderId pid = new ProviderId("of", "foo");
286 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
287 final Set<IpAddress> ips1 = ImmutableSet.of(IpAddress.valueOf("1111:1111:1111:1::"));
288 final Host host1 =
289 new DefaultHost(pid, HostId.hostId(mac1), valueOf(1), vlanId((short) 1),
290 new HostLocation(DeviceId.deviceId("1"), portNumber(11), 1),
291 ips1);
292
293 hosts.add(host1);
294
295 expect(mockHostService.getHost(HostId.hostId("00:00:11:00:00:01/1")))
296 .andReturn(host1)
297 .anyTimes();
298 replay(mockHostService);
299
300 WebResource rs = resource();
301 String response = rs.path("hosts/00:00:11:00:00:01%2F1").get(String.class);
302 final JsonObject result = JsonObject.readFrom(response);
303 assertThat(result, matchesHost(host1));
304 }
305
306 /**
307 * Tests fetch of one host by mac and vlan.
308 */
309 @Test
310 public void testSingleHostByMacAndVlanFetch() {
311 final ProviderId pid = new ProviderId("of", "foo");
312 final MacAddress mac1 = MacAddress.valueOf("00:00:11:00:00:01");
313 final Set<IpAddress> ips1 = ImmutableSet.of(IpAddress.valueOf("1111:1111:1111:1::"));
314 final Host host1 =
315 new DefaultHost(pid, HostId.hostId(mac1), valueOf(1), vlanId((short) 1),
316 new HostLocation(DeviceId.deviceId("1"), portNumber(11), 1),
317 ips1);
318
319 hosts.add(host1);
320
321 expect(mockHostService.getHost(HostId.hostId("00:00:11:00:00:01/1")))
322 .andReturn(host1)
323 .anyTimes();
324 replay(mockHostService);
325
326 WebResource rs = resource();
327 String response = rs.path("hosts/00:00:11:00:00:01/1").get(String.class);
328 final JsonObject result = JsonObject.readFrom(response);
329 assertThat(result, matchesHost(host1));
330 }
331
332}
333