Committed by
Ray Milkey
Add a null test case for getMeterByDeviceIdAndMeterId REST method
Change-Id: Icf38aa1a84cc3161bdd0212460c37877a22731bd
Showing
2 changed files
with
26 additions
and
1 deletions
... | @@ -51,6 +51,7 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -51,6 +51,7 @@ import static org.slf4j.LoggerFactory.getLogger; |
51 | public class MetersWebResource extends AbstractWebResource { | 51 | public class MetersWebResource extends AbstractWebResource { |
52 | private final Logger log = getLogger(getClass()); | 52 | private final Logger log = getLogger(getClass()); |
53 | public static final String DEVICE_INVALID = "Invalid deviceId in meter creation request"; | 53 | public static final String DEVICE_INVALID = "Invalid deviceId in meter creation request"; |
54 | + public static final String METER_NOT_FOUND = "Meter is not found for "; | ||
54 | 55 | ||
55 | final MeterService meterService = get(MeterService.class); | 56 | final MeterService meterService = get(MeterService.class); |
56 | final ObjectNode root = mapper().createObjectNode(); | 57 | final ObjectNode root = mapper().createObjectNode(); |
... | @@ -107,7 +108,7 @@ public class MetersWebResource extends AbstractWebResource { | ... | @@ -107,7 +108,7 @@ public class MetersWebResource extends AbstractWebResource { |
107 | MeterId mid = MeterId.meterId(Long.valueOf(meterId)); | 108 | MeterId mid = MeterId.meterId(Long.valueOf(meterId)); |
108 | 109 | ||
109 | final Meter meter = nullIsNotFound(meterService.getMeter(did, mid), | 110 | final Meter meter = nullIsNotFound(meterService.getMeter(did, mid), |
110 | - "Meter is not found for " + mid.id()); | 111 | + METER_NOT_FOUND + mid.id()); |
111 | 112 | ||
112 | metersNode.add(codec(Meter.class).encode(meter, this)); | 113 | metersNode.add(codec(Meter.class).encode(meter, this)); |
113 | return ok(root).build(); | 114 | return ok(root).build(); | ... | ... |
... | @@ -46,6 +46,7 @@ import org.onosproject.net.meter.Meter; | ... | @@ -46,6 +46,7 @@ import org.onosproject.net.meter.Meter; |
46 | import org.onosproject.net.meter.MeterId; | 46 | import org.onosproject.net.meter.MeterId; |
47 | import org.onosproject.net.meter.MeterService; | 47 | import org.onosproject.net.meter.MeterService; |
48 | import org.onosproject.net.meter.MeterState; | 48 | import org.onosproject.net.meter.MeterState; |
49 | +import org.onosproject.rest.resources.CoreWebApplication; | ||
49 | 50 | ||
50 | import javax.ws.rs.core.MediaType; | 51 | import javax.ws.rs.core.MediaType; |
51 | import java.io.InputStream; | 52 | import java.io.InputStream; |
... | @@ -66,6 +67,7 @@ import static org.easymock.EasyMock.replay; | ... | @@ -66,6 +67,7 @@ import static org.easymock.EasyMock.replay; |
66 | import static org.easymock.EasyMock.verify; | 67 | import static org.easymock.EasyMock.verify; |
67 | import static org.hamcrest.Matchers.hasSize; | 68 | import static org.hamcrest.Matchers.hasSize; |
68 | import static org.hamcrest.Matchers.is; | 69 | import static org.hamcrest.Matchers.is; |
70 | +import static org.junit.Assert.assertEquals; | ||
69 | import static org.junit.Assert.assertThat; | 71 | import static org.junit.Assert.assertThat; |
70 | import static org.hamcrest.Matchers.notNullValue; | 72 | import static org.hamcrest.Matchers.notNullValue; |
71 | import static org.onosproject.net.NetTestTools.APP_ID; | 73 | import static org.onosproject.net.NetTestTools.APP_ID; |
... | @@ -94,6 +96,10 @@ public class MetersResourceTest extends ResourceTest { | ... | @@ -94,6 +96,10 @@ public class MetersResourceTest extends ResourceTest { |
94 | final MockMeter meter4 = new MockMeter(deviceId2, 4, 444, 4); | 96 | final MockMeter meter4 = new MockMeter(deviceId2, 4, 444, 4); |
95 | final MockMeter meter5 = new MockMeter(deviceId3, 5, 555, 5); | 97 | final MockMeter meter5 = new MockMeter(deviceId3, 5, 555, 5); |
96 | 98 | ||
99 | + public MetersResourceTest() { | ||
100 | + super(CoreWebApplication.class); | ||
101 | + } | ||
102 | + | ||
97 | /** | 103 | /** |
98 | * Mock class for a meter. | 104 | * Mock class for a meter. |
99 | */ | 105 | */ |
... | @@ -446,6 +452,24 @@ public class MetersResourceTest extends ResourceTest { | ... | @@ -446,6 +452,24 @@ public class MetersResourceTest extends ResourceTest { |
446 | } | 452 | } |
447 | 453 | ||
448 | /** | 454 | /** |
455 | + * Test whether the REST API returns 404 if no entry has been found. | ||
456 | + */ | ||
457 | + @Test | ||
458 | + public void testMeterByDeviceIdAndMeterId() { | ||
459 | + setupMockMeters(); | ||
460 | + | ||
461 | + expect(mockMeterService.getMeter(anyObject(), anyObject())) | ||
462 | + .andReturn(null).anyTimes(); | ||
463 | + replay(mockMeterService); | ||
464 | + | ||
465 | + final WebResource rs = resource(); | ||
466 | + final ClientResponse response = rs.path("meters/" + deviceId3.toString() | ||
467 | + + "/" + "888").get(ClientResponse.class); | ||
468 | + | ||
469 | + assertEquals(404, response.getStatus()); | ||
470 | + } | ||
471 | + | ||
472 | + /** | ||
449 | * Tests creating a meter with POST. | 473 | * Tests creating a meter with POST. |
450 | */ | 474 | */ |
451 | @Test | 475 | @Test | ... | ... |
-
Please register or login to post a comment