From d44649e8293e9df9d4dd7bb5f9ebab1ad979719a Mon Sep 17 00:00:00 2001 From: Phaneendra Manda Date: Tue, 1 Dec 2015 21:11:57 +0530 Subject: [PATCH] [ONOS-3162] UT for Port pair group web resource Change-Id: I47cbadbd2586a52bbc1efebc043cf21feb2ea0b3 --- .../resources/PortPairGroupWebResource.java | 51 ++-- .../resources/PortPairGroupResourceTest.java | 234 ++++++++++++++++++ .../vtnweb/resources/post-PortPairGroup.json | 11 + 3 files changed, 273 insertions(+), 23 deletions(-) create mode 100644 apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java create mode 100644 apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPairGroup.json diff --git a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairGroupWebResource.java b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairGroupWebResource.java index 69daad379d..dc5328a2ed 100644 --- a/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairGroupWebResource.java +++ b/apps/vtn/vtnweb/src/main/java/org/onosproject/vtnweb/resources/PortPairGroupWebResource.java @@ -16,7 +16,6 @@ package org.onosproject.vtnweb.resources; -import static javax.ws.rs.core.Response.Status.NOT_FOUND; import static javax.ws.rs.core.Response.Status.OK; import static org.onlab.util.Tools.nullIsNotFound; @@ -38,11 +37,12 @@ import org.onosproject.rest.AbstractWebResource; import org.onosproject.vtnrsc.PortPairGroup; import org.onosproject.vtnrsc.PortPairGroupId; import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; -import org.onosproject.vtnweb.web.PortPairGroupCodec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; /** @@ -53,7 +53,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; public class PortPairGroupWebResource extends AbstractWebResource { private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class); - private final PortPairGroupService service = get(PortPairGroupService.class); public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found"; public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists"; public static final String PORT_PAIR_GROUP_ID_NOT_EXIST = "Port pair group does not exist with identifier"; @@ -66,10 +65,15 @@ public class PortPairGroupWebResource extends AbstractWebResource { @GET @Produces(MediaType.APPLICATION_JSON) public Response getPortPairGroups() { - Iterable portPairGroups = service.getPortPairGroups(); - ObjectNode result = new ObjectMapper().createObjectNode(); - result.set("port_pair_groups", new PortPairGroupCodec().encode(portPairGroups, this)); - return ok(result).build(); + Iterable portPairGroups = get(PortPairGroupService.class).getPortPairGroups(); + ObjectNode result = mapper().createObjectNode(); + ArrayNode portPairGroupEntry = result.putArray("port_pair_groups"); + if (portPairGroups != null) { + for (final PortPairGroup portPairGroup : portPairGroups) { + portPairGroupEntry.add(codec(PortPairGroup.class).encode(portPairGroup, this)); + } + } + return ok(result.toString()).build(); } /** @@ -82,17 +86,13 @@ public class PortPairGroupWebResource extends AbstractWebResource { @Path("{group_id}") @Produces(MediaType.APPLICATION_JSON) public Response getPortPairGroup(@PathParam("group_id") String id) { - - if (!service.exists(PortPairGroupId.of(id))) { - return Response.status(NOT_FOUND) - .entity(PORT_PAIR_GROUP_NOT_FOUND).build(); - } - PortPairGroup portPairGroup = nullIsNotFound(service.getPortPairGroup(PortPairGroupId.of(id)), + PortPairGroup portPairGroup = nullIsNotFound(get(PortPairGroupService.class) + .getPortPairGroup(PortPairGroupId.of(id)), PORT_PAIR_GROUP_NOT_FOUND); - ObjectNode result = new ObjectMapper().createObjectNode(); - result.set("port_pair_group", new PortPairGroupCodec().encode(portPairGroup, this)); - return ok(result).build(); + ObjectNode result = mapper().createObjectNode(); + result.set("port_pair_group", codec(PortPairGroup.class).encode(portPairGroup, this)); + return ok(result.toString()).build(); } /** @@ -108,10 +108,12 @@ public class PortPairGroupWebResource extends AbstractWebResource { public Response createPortPairGroup(InputStream stream) { try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); + JsonNode port = jsonTree.get("port_pair_group"); - PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this); - Boolean issuccess = nullIsNotFound(service.createPortPairGroup(portPairGroup), + PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this); + Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).createPortPairGroup(portPairGroup), PORT_PAIR_GROUP_NOT_FOUND); return Response.status(OK).entity(issuccess.toString()).build(); } catch (IOException e) { @@ -134,9 +136,12 @@ public class PortPairGroupWebResource extends AbstractWebResource { public Response updatePortPairGroup(@PathParam("group_id") String id, final InputStream stream) { try { - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); - PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this); - Boolean isSuccess = nullIsNotFound(service.updatePortPairGroup(portPairGroup), PORT_PAIR_GROUP_NOT_FOUND); + ObjectMapper mapper = new ObjectMapper(); + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); + JsonNode port = jsonTree.get("port_pair_group"); + PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this); + Boolean isSuccess = nullIsNotFound(get(PortPairGroupService.class).updatePortPairGroup(portPairGroup), + PORT_PAIR_GROUP_NOT_FOUND); return Response.status(OK).entity(isSuccess.toString()).build(); } catch (IOException e) { log.error("Update port pair group failed because of exception {}.", e.toString()); @@ -154,7 +159,7 @@ public class PortPairGroupWebResource extends AbstractWebResource { public void deletePortPairGroup(@PathParam("group_id") String id) { log.debug("Deletes port pair group by identifier {}.", id); PortPairGroupId portPairGroupId = PortPairGroupId.of(id); - Boolean issuccess = nullIsNotFound(service.removePortPairGroup(portPairGroupId), + Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).removePortPairGroup(portPairGroupId), PORT_PAIR_GROUP_NOT_FOUND); if (!issuccess) { log.debug("Port pair group identifier {} does not exist", id); diff --git a/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java new file mode 100644 index 0000000000..c13f214158 --- /dev/null +++ b/apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.java @@ -0,0 +1,234 @@ +/* + * Copyright 2014-2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.vtnweb.resources; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +import javax.ws.rs.core.MediaType; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onlab.osgi.ServiceDirectory; +import org.onlab.osgi.TestServiceDirectory; +import org.onlab.rest.BaseResource; +import org.onosproject.codec.CodecService; +import org.onosproject.vtnrsc.PortPairGroup; +import org.onosproject.vtnrsc.PortPairGroupId; +import org.onosproject.vtnrsc.PortPairId; +import org.onosproject.vtnrsc.TenantId; +import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; +import org.onosproject.vtnweb.web.SfcCodecContext; + +import com.eclipsesource.json.JsonObject; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.UniformInterfaceException; +import com.sun.jersey.api.client.WebResource; +/** + * Unit tests for port pair group REST APIs. + */ +public class PortPairGroupResourceTest extends VtnResourceTest { + + final PortPairGroupService portPairGroupService = createMock(PortPairGroupService.class); + + PortPairGroupId portPairGroupId1 = PortPairGroupId.of("4512d643-24fc-4fae-af4b-321c5e2eb3d1"); + TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); + private final List portPairList1 = Lists.newArrayList(); + + final MockPortPairGroup portPairGroup1 = new MockPortPairGroup(portPairGroupId1, tenantId1, "portPairGroup1", + "Mock port pair group", portPairList1); + + /** + * Mock class for a port pair group. + */ + private static class MockPortPairGroup implements PortPairGroup { + + private final PortPairGroupId portPairGroupId; + private final TenantId tenantId; + private final String name; + private final String description; + private final List portPairList; + + public MockPortPairGroup(PortPairGroupId portPairGroupId, TenantId tenantId, + String name, String description, + List portPairList) { + + this.portPairGroupId = portPairGroupId; + this.tenantId = tenantId; + this.name = name; + this.description = description; + this.portPairList = portPairList; + } + + @Override + public PortPairGroupId portPairGroupId() { + return portPairGroupId; + } + + @Override + public TenantId tenantId() { + return tenantId; + } + + @Override + public String name() { + return name; + } + + @Override + public String description() { + return description; + } + + @Override + public List portPairs() { + return ImmutableList.copyOf(portPairList); + } + + @Override + public boolean exactMatch(PortPairGroup portPairGroup) { + return this.equals(portPairGroup) && + Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) && + Objects.equals(this.tenantId, portPairGroup.tenantId()); + } + } + + /** + * Sets up the global values for all the tests. + */ + @Before + public void setUpTest() { + SfcCodecContext context = new SfcCodecContext(); + ServiceDirectory testDirectory = new TestServiceDirectory() + .add(PortPairGroupService.class, portPairGroupService) + .add(CodecService.class, context.codecManager()); + BaseResource.setServiceDirectory(testDirectory); + + } + + /** + * Cleans up. + */ + @After + public void tearDownTest() { + } + + /** + * Tests the result of the rest api GET when there are no port pair groups. + */ + @Test + public void testPortPairGroupsEmpty() { + + expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes(); + replay(portPairGroupService); + final WebResource rs = resource(); + final String response = rs.path("port_pair_groups").get(String.class); + assertThat(response, is("{\"port_pair_groups\":[]}")); + } + + /** + * Tests the result of a rest api GET for port pair group id. + */ + @Test + public void testGetPortPairGroupId() { + + final Set portPairGroups = new HashSet<>(); + portPairGroups.add(portPairGroup1); + + expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes(); + expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes(); + replay(portPairGroupService); + + final WebResource rs = resource(); + final String response = rs.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1").get(String.class); + final JsonObject result = JsonObject.readFrom(response); + assertThat(result, notNullValue()); + } + + /** + * Tests that a fetch of a non-existent port pair group object throws an exception. + */ + @Test + public void testBadGet() { + expect(portPairGroupService.getPortPairGroup(anyObject())) + .andReturn(null).anyTimes(); + replay(portPairGroupService); + WebResource rs = resource(); + try { + rs.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); + fail("Fetch of non-existent port pair group did not throw an exception"); + } catch (UniformInterfaceException ex) { + assertThat(ex.getMessage(), + containsString("returned a response status of")); + } + } + + /** + * Tests creating a port pair group with POST. + */ + @Test + public void testPost() { + + expect(portPairGroupService.createPortPairGroup(anyObject())) + .andReturn(true).anyTimes(); + replay(portPairGroupService); + + WebResource rs = resource(); + InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json"); + + ClientResponse response = rs.path("port_pair_groups") + .type(MediaType.APPLICATION_JSON_TYPE) + .post(ClientResponse.class, jsonStream); + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); + } + + /** + * Tests deleting a port pair group. + */ + @Test + public void testDelete() { + expect(portPairGroupService.removePortPairGroup(anyObject())) + .andReturn(true).anyTimes(); + replay(portPairGroupService); + + WebResource rs = resource(); + + String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1"; + + ClientResponse deleteResponse = rs.path(location) + .type(MediaType.APPLICATION_JSON_TYPE) + .delete(ClientResponse.class); + assertThat(deleteResponse.getStatus(), + is(HttpURLConnection.HTTP_NO_CONTENT)); + } +} diff --git a/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPairGroup.json b/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPairGroup.json new file mode 100644 index 0000000000..f6a888d98d --- /dev/null +++ b/apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPairGroup.json @@ -0,0 +1,11 @@ +{"port_pair_group": { + "id": "4512d643-24fc-4fae-af4b-321c5e2eb3d1", + "name": "portPairGroup1", + "tenant_id": "d382007aa9904763a801f68ecf065cf5", + "description": "Mock port pair group", + "port_pairs": [ + "875dfeda-43ed-23fe-454b-764feab2c342", + "78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae" + ] +} +}