Jian Li
Committed by Ray Milkey

Add a null test case for getMeterByDeviceIdAndMeterId REST method

Change-Id: Icf38aa1a84cc3161bdd0212460c37877a22731bd
......@@ -51,6 +51,7 @@ import static org.slf4j.LoggerFactory.getLogger;
public class MetersWebResource extends AbstractWebResource {
private final Logger log = getLogger(getClass());
public static final String DEVICE_INVALID = "Invalid deviceId in meter creation request";
public static final String METER_NOT_FOUND = "Meter is not found for ";
final MeterService meterService = get(MeterService.class);
final ObjectNode root = mapper().createObjectNode();
......@@ -107,7 +108,7 @@ public class MetersWebResource extends AbstractWebResource {
MeterId mid = MeterId.meterId(Long.valueOf(meterId));
final Meter meter = nullIsNotFound(meterService.getMeter(did, mid),
"Meter is not found for " + mid.id());
METER_NOT_FOUND + mid.id());
metersNode.add(codec(Meter.class).encode(meter, this));
return ok(root).build();
......
......@@ -46,6 +46,7 @@ import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterId;
import org.onosproject.net.meter.MeterService;
import org.onosproject.net.meter.MeterState;
import org.onosproject.rest.resources.CoreWebApplication;
import javax.ws.rs.core.MediaType;
import java.io.InputStream;
......@@ -66,6 +67,7 @@ import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.onosproject.net.NetTestTools.APP_ID;
......@@ -94,6 +96,10 @@ public class MetersResourceTest extends ResourceTest {
final MockMeter meter4 = new MockMeter(deviceId2, 4, 444, 4);
final MockMeter meter5 = new MockMeter(deviceId3, 5, 555, 5);
public MetersResourceTest() {
super(CoreWebApplication.class);
}
/**
* Mock class for a meter.
*/
......@@ -446,6 +452,24 @@ public class MetersResourceTest extends ResourceTest {
}
/**
* Test whether the REST API returns 404 if no entry has been found.
*/
@Test
public void testMeterByDeviceIdAndMeterId() {
setupMockMeters();
expect(mockMeterService.getMeter(anyObject(), anyObject()))
.andReturn(null).anyTimes();
replay(mockMeterService);
final WebResource rs = resource();
final ClientResponse response = rs.path("meters/" + deviceId3.toString()
+ "/" + "888").get(ClientResponse.class);
assertEquals(404, response.getStatus());
}
/**
* Tests creating a meter with POST.
*/
@Test
......