Kedar Gupta
Committed by Gerrit Code Review

onos-2420 Add unit test for Host REST API and fix documentation errors.

Change-Id: I633d6a8ed75dd3f79f134873967615c196f7e14b
......@@ -112,8 +112,9 @@ public class HostsWebResource extends AbstractWebResource {
ObjectNode root = (ObjectNode) mapper().readTree(stream);
HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
InternalHostProvider hostProvider = new InternalHostProvider(hostProviderRegistry);
hostProvider.register();
InternalHostProvider hostProvider = new InternalHostProvider();
HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
hostProvider.setHostProviderService(hostProviderService);
HostId hostId = hostProvider.parseHost(root);
UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
......@@ -121,7 +122,7 @@ public class HostsWebResource extends AbstractWebResource {
.path(hostId.mac().toString())
.path(hostId.vlanId().toString());
location = locationBuilder.build();
hostProvider.unregister();
hostProviderRegistry.unregister(hostProvider);
} catch (IOException ex) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
......@@ -131,49 +132,24 @@ public class HostsWebResource extends AbstractWebResource {
.build();
}
/**
* Produces annotations from specified JsonNode. Copied from the ConfigProvider
* class for use in the POST method.
*
* @param node node to be annotated
* @return SparseAnnotations object with information about node
*/
private SparseAnnotations annotations(JsonNode node) {
if (node == null) {
return DefaultAnnotations.EMPTY;
}
DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
Iterator<String> it = node.fieldNames();
while (it.hasNext()) {
String k = it.next();
builder.set(k, node.get(k).asText());
}
return builder.build();
}
private final class InternalHostProvider implements HostProvider {
private final ProviderId providerId =
new ProviderId("host", "org.onosproject.rest", true);
private HostProviderRegistry hostProviderRegistry;
private HostProviderService hostProviderService;
// Not implemented since there is no need to check for hosts on network
public void triggerProbe(Host host) {
// No need to implement since we don't need to check if the host exists
}
private InternalHostProvider(HostProviderRegistry hostProviderRegistry) {
this.hostProviderRegistry = hostProviderRegistry;
}
private void register() {
this.hostProviderService = hostProviderRegistry.register(this);
// Creates new InternalHostProvider with a HostProviderRegistry param.
private InternalHostProvider() {
}
private void unregister() {
hostProviderRegistry.unregister(this);
public void setHostProviderService(HostProviderService service) {
this.hostProviderService = service;
}
// Return the ProviderId of "this"
public ProviderId id() {
return providerId;
}
......@@ -204,6 +180,28 @@ public class HostsWebResource extends AbstractWebResource {
hostProviderService.hostDetected(hostId, desc);
return hostId;
}
/**
* Produces annotations from specified JsonNode. Copied from the ConfigProvider
* class for use in the POST method.
*
* @param node node to be annotated
* @return SparseAnnotations object with information about node
*/
private SparseAnnotations annotations(JsonNode node) {
if (node == null) {
return DefaultAnnotations.EMPTY;
}
DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
Iterator<String> it = node.fieldNames();
while (it.hasNext()) {
String k = it.next();
builder.set(k, node.get(k).asText());
}
return builder.build();
}
}
}
......
......@@ -16,10 +16,14 @@
package org.onosproject.rest;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashSet;
import java.util.Set;
import com.sun.jersey.api.client.ClientResponse;
import org.hamcrest.Description;
import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeMatcher;
import org.junit.After;
import org.junit.Before;
......@@ -36,6 +40,8 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.Host;
import org.onosproject.net.HostId;
import org.onosproject.net.HostLocation;
import org.onosproject.net.host.HostProviderRegistry;
import org.onosproject.net.host.HostProviderService;
import org.onosproject.net.host.HostService;
import org.onosproject.net.provider.ProviderId;
......@@ -45,10 +51,9 @@ import com.google.common.collect.ImmutableSet;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import javax.ws.rs.core.MediaType;
import static org.easymock.EasyMock.*;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
......@@ -65,6 +70,8 @@ import static org.onosproject.net.PortNumber.portNumber;
*/
public class HostResourceTest extends ResourceTest {
final HostService mockHostService = createMock(HostService.class);
final HostProviderRegistry mockHostProviderRegistry = createMock(HostProviderRegistry.class);
final HostProviderService mockHostProviderService = createMock(HostProviderService.class);
final HashSet<Host> hosts = new HashSet<>();
/**
......@@ -80,8 +87,8 @@ public class HostResourceTest extends ResourceTest {
ServiceDirectory testDirectory =
new TestServiceDirectory()
.add(HostService.class, mockHostService)
.add(CodecService.class, codecService);
.add(CodecService.class, codecService)
.add(HostProviderRegistry.class, mockHostProviderRegistry);
BaseResource.setServiceDirectory(testDirectory);
}
......@@ -352,5 +359,32 @@ public class HostResourceTest extends ResourceTest {
}
}
/**
* Tests post of a single host via JSON stream.
*/
@Test
public void testPost() {
mockHostProviderService.hostDetected(anyObject(), anyObject());
expectLastCall();
replay(mockHostProviderService);
expect(mockHostProviderRegistry.register(anyObject())).andReturn(mockHostProviderService);
mockHostProviderRegistry.unregister(anyObject());
expectLastCall();
replay(mockHostProviderRegistry);
replay(mockHostService);
InputStream jsonStream = IntentsResourceTest.class
.getResourceAsStream("post-host.json");
WebResource rs = resource();
ClientResponse response = rs.path("hosts")
.type(MediaType.APPLICATION_JSON_TYPE)
.post(ClientResponse.class, jsonStream);
assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
String location = response.getLocation().getPath();
assertThat(location, Matchers.startsWith("/hosts/11:22:33:44:55:66/-1"));
}
}
......
{
"id": "11:22:33:44:55:66/-1",
"vlan": "-1",
"mac": "11:22:33:44:55:66",
"location": {
"port": 3,
"elementId": "of:0000000000000001"
},
"ipAddresses": [
"10.10.10.10"
]
}