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 { ...@@ -112,8 +112,9 @@ public class HostsWebResource extends AbstractWebResource {
112 ObjectNode root = (ObjectNode) mapper().readTree(stream); 112 ObjectNode root = (ObjectNode) mapper().readTree(stream);
113 113
114 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class); 114 HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
115 - InternalHostProvider hostProvider = new InternalHostProvider(hostProviderRegistry); 115 + InternalHostProvider hostProvider = new InternalHostProvider();
116 - hostProvider.register(); 116 + HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
117 + hostProvider.setHostProviderService(hostProviderService);
117 HostId hostId = hostProvider.parseHost(root); 118 HostId hostId = hostProvider.parseHost(root);
118 119
119 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder() 120 UriBuilder locationBuilder = uriInfo.getBaseUriBuilder()
...@@ -121,7 +122,7 @@ public class HostsWebResource extends AbstractWebResource { ...@@ -121,7 +122,7 @@ public class HostsWebResource extends AbstractWebResource {
121 .path(hostId.mac().toString()) 122 .path(hostId.mac().toString())
122 .path(hostId.vlanId().toString()); 123 .path(hostId.vlanId().toString());
123 location = locationBuilder.build(); 124 location = locationBuilder.build();
124 - hostProvider.unregister(); 125 + hostProviderRegistry.unregister(hostProvider);
125 126
126 } catch (IOException ex) { 127 } catch (IOException ex) {
127 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); 128 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
...@@ -131,49 +132,24 @@ public class HostsWebResource extends AbstractWebResource { ...@@ -131,49 +132,24 @@ public class HostsWebResource extends AbstractWebResource {
131 .build(); 132 .build();
132 } 133 }
133 134
134 - /**
135 - * Produces annotations from specified JsonNode. Copied from the ConfigProvider
136 - * class for use in the POST method.
137 - *
138 - * @param node node to be annotated
139 - * @return SparseAnnotations object with information about node
140 - */
141 - private SparseAnnotations annotations(JsonNode node) {
142 - if (node == null) {
143 - return DefaultAnnotations.EMPTY;
144 - }
145 -
146 - DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
147 - Iterator<String> it = node.fieldNames();
148 - while (it.hasNext()) {
149 - String k = it.next();
150 - builder.set(k, node.get(k).asText());
151 - }
152 - return builder.build();
153 - }
154 -
155 private final class InternalHostProvider implements HostProvider { 135 private final class InternalHostProvider implements HostProvider {
156 private final ProviderId providerId = 136 private final ProviderId providerId =
157 new ProviderId("host", "org.onosproject.rest", true); 137 new ProviderId("host", "org.onosproject.rest", true);
158 - private HostProviderRegistry hostProviderRegistry;
159 private HostProviderService hostProviderService; 138 private HostProviderService hostProviderService;
160 139
140 + // Not implemented since there is no need to check for hosts on network
161 public void triggerProbe(Host host) { 141 public void triggerProbe(Host host) {
162 - // No need to implement since we don't need to check if the host exists
163 - }
164 -
165 - private InternalHostProvider(HostProviderRegistry hostProviderRegistry) {
166 - this.hostProviderRegistry = hostProviderRegistry;
167 } 142 }
168 143
169 - private void register() { 144 + // Creates new InternalHostProvider with a HostProviderRegistry param.
170 - this.hostProviderService = hostProviderRegistry.register(this); 145 + private InternalHostProvider() {
171 } 146 }
172 147
173 - private void unregister() { 148 + public void setHostProviderService(HostProviderService service) {
174 - hostProviderRegistry.unregister(this); 149 + this.hostProviderService = service;
175 } 150 }
176 151
152 + // Return the ProviderId of "this"
177 public ProviderId id() { 153 public ProviderId id() {
178 return providerId; 154 return providerId;
179 } 155 }
...@@ -204,6 +180,28 @@ public class HostsWebResource extends AbstractWebResource { ...@@ -204,6 +180,28 @@ public class HostsWebResource extends AbstractWebResource {
204 hostProviderService.hostDetected(hostId, desc); 180 hostProviderService.hostDetected(hostId, desc);
205 return hostId; 181 return hostId;
206 } 182 }
183 +
184 + /**
185 + * Produces annotations from specified JsonNode. Copied from the ConfigProvider
186 + * class for use in the POST method.
187 + *
188 + * @param node node to be annotated
189 + * @return SparseAnnotations object with information about node
190 + */
191 + private SparseAnnotations annotations(JsonNode node) {
192 + if (node == null) {
193 + return DefaultAnnotations.EMPTY;
194 + }
195 +
196 + DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
197 + Iterator<String> it = node.fieldNames();
198 + while (it.hasNext()) {
199 + String k = it.next();
200 + builder.set(k, node.get(k).asText());
201 + }
202 + return builder.build();
203 + }
204 +
207 } 205 }
208 } 206 }
209 207
......
...@@ -16,10 +16,14 @@ ...@@ -16,10 +16,14 @@
16 package org.onosproject.rest; 16 package org.onosproject.rest;
17 17
18 18
19 +import java.io.InputStream;
20 +import java.net.HttpURLConnection;
19 import java.util.HashSet; 21 import java.util.HashSet;
20 import java.util.Set; 22 import java.util.Set;
21 23
24 +import com.sun.jersey.api.client.ClientResponse;
22 import org.hamcrest.Description; 25 import org.hamcrest.Description;
26 +import org.hamcrest.Matchers;
23 import org.hamcrest.TypeSafeMatcher; 27 import org.hamcrest.TypeSafeMatcher;
24 import org.junit.After; 28 import org.junit.After;
25 import org.junit.Before; 29 import org.junit.Before;
...@@ -36,6 +40,8 @@ import org.onosproject.net.DeviceId; ...@@ -36,6 +40,8 @@ import org.onosproject.net.DeviceId;
36 import org.onosproject.net.Host; 40 import org.onosproject.net.Host;
37 import org.onosproject.net.HostId; 41 import org.onosproject.net.HostId;
38 import org.onosproject.net.HostLocation; 42 import org.onosproject.net.HostLocation;
43 +import org.onosproject.net.host.HostProviderRegistry;
44 +import org.onosproject.net.host.HostProviderService;
39 import org.onosproject.net.host.HostService; 45 import org.onosproject.net.host.HostService;
40 import org.onosproject.net.provider.ProviderId; 46 import org.onosproject.net.provider.ProviderId;
41 47
...@@ -45,10 +51,9 @@ import com.google.common.collect.ImmutableSet; ...@@ -45,10 +51,9 @@ import com.google.common.collect.ImmutableSet;
45 import com.sun.jersey.api.client.UniformInterfaceException; 51 import com.sun.jersey.api.client.UniformInterfaceException;
46 import com.sun.jersey.api.client.WebResource; 52 import com.sun.jersey.api.client.WebResource;
47 53
48 -import static org.easymock.EasyMock.createMock; 54 +import javax.ws.rs.core.MediaType;
49 -import static org.easymock.EasyMock.expect; 55 +
50 -import static org.easymock.EasyMock.replay; 56 +import static org.easymock.EasyMock.*;
51 -import static org.easymock.EasyMock.verify;
52 import static org.hamcrest.Matchers.containsString; 57 import static org.hamcrest.Matchers.containsString;
53 import static org.hamcrest.Matchers.hasSize; 58 import static org.hamcrest.Matchers.hasSize;
54 import static org.hamcrest.Matchers.is; 59 import static org.hamcrest.Matchers.is;
...@@ -65,6 +70,8 @@ import static org.onosproject.net.PortNumber.portNumber; ...@@ -65,6 +70,8 @@ import static org.onosproject.net.PortNumber.portNumber;
65 */ 70 */
66 public class HostResourceTest extends ResourceTest { 71 public class HostResourceTest extends ResourceTest {
67 final HostService mockHostService = createMock(HostService.class); 72 final HostService mockHostService = createMock(HostService.class);
73 + final HostProviderRegistry mockHostProviderRegistry = createMock(HostProviderRegistry.class);
74 + final HostProviderService mockHostProviderService = createMock(HostProviderService.class);
68 final HashSet<Host> hosts = new HashSet<>(); 75 final HashSet<Host> hosts = new HashSet<>();
69 76
70 /** 77 /**
...@@ -80,8 +87,8 @@ public class HostResourceTest extends ResourceTest { ...@@ -80,8 +87,8 @@ public class HostResourceTest extends ResourceTest {
80 ServiceDirectory testDirectory = 87 ServiceDirectory testDirectory =
81 new TestServiceDirectory() 88 new TestServiceDirectory()
82 .add(HostService.class, mockHostService) 89 .add(HostService.class, mockHostService)
83 - .add(CodecService.class, codecService); 90 + .add(CodecService.class, codecService)
84 - 91 + .add(HostProviderRegistry.class, mockHostProviderRegistry);
85 BaseResource.setServiceDirectory(testDirectory); 92 BaseResource.setServiceDirectory(testDirectory);
86 } 93 }
87 94
...@@ -352,5 +359,32 @@ public class HostResourceTest extends ResourceTest { ...@@ -352,5 +359,32 @@ public class HostResourceTest extends ResourceTest {
352 } 359 }
353 } 360 }
354 361
362 + /**
363 + * Tests post of a single host via JSON stream.
364 + */
365 + @Test
366 + public void testPost() {
367 + mockHostProviderService.hostDetected(anyObject(), anyObject());
368 + expectLastCall();
369 + replay(mockHostProviderService);
370 +
371 + expect(mockHostProviderRegistry.register(anyObject())).andReturn(mockHostProviderService);
372 + mockHostProviderRegistry.unregister(anyObject());
373 + expectLastCall();
374 + replay(mockHostProviderRegistry);
375 +
376 + replay(mockHostService);
377 +
378 + InputStream jsonStream = IntentsResourceTest.class
379 + .getResourceAsStream("post-host.json");
380 + WebResource rs = resource();
381 +
382 + ClientResponse response = rs.path("hosts")
383 + .type(MediaType.APPLICATION_JSON_TYPE)
384 + .post(ClientResponse.class, jsonStream);
385 + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_CREATED));
386 + String location = response.getLocation().getPath();
387 + assertThat(location, Matchers.startsWith("/hosts/11:22:33:44:55:66/-1"));
388 + }
355 } 389 }
356 390
......
1 +{
2 + "id": "11:22:33:44:55:66/-1",
3 + "vlan": "-1",
4 + "mac": "11:22:33:44:55:66",
5 + "location": {
6 + "port": 3,
7 + "elementId": "of:0000000000000001"
8 + },
9 + "ipAddresses": [
10 + "10.10.10.10"
11 + ]
12 +}