Committed by
Gerrit Code Review
[ONOS-3164]UT for Port chain web resource
Change-Id: I0795a0d93518fe6c1481cd0597b8a5f937dce662
Showing
3 changed files
with
287 additions
and
20 deletions
| ... | @@ -15,7 +15,6 @@ | ... | @@ -15,7 +15,6 @@ |
| 15 | */ | 15 | */ |
| 16 | package org.onosproject.vtnweb.resources; | 16 | package org.onosproject.vtnweb.resources; |
| 17 | 17 | ||
| 18 | -import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
| 19 | import static javax.ws.rs.core.Response.Status.OK; | 18 | import static javax.ws.rs.core.Response.Status.OK; |
| 20 | import static org.onlab.util.Tools.nullIsNotFound; | 19 | import static org.onlab.util.Tools.nullIsNotFound; |
| 21 | 20 | ||
| ... | @@ -37,11 +36,11 @@ import org.onosproject.rest.AbstractWebResource; | ... | @@ -37,11 +36,11 @@ import org.onosproject.rest.AbstractWebResource; |
| 37 | import org.onosproject.vtnrsc.PortChain; | 36 | import org.onosproject.vtnrsc.PortChain; |
| 38 | import org.onosproject.vtnrsc.PortChainId; | 37 | import org.onosproject.vtnrsc.PortChainId; |
| 39 | import org.onosproject.vtnrsc.portchain.PortChainService; | 38 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 40 | -import org.onosproject.vtnweb.web.PortChainCodec; | ||
| 41 | import org.slf4j.Logger; | 39 | import org.slf4j.Logger; |
| 42 | import org.slf4j.LoggerFactory; | 40 | import org.slf4j.LoggerFactory; |
| 43 | 41 | ||
| 44 | -import com.fasterxml.jackson.databind.ObjectMapper; | 42 | +import com.fasterxml.jackson.databind.JsonNode; |
| 43 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
| 45 | import com.fasterxml.jackson.databind.node.ObjectNode; | 44 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 46 | 45 | ||
| 47 | /** | 46 | /** |
| ... | @@ -52,7 +51,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; | ... | @@ -52,7 +51,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
| 52 | public class PortChainWebResource extends AbstractWebResource { | 51 | public class PortChainWebResource extends AbstractWebResource { |
| 53 | 52 | ||
| 54 | private final Logger log = LoggerFactory.getLogger(PortChainWebResource.class); | 53 | private final Logger log = LoggerFactory.getLogger(PortChainWebResource.class); |
| 55 | - private final PortChainService service = get(PortChainService.class); | ||
| 56 | public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found"; | 54 | public static final String PORT_CHAIN_NOT_FOUND = "Port chain not found"; |
| 57 | public static final String PORT_CHAIN_ID_EXIST = "Port chain exists"; | 55 | public static final String PORT_CHAIN_ID_EXIST = "Port chain exists"; |
| 58 | public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier"; | 56 | public static final String PORT_CHAIN_ID_NOT_EXIST = "Port chain does not exist with identifier"; |
| ... | @@ -65,10 +63,15 @@ public class PortChainWebResource extends AbstractWebResource { | ... | @@ -65,10 +63,15 @@ public class PortChainWebResource extends AbstractWebResource { |
| 65 | @GET | 63 | @GET |
| 66 | @Produces(MediaType.APPLICATION_JSON) | 64 | @Produces(MediaType.APPLICATION_JSON) |
| 67 | public Response getPortChains() { | 65 | public Response getPortChains() { |
| 68 | - Iterable<PortChain> portChains = service.getPortChains(); | 66 | + Iterable<PortChain> portChains = get(PortChainService.class).getPortChains(); |
| 69 | - ObjectNode result = new ObjectMapper().createObjectNode(); | 67 | + ObjectNode result = mapper().createObjectNode(); |
| 70 | - result.set("port_chains", new PortChainCodec().encode(portChains, this)); | 68 | + ArrayNode portChainEntry = result.putArray("port_chains"); |
| 71 | - return ok(result).build(); | 69 | + if (portChains != null) { |
| 70 | + for (final PortChain portChain : portChains) { | ||
| 71 | + portChainEntry.add(codec(PortChain.class).encode(portChain, this)); | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + return ok(result.toString()).build(); | ||
| 72 | } | 75 | } |
| 73 | 76 | ||
| 74 | /** | 77 | /** |
| ... | @@ -82,14 +85,11 @@ public class PortChainWebResource extends AbstractWebResource { | ... | @@ -82,14 +85,11 @@ public class PortChainWebResource extends AbstractWebResource { |
| 82 | @Produces(MediaType.APPLICATION_JSON) | 85 | @Produces(MediaType.APPLICATION_JSON) |
| 83 | public Response getPortPain(@PathParam("chain_id") String id) { | 86 | public Response getPortPain(@PathParam("chain_id") String id) { |
| 84 | 87 | ||
| 85 | - if (!service.exists(PortChainId.of(id))) { | 88 | + PortChain portChain = nullIsNotFound(get(PortChainService.class).getPortChain(PortChainId.of(id)), |
| 86 | - return Response.status(NOT_FOUND).entity(PORT_CHAIN_NOT_FOUND).build(); | ||
| 87 | - } | ||
| 88 | - PortChain portChain = nullIsNotFound(service.getPortChain(PortChainId.of(id)), | ||
| 89 | PORT_CHAIN_NOT_FOUND); | 89 | PORT_CHAIN_NOT_FOUND); |
| 90 | - ObjectNode result = new ObjectMapper().createObjectNode(); | 90 | + ObjectNode result = mapper().createObjectNode(); |
| 91 | - result.set("port_chain", new PortChainCodec().encode(portChain, this)); | 91 | + result.set("port_chain", codec(PortChain.class).encode(portChain, this)); |
| 92 | - return ok(result).build(); | 92 | + return ok(result.toString()).build(); |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | /** | 95 | /** |
| ... | @@ -105,8 +105,10 @@ public class PortChainWebResource extends AbstractWebResource { | ... | @@ -105,8 +105,10 @@ public class PortChainWebResource extends AbstractWebResource { |
| 105 | public Response createPortChain(InputStream stream) { | 105 | public Response createPortChain(InputStream stream) { |
| 106 | try { | 106 | try { |
| 107 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | 107 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 108 | - PortChain portChain = codec(PortChain.class).decode(jsonTree, this); | 108 | + JsonNode port = jsonTree.get("port_chain"); |
| 109 | - Boolean issuccess = nullIsNotFound(service.createPortChain(portChain), PORT_CHAIN_NOT_FOUND); | 109 | + PortChain portChain = codec(PortChain.class).decode((ObjectNode) port, this); |
| 110 | + Boolean issuccess = nullIsNotFound(get(PortChainService.class).createPortChain(portChain), | ||
| 111 | + PORT_CHAIN_NOT_FOUND); | ||
| 110 | return Response.status(OK).entity(issuccess.toString()).build(); | 112 | return Response.status(OK).entity(issuccess.toString()).build(); |
| 111 | } catch (IOException e) { | 113 | } catch (IOException e) { |
| 112 | log.error("Exception while creating port chain {}.", e.toString()); | 114 | log.error("Exception while creating port chain {}.", e.toString()); |
| ... | @@ -129,8 +131,10 @@ public class PortChainWebResource extends AbstractWebResource { | ... | @@ -129,8 +131,10 @@ public class PortChainWebResource extends AbstractWebResource { |
| 129 | final InputStream stream) { | 131 | final InputStream stream) { |
| 130 | try { | 132 | try { |
| 131 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | 133 | ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); |
| 132 | - PortChain portChain = codec(PortChain.class).decode(jsonTree, this); | 134 | + JsonNode port = jsonTree.get("port_chain"); |
| 133 | - Boolean result = nullIsNotFound(service.updatePortChain(portChain), PORT_CHAIN_NOT_FOUND); | 135 | + PortChain portChain = codec(PortChain.class).decode((ObjectNode) port, this); |
| 136 | + Boolean result = nullIsNotFound(get(PortChainService.class).updatePortChain(portChain), | ||
| 137 | + PORT_CHAIN_NOT_FOUND); | ||
| 134 | return Response.status(OK).entity(result.toString()).build(); | 138 | return Response.status(OK).entity(result.toString()).build(); |
| 135 | } catch (IOException e) { | 139 | } catch (IOException e) { |
| 136 | log.error("Update port chain failed because of exception {}.", e.toString()); | 140 | log.error("Update port chain failed because of exception {}.", e.toString()); |
| ... | @@ -149,7 +153,8 @@ public class PortChainWebResource extends AbstractWebResource { | ... | @@ -149,7 +153,8 @@ public class PortChainWebResource extends AbstractWebResource { |
| 149 | log.debug("Deletes port chain by identifier {}.", id); | 153 | log.debug("Deletes port chain by identifier {}.", id); |
| 150 | PortChainId portChainId = PortChainId.of(id); | 154 | PortChainId portChainId = PortChainId.of(id); |
| 151 | 155 | ||
| 152 | - Boolean issuccess = nullIsNotFound(service.removePortChain(portChainId), PORT_CHAIN_NOT_FOUND); | 156 | + Boolean issuccess = nullIsNotFound(get(PortChainService.class).removePortChain(portChainId), |
| 157 | + PORT_CHAIN_NOT_FOUND); | ||
| 153 | if (!issuccess) { | 158 | if (!issuccess) { |
| 154 | log.debug("Port Chain identifier {} does not exist", id); | 159 | log.debug("Port Chain identifier {} does not exist", id); |
| 155 | } | 160 | } | ... | ... |
apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortChainResourceTest.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2014-2015 Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.vtnweb.resources; | ||
| 17 | + | ||
| 18 | +import static org.easymock.EasyMock.anyObject; | ||
| 19 | +import static org.easymock.EasyMock.createMock; | ||
| 20 | +import static org.easymock.EasyMock.expect; | ||
| 21 | +import static org.easymock.EasyMock.replay; | ||
| 22 | +import static org.hamcrest.Matchers.containsString; | ||
| 23 | +import static org.hamcrest.Matchers.is; | ||
| 24 | +import static org.hamcrest.Matchers.notNullValue; | ||
| 25 | +import static org.junit.Assert.assertThat; | ||
| 26 | +import static org.junit.Assert.fail; | ||
| 27 | + | ||
| 28 | +import java.io.InputStream; | ||
| 29 | +import java.net.HttpURLConnection; | ||
| 30 | +import java.util.HashSet; | ||
| 31 | +import java.util.List; | ||
| 32 | +import java.util.Objects; | ||
| 33 | +import java.util.Set; | ||
| 34 | + | ||
| 35 | +import javax.ws.rs.core.MediaType; | ||
| 36 | + | ||
| 37 | +import org.junit.After; | ||
| 38 | +import org.junit.Before; | ||
| 39 | +import org.junit.Test; | ||
| 40 | +import org.onlab.osgi.ServiceDirectory; | ||
| 41 | +import org.onlab.osgi.TestServiceDirectory; | ||
| 42 | +import org.onlab.rest.BaseResource; | ||
| 43 | +import org.onosproject.codec.CodecService; | ||
| 44 | +import org.onosproject.vtnrsc.FlowClassifierId; | ||
| 45 | +import org.onosproject.vtnrsc.PortChain; | ||
| 46 | +import org.onosproject.vtnrsc.PortChainId; | ||
| 47 | +import org.onosproject.vtnrsc.PortPairGroupId; | ||
| 48 | +import org.onosproject.vtnrsc.TenantId; | ||
| 49 | +import org.onosproject.vtnrsc.portchain.PortChainService; | ||
| 50 | +import org.onosproject.vtnweb.web.SfcCodecContext; | ||
| 51 | + | ||
| 52 | +import com.eclipsesource.json.JsonObject; | ||
| 53 | +import com.google.common.collect.ImmutableList; | ||
| 54 | +import com.google.common.collect.Lists; | ||
| 55 | +import com.sun.jersey.api.client.ClientResponse; | ||
| 56 | +import com.sun.jersey.api.client.UniformInterfaceException; | ||
| 57 | +import com.sun.jersey.api.client.WebResource; | ||
| 58 | + | ||
| 59 | +/** | ||
| 60 | + * Unit tests for port chain REST APIs. | ||
| 61 | + */ | ||
| 62 | +public class PortChainResourceTest extends VtnResourceTest { | ||
| 63 | + | ||
| 64 | + final PortChainService portChainService = createMock(PortChainService.class); | ||
| 65 | + | ||
| 66 | + PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); | ||
| 67 | + TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); | ||
| 68 | + private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList(); | ||
| 69 | + private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList(); | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1", | ||
| 73 | + "Mock port chain", portPairGroupList1, | ||
| 74 | + flowClassifierList1); | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * Mock class for a port chain. | ||
| 78 | + */ | ||
| 79 | + private static class MockPortChain implements PortChain { | ||
| 80 | + | ||
| 81 | + private final PortChainId portChainId; | ||
| 82 | + private final TenantId tenantId; | ||
| 83 | + private final String name; | ||
| 84 | + private final String description; | ||
| 85 | + private final List<PortPairGroupId> portPairGroupList; | ||
| 86 | + private final List<FlowClassifierId> flowClassifierList; | ||
| 87 | + | ||
| 88 | + public MockPortChain(PortChainId portChainId, TenantId tenantId, | ||
| 89 | + String name, String description, | ||
| 90 | + List<PortPairGroupId> portPairGroupList, | ||
| 91 | + List<FlowClassifierId> flowClassifierList) { | ||
| 92 | + | ||
| 93 | + this.portChainId = portChainId; | ||
| 94 | + this.tenantId = tenantId; | ||
| 95 | + this.name = name; | ||
| 96 | + this.description = description; | ||
| 97 | + this.portPairGroupList = portPairGroupList; | ||
| 98 | + this.flowClassifierList = flowClassifierList; | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + @Override | ||
| 102 | + public PortChainId portChainId() { | ||
| 103 | + return portChainId; | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + @Override | ||
| 107 | + public TenantId tenantId() { | ||
| 108 | + return tenantId; | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + @Override | ||
| 112 | + public String name() { | ||
| 113 | + return name; | ||
| 114 | + } | ||
| 115 | + | ||
| 116 | + @Override | ||
| 117 | + public String description() { | ||
| 118 | + return description; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + @Override | ||
| 122 | + public List<PortPairGroupId> portPairGroups() { | ||
| 123 | + return ImmutableList.copyOf(portPairGroupList); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + @Override | ||
| 127 | + public List<FlowClassifierId> flowClassifiers() { | ||
| 128 | + return ImmutableList.copyOf(flowClassifierList); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + @Override | ||
| 132 | + public boolean exactMatch(PortChain portChain) { | ||
| 133 | + return this.equals(portChain) && | ||
| 134 | + Objects.equals(this.portChainId, portChain.portChainId()) && | ||
| 135 | + Objects.equals(this.tenantId, portChain.tenantId()); | ||
| 136 | + } | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * Sets up the global values for all the tests. | ||
| 141 | + */ | ||
| 142 | + @Before | ||
| 143 | + public void setUpTest() { | ||
| 144 | + SfcCodecContext context = new SfcCodecContext(); | ||
| 145 | + ServiceDirectory testDirectory = new TestServiceDirectory() | ||
| 146 | + .add(PortChainService.class, portChainService) | ||
| 147 | + .add(CodecService.class, context.codecManager()); | ||
| 148 | + BaseResource.setServiceDirectory(testDirectory); | ||
| 149 | + | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + /** | ||
| 153 | + * Cleans up. | ||
| 154 | + */ | ||
| 155 | + @After | ||
| 156 | + public void tearDownTest() { | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + /** | ||
| 160 | + * Tests the result of the rest api GET when there are no port chains. | ||
| 161 | + */ | ||
| 162 | + @Test | ||
| 163 | + public void testPortChainsEmpty() { | ||
| 164 | + | ||
| 165 | + expect(portChainService.getPortChains()).andReturn(null).anyTimes(); | ||
| 166 | + replay(portChainService); | ||
| 167 | + final WebResource rs = resource(); | ||
| 168 | + final String response = rs.path("port_chains").get(String.class); | ||
| 169 | + assertThat(response, is("{\"port_chains\":[]}")); | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + /** | ||
| 173 | + * Tests the result of a rest api GET for port chain id. | ||
| 174 | + */ | ||
| 175 | + @Test | ||
| 176 | + public void testGetPortChainId() { | ||
| 177 | + | ||
| 178 | + final Set<PortChain> portChains = new HashSet<>(); | ||
| 179 | + portChains.add(portChain1); | ||
| 180 | + | ||
| 181 | + expect(portChainService.exists(anyObject())).andReturn(true).anyTimes(); | ||
| 182 | + expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes(); | ||
| 183 | + replay(portChainService); | ||
| 184 | + | ||
| 185 | + final WebResource rs = resource(); | ||
| 186 | + final String response = rs.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751").get(String.class); | ||
| 187 | + final JsonObject result = JsonObject.readFrom(response); | ||
| 188 | + assertThat(result, notNullValue()); | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + /** | ||
| 192 | + * Tests that a fetch of a non-existent port chain object throws an exception. | ||
| 193 | + */ | ||
| 194 | + @Test | ||
| 195 | + public void testBadGet() { | ||
| 196 | + expect(portChainService.getPortChain(anyObject())) | ||
| 197 | + .andReturn(null).anyTimes(); | ||
| 198 | + replay(portChainService); | ||
| 199 | + WebResource rs = resource(); | ||
| 200 | + try { | ||
| 201 | + rs.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); | ||
| 202 | + fail("Fetch of non-existent port chain did not throw an exception"); | ||
| 203 | + } catch (UniformInterfaceException ex) { | ||
| 204 | + assertThat(ex.getMessage(), | ||
| 205 | + containsString("returned a response status of")); | ||
| 206 | + } | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + /** | ||
| 210 | + * Tests creating a port chain with POST. | ||
| 211 | + */ | ||
| 212 | + @Test | ||
| 213 | + public void testPost() { | ||
| 214 | + | ||
| 215 | + expect(portChainService.createPortChain(anyObject())) | ||
| 216 | + .andReturn(true).anyTimes(); | ||
| 217 | + replay(portChainService); | ||
| 218 | + | ||
| 219 | + WebResource rs = resource(); | ||
| 220 | + InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json"); | ||
| 221 | + | ||
| 222 | + ClientResponse response = rs.path("port_chains") | ||
| 223 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
| 224 | + .post(ClientResponse.class, jsonStream); | ||
| 225 | + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); | ||
| 226 | + } | ||
| 227 | + | ||
| 228 | + /** | ||
| 229 | + * Tests deleting a port chain. | ||
| 230 | + */ | ||
| 231 | + @Test | ||
| 232 | + public void testDelete() { | ||
| 233 | + expect(portChainService.removePortChain(anyObject())) | ||
| 234 | + .andReturn(true).anyTimes(); | ||
| 235 | + replay(portChainService); | ||
| 236 | + | ||
| 237 | + WebResource rs = resource(); | ||
| 238 | + | ||
| 239 | + String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751"; | ||
| 240 | + | ||
| 241 | + ClientResponse deleteResponse = rs.path(location) | ||
| 242 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
| 243 | + .delete(ClientResponse.class); | ||
| 244 | + assertThat(deleteResponse.getStatus(), | ||
| 245 | + is(HttpURLConnection.HTTP_NO_CONTENT)); | ||
| 246 | + } | ||
| 247 | +} |
| 1 | +{"port_pair": { | ||
| 2 | + "id": "1278dcd4-459f-62ed-754b-87fc5e4a6751", | ||
| 3 | + "name": "PC2", | ||
| 4 | + "tenant_id": "d382007aa9904763a801f68ecf065cf5", | ||
| 5 | + "description": "Two flows and two port-pair-groups", | ||
| 6 | + "flow_classifiers": [ | ||
| 7 | + "456a4a34-2e9c-14ae-37fb-765feae2eb05", | ||
| 8 | + "4a334cd4-fe9c-4fae-af4b-321c5e2eb051" | ||
| 9 | + ], | ||
| 10 | + "port_pair_groups": [ | ||
| 11 | + "4512d643-24fc-4fae-af4b-321c5e2eb3d1", | ||
| 12 | + "4a634d49-76dc-4fae-af4b-321c5e23d651" | ||
| 13 | + ] | ||
| 14 | + } | ||
| 15 | +} |
-
Please register or login to post a comment