Jian Li
Committed by Gerrit Code Review

Add getMetersByDeviceId REST method

Change-Id: I50b9158aac1c8b0789ee271276db0fa701fdb7df
......@@ -73,10 +73,29 @@ public class MetersWebResource extends AbstractWebResource {
}
/**
* Returns a collection of meters by the device id.
*
* @param deviceId device identifier
* @return array of meters which belongs to specified device
* @onos.rsModel Meters
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{deviceId}")
public Response getMetersByDeviceId(@PathParam("deviceId") String deviceId) {
DeviceId did = DeviceId.deviceId(deviceId);
final Iterable<Meter> meters = meterService.getMeters(did);
if (meters != null) {
meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
}
return ok(root).build();
}
/**
* Returns a meter by the meter id.
*
* @param deviceId device identifier
* @return array of all the groups in the system
* @return a meter, return 404 if no entry has been found
* @onos.rsModel Meter
*/
@GET
......
......@@ -393,7 +393,35 @@ public class MetersResourceTest extends ResourceTest {
}
/**
* Tests the result of a rest api GET for a device.
* Tests the results of a rest api GET for a device.
*/
@Test
public void testMeterSingleDevice() {
setupMockMeters();
final Set<Meter> meters1 = new HashSet<>();
meters1.add(meter1);
meters1.add(meter2);
expect(mockMeterService.getMeters(anyObject())).andReturn(meters1).anyTimes();
replay(mockMeterService);
replay(mockDeviceService);
final WebResource rs = resource();
final String response = rs.path("meters/" + deviceId1.toString()).get(String.class);
final JsonObject result = JsonObject.readFrom(response);
assertThat(result, notNullValue());
assertThat(result.names(), hasSize(1));
assertThat(result.names().get(0), is("meters"));
final JsonArray jsonMeters = result.get("meters").asArray();
assertThat(jsonMeters, notNullValue());
assertThat(jsonMeters, hasMeter(meter1));
assertThat(jsonMeters, hasMeter(meter2));
}
/**
* Tests the result of a rest api GET for a device with meter id.
*/
@Test
public void testMeterSingleDeviceWithId() {
......