Reference Guide

Table Of Contents
@Override
@After
public void tearDown() throws Exception {
super.tearDown();
sl.unregister(SwitchService.class, switchServiceMock);
}
@Test
public void testList() {
// Create mocks and define test case data
List<Switch> switches = Collections.emptyList(); // Create test case
// Recording phase (Define expectations)
EasyMock.expect(
switchServiceMock.getAll().andReturn(switches);
// Execution phase
EasyMock.replay(switchServiceMock);
String response = get(BASE_PATH);
// Verification phase
String expectedResponse = "{\"switches\":[]}";
assertResponseContains(response, expectedResponse);
EasyMock.verify(switchServiceMock);
}
...
}
JSON Encoding
As described previously, the tasks a REST API normally accomplishes is decoding the request and
encoding the result into the response. This sample application uses JSON [36] format but could
have used any other, like XML. There are several different tools to assist on JSON conversion and
any tool and any way of organizing the codecs (or converters) could have been selected.
However, the HP VAN SDN Controller SDK offers some infrastructure classes and services with the
aim of unifying the way JSON codecs are implemented and shared. The generated sample
application is too simple to use a codec, so a more complex example using a codec is presented
here. This example makes use of such JSON API to implement a JSON codec for the Switch model
object so it is used by the SwitchResource.
Implementation of the JSON codecs is located at the hm-rs module; however for real applications
creating a new module to locate codecs might result in a better organization. The listing,
SwitchJsonCodec.java, shows the JSON codec for Switch (Defining Model Objects on page 150).
SwitchJsonCodec.java:
183