SDN Controller Programming Guide
150
}
ServiceAssistant shows an alternative way of declaring dependencies. ServiceAssistant is
annotated with @References instead of declaring a variable of type SwitchService and then
annotate it with @Reference as in Consuming Services with OSGi Declarative Services on page
134 under the Dependent SwitchComponent.java listing. In this case we wouldn’t use the variable
since we pass the bound service to the ServiceLocator.
The sample application’s domain service (SwitchService) is ready to be used by the REST layer.
The following listing shows an extract of a modified SwitchResource (from Creating Domain
Service Resource (REST Interface of Business Logic Service) on page 137) that makes use of the
inherited get(Class<?>) method to get a reference of SwitchService.
Consuming Domain Services:
package com.hp.hm.rs;
import com.hp.hm.api.SwitchService;
...
@Path("switches")
public class SwitchResource extends ControllerResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response list() {
SwitchService service = get(SwitchService.class);
List<Switch> switches = service.find(null, null);
String result = "{switches:{}}"; // TODO: Encode switches
return ok(result).build();
}
...
}
The following SwitchResourceTest.java Mocking Domain Services listing shows an extract of a
modified SwitchResourceTest (from RESTful Web Services Unit Test on page 146) that uses
EasyMock [38] to mock SwitchService. Note how SwitchResourceTest registers the service mock
before the test and unregisters it after.
SwitchResourceTest.java Mocking Domain Services:
package com.hp.hm.rs;
import org.easymock.EasyMock;
import com.hp.hm.api.SwitchService;
...
public class SwitchResourceTest extends ControllerResourceTest {
private static final String BASE_PATH = "switches";
private SwitchService switchServiceMock;
public SwitchResourceTest() {
super("com.hp.hm.rs");
}