DevicesResourceTest.java
12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.rest;
import java.util.List;
import com.eclipsesource.json.Json;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.osgi.ServiceDirectory;
import org.onlab.osgi.TestServiceDirectory;
import org.onlab.rest.BaseResource;
import org.onosproject.codec.CodecService;
import org.onosproject.codec.impl.CodecManager;
import org.onosproject.net.DefaultPort;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.Port;
import org.onosproject.net.device.DeviceService;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.google.common.collect.ImmutableList;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.onosproject.net.NetTestTools.device;
import static org.onosproject.net.NetTestTools.did;
import static org.onosproject.net.PortNumber.portNumber;
/**
* Unit tests for devices REST APIs.
*/
public class DevicesResourceTest extends ResourceTest {
DeviceService mockDeviceService;
/**
* Hamcrest matcher to check that an device representation in JSON matches
* the actual device.
*/
public static class DeviceJsonMatcher extends TypeSafeMatcher<JsonObject> {
private final Device device;
private String reason = "";
public DeviceJsonMatcher(Device deviceValue) {
device = deviceValue;
}
@Override
public boolean matchesSafely(JsonObject jsonDevice) {
// check id
String jsonId = jsonDevice.get("id").asString();
if (!jsonId.equals(device.id().toString())) {
reason = "id " + device.id().toString();
return false;
}
// check type
String jsonType = jsonDevice.get("type").asString();
if (!jsonType.equals(device.type().toString())) {
reason = "appId " + device.type().toString();
return false;
}
// check manufacturer
String jsonManufacturer = jsonDevice.get("mfr").asString();
if (!jsonManufacturer.equals(device.manufacturer())) {
reason = "manufacturer " + device.manufacturer();
return false;
}
// check HW version field
String jsonHwVersion = jsonDevice.get("hw").asString();
if (!jsonHwVersion.equals(device.hwVersion())) {
reason = "hw Version " + device.hwVersion();
return false;
}
// check SW version field
String jsonSwVersion = jsonDevice.get("sw").asString();
if (!jsonSwVersion.equals(device.swVersion())) {
reason = "sw Version " + device.swVersion();
return false;
}
// check serial number field
String jsonSerialNumber = jsonDevice.get("serial").asString();
if (!jsonSerialNumber.equals(device.serialNumber())) {
reason = "serial number " + device.serialNumber();
return false;
}
// check chassis id field
String jsonChassisId = jsonDevice.get("chassisId").asString();
if (!jsonChassisId.equals(device.chassisId().toString())) {
reason = "Chassis id " + device.chassisId().toString();
return false;
}
return true;
}
@Override
public void describeTo(Description description) {
description.appendText(reason);
}
}
/**
* Factory to allocate an device matcher.
*
* @param device device object we are looking for
* @return matcher
*/
private static DeviceJsonMatcher matchesDevice(Device device) {
return new DeviceJsonMatcher(device);
}
/**
* Hamcrest matcher to check that an device is represented properly in a JSON
* array of devices.
*/
private static class DeviceJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
private final Device device;
private String reason = "";
public DeviceJsonArrayMatcher(Device deviceValue) {
device = deviceValue;
}
@Override
public boolean matchesSafely(JsonArray json) {
final int minExpectedAttributes = 9;
final int maxExpectedAttributes = 10;
boolean deviceFound = false;
for (int jsonDeviceIndex = 0; jsonDeviceIndex < json.size();
jsonDeviceIndex++) {
JsonObject jsonDevice = json.get(jsonDeviceIndex).asObject();
if (jsonDevice.names().size() < minExpectedAttributes ||
jsonDevice.names().size() > maxExpectedAttributes) {
reason = "Found a device with the wrong number of attributes";
return false;
}
String jsonDeviceId = jsonDevice.get("id").asString();
if (jsonDeviceId.equals(device.id().toString())) {
deviceFound = true;
// We found the correct device, check attribute values
assertThat(jsonDevice, matchesDevice(device));
}
}
if (!deviceFound) {
reason = "Device with id " + device.id().toString() + " not found";
return false;
} else {
return true;
}
}
@Override
public void describeTo(Description description) {
description.appendText(reason);
}
}
/**
* Factory to allocate an device array matcher.
*
* @param device device object we are looking for
* @return matcher
*/
private static DeviceJsonArrayMatcher hasDevice(Device device) {
return new DeviceJsonArrayMatcher(device);
}
/**
* Initializes test mocks and environment.
*/
@Before
public void setUpMocks() {
mockDeviceService = createMock(DeviceService.class);
expect(mockDeviceService.isAvailable(isA(DeviceId.class)))
.andReturn(true)
.anyTimes();
expect(mockDeviceService.getRole(isA(DeviceId.class)))
.andReturn(MastershipRole.MASTER)
.anyTimes();
// Register the services needed for the test
CodecManager codecService = new CodecManager();
codecService.activate();
ServiceDirectory testDirectory =
new TestServiceDirectory()
.add(DeviceService.class, mockDeviceService)
.add(CodecService.class, codecService);
BaseResource.setServiceDirectory(testDirectory);
}
/**
* Verifies test mocks.
*/
@After
public void tearDownMocks() {
verify(mockDeviceService);
}
/**
* Tests the result of the rest api GET when there are no devices.
*/
@Test
public void testDevicesEmptyArray() {
expect(mockDeviceService.getDevices()).andReturn(ImmutableList.of());
replay(mockDeviceService);
WebResource rs = resource();
String response = rs.path("devices").get(String.class);
assertThat(response, is("{\"devices\":[]}"));
}
/**
* Tests the result of the rest api GET when there are devices present.
*/
@Test
public void testDevices() {
Device device1 = device("dev1");
Device device2 = device("dev2");
Device device3 = device("dev3");
expect(mockDeviceService.getDevices())
.andReturn(ImmutableList.of(device1, device2, device3))
.anyTimes();
replay(mockDeviceService);
WebResource rs = resource();
String response = rs.path("devices").get(String.class);
assertThat(response, containsString("{\"devices\":["));
JsonObject result = Json.parse(response).asObject();
assertThat(result, notNullValue());
assertThat(result.names(), hasSize(1));
assertThat(result.names().get(0), is("devices"));
JsonArray jsonDevices = result.get("devices").asArray();
assertThat(jsonDevices, notNullValue());
assertThat(jsonDevices.size(), is(3));
assertThat(jsonDevices, hasDevice(device1));
assertThat(jsonDevices, hasDevice(device2));
assertThat(jsonDevices, hasDevice(device3));
}
/**
* Tests the result of a rest api GET for a single device.
*/
@Test
public void testDevicesSingle() {
String deviceIdString = "testdevice";
DeviceId deviceId = did(deviceIdString);
Device device = device(deviceIdString);
expect(mockDeviceService.getDevice(deviceId))
.andReturn(device)
.once();
replay(mockDeviceService);
WebResource rs = resource();
String response = rs.path("devices/" + deviceId).get(String.class);
JsonObject result = Json.parse(response).asObject();
assertThat(result, matchesDevice(device));
}
/**
* Tests the result of a rest api GET for the ports of a single device.
*/
@Test
public void testDeviceAndPorts() {
String deviceIdString = "testdevice";
DeviceId deviceId = did(deviceIdString);
Device device = device(deviceIdString);
Port port1 = new DefaultPort(device, portNumber(1), true);
Port port2 = new DefaultPort(device, portNumber(2), true);
Port port3 = new DefaultPort(device, portNumber(3), true);
List<Port> ports = ImmutableList.of(port1, port2, port3);
expect(mockDeviceService.getDevice(deviceId))
.andReturn(device)
.once();
expect(mockDeviceService.getPorts(deviceId))
.andReturn(ports)
.once();
replay(mockDeviceService);
WebResource rs = resource();
String response =
rs.path("devices/" + deviceId + "/ports")
.get(String.class);
JsonObject result = Json.parse(response).asObject();
assertThat(result, matchesDevice(device));
JsonArray jsonPorts = result.get("ports").asArray();
assertThat(jsonPorts.size(), is(3));
for (int portIndex = 0; portIndex < jsonPorts.size(); portIndex++) {
JsonObject jsonPort = jsonPorts.get(portIndex).asObject();
assertThat(jsonPort.get("port").asString(),
is(Integer.toString(portIndex + 1)));
assertThat(jsonPort.get("isEnabled").asBoolean(),
is(true));
assertThat(jsonPort.get("type").asString(),
equalTo("copper"));
assertThat(jsonPort.get("portSpeed").asLong(),
is(1000L));
}
}
/**
* Tests that a fetch of a non-existent device object throws an exception.
*/
@Test
public void testBadGet() {
expect(mockDeviceService.getDevice(isA(DeviceId.class)))
.andReturn(null)
.anyTimes();
replay(mockDeviceService);
WebResource rs = resource();
try {
rs.path("devices/0").get(String.class);
fail("Fetch of non-existent device did not throw an exception");
} catch (UniformInterfaceException ex) {
assertThat(ex.getMessage(),
containsString("returned a response status of"));
}
}
}