Jian Li
Committed by Gerrit Code Review

Add getMetersByDeviceId REST method

Change-Id: I50b9158aac1c8b0789ee271276db0fa701fdb7df
...@@ -73,10 +73,29 @@ public class MetersWebResource extends AbstractWebResource { ...@@ -73,10 +73,29 @@ public class MetersWebResource extends AbstractWebResource {
73 } 73 }
74 74
75 /** 75 /**
76 + * Returns a collection of meters by the device id.
77 + *
78 + * @param deviceId device identifier
79 + * @return array of meters which belongs to specified device
80 + * @onos.rsModel Meters
81 + */
82 + @GET
83 + @Produces(MediaType.APPLICATION_JSON)
84 + @Path("{deviceId}")
85 + public Response getMetersByDeviceId(@PathParam("deviceId") String deviceId) {
86 + DeviceId did = DeviceId.deviceId(deviceId);
87 + final Iterable<Meter> meters = meterService.getMeters(did);
88 + if (meters != null) {
89 + meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
90 + }
91 + return ok(root).build();
92 + }
93 +
94 + /**
76 * Returns a meter by the meter id. 95 * Returns a meter by the meter id.
77 * 96 *
78 * @param deviceId device identifier 97 * @param deviceId device identifier
79 - * @return array of all the groups in the system 98 + * @return a meter, return 404 if no entry has been found
80 * @onos.rsModel Meter 99 * @onos.rsModel Meter
81 */ 100 */
82 @GET 101 @GET
......
...@@ -393,7 +393,35 @@ public class MetersResourceTest extends ResourceTest { ...@@ -393,7 +393,35 @@ public class MetersResourceTest extends ResourceTest {
393 } 393 }
394 394
395 /** 395 /**
396 - * Tests the result of a rest api GET for a device. 396 + * Tests the results of a rest api GET for a device.
397 + */
398 + @Test
399 + public void testMeterSingleDevice() {
400 + setupMockMeters();
401 +
402 + final Set<Meter> meters1 = new HashSet<>();
403 + meters1.add(meter1);
404 + meters1.add(meter2);
405 +
406 + expect(mockMeterService.getMeters(anyObject())).andReturn(meters1).anyTimes();
407 + replay(mockMeterService);
408 + replay(mockDeviceService);
409 +
410 + final WebResource rs = resource();
411 + final String response = rs.path("meters/" + deviceId1.toString()).get(String.class);
412 + final JsonObject result = JsonObject.readFrom(response);
413 + assertThat(result, notNullValue());
414 +
415 + assertThat(result.names(), hasSize(1));
416 + assertThat(result.names().get(0), is("meters"));
417 + final JsonArray jsonMeters = result.get("meters").asArray();
418 + assertThat(jsonMeters, notNullValue());
419 + assertThat(jsonMeters, hasMeter(meter1));
420 + assertThat(jsonMeters, hasMeter(meter2));
421 + }
422 +
423 + /**
424 + * Tests the result of a rest api GET for a device with meter id.
397 */ 425 */
398 @Test 426 @Test
399 public void testMeterSingleDeviceWithId() { 427 public void testMeterSingleDeviceWithId() {
......