Yoonseon Han
Committed by Gerrit Code Review

[ONOS-5324] Issue with Meter POST Rest API

- ConsistentMapException while POST a Meter With non existent DeviceId

Changes
1. added DeviceID check mechanism to confirm
   the requested ID refers an available device.
2. Changed MeterResourceTest to address
   a specific deviceID (of:0000000000000001)

Change-Id: I9ce70a73dc03e9e7516e8aaff3895a66f3b69b13
......@@ -19,6 +19,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.net.DeviceId;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.meter.DefaultMeterRequest;
import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterId;
......@@ -140,12 +141,17 @@ public class MetersWebResource extends AbstractWebResource {
ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
JsonNode specifiedDeviceId = jsonTree.get("deviceId");
if (specifiedDeviceId != null &&
!specifiedDeviceId.asText().equals(deviceId)) {
if ((specifiedDeviceId != null &&
!specifiedDeviceId.asText().equals(deviceId)) ||
get(DeviceService.class).getDevice(DeviceId.deviceId(deviceId))
== null) {
throw new IllegalArgumentException(DEVICE_INVALID);
}
jsonTree.put("deviceId", deviceId);
final MeterRequest meterRequest = codec(MeterRequest.class).decode(jsonTree, this);
final MeterRequest meterRequest = codec(MeterRequest.class)
.decode(jsonTree, this);
final Meter meter = meterService.submit(meterRequest);
UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
......
......@@ -87,10 +87,13 @@ public class MetersResourceTest extends ResourceTest {
final DeviceId deviceId1 = DeviceId.deviceId("1");
final DeviceId deviceId2 = DeviceId.deviceId("2");
final DeviceId deviceId3 = DeviceId.deviceId("3");
final DeviceId deviceId4 = DeviceId.deviceId("of:0000000000000001");
final Device device1 = new DefaultDevice(null, deviceId1, Device.Type.OTHER,
"", "", "", "", null);
final Device device2 = new DefaultDevice(null, deviceId2, Device.Type.OTHER,
"", "", "", "", null);
final Device device4 = new DefaultDevice(null, deviceId4, Device.Type.OTHER,
"", "", "", "", null);
final MockMeter meter1 = new MockMeter(deviceId1, 1, 111, 1);
final MockMeter meter2 = new MockMeter(deviceId1, 2, 222, 2);
......@@ -214,8 +217,10 @@ public class MetersResourceTest extends ResourceTest {
.andReturn(device1);
expect(mockDeviceService.getDevice(deviceId2))
.andReturn(device2);
expect(mockDeviceService.getDevice(deviceId4))
.andReturn(device4);
expect(mockDeviceService.getDevices())
.andReturn(ImmutableSet.of(device1, device2));
.andReturn(ImmutableSet.of(device1, device2, device4));
// Mock Core Service
expect(mockCoreService.getAppId(anyShort()))
......@@ -475,6 +480,7 @@ public class MetersResourceTest extends ResourceTest {
mockMeterService.submit(anyObject());
expectLastCall().andReturn(meter5).anyTimes();
replay(mockMeterService);
replay(mockDeviceService);
WebTarget wt = target();
InputStream jsonStream = MetersResourceTest.class
......@@ -489,6 +495,26 @@ public class MetersResourceTest extends ResourceTest {
}
/**
* Tests creating a meter with POST, but wrong deviceID.
*/
@Test
public void testPostWithWrongDevice() {
mockMeterService.submit(anyObject());
expectLastCall().andReturn(meter5).anyTimes();
replay(mockMeterService);
replay(mockDeviceService);
WebTarget wt = target();
InputStream jsonStream = MetersResourceTest.class
.getResourceAsStream("post-meter.json");
Response response = wt.path("meters/of:0000000000000002")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.json(jsonStream));
assertThat(response.getStatus(), is(HttpURLConnection.HTTP_BAD_REQUEST));
}
/**
* Tests deleting a meter.
*/
@Test
......