Phaneendra Manda
Committed by Ray Milkey

[ONOS-3161]UT for port pair web resource

Change-Id: I81a98b9407305f572eec999f9555dd4a6bc4e808
...@@ -42,7 +42,9 @@ import org.onosproject.vtnweb.web.PortPairCodec; ...@@ -42,7 +42,9 @@ import org.onosproject.vtnweb.web.PortPairCodec;
42 import org.slf4j.Logger; 42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory; 43 import org.slf4j.LoggerFactory;
44 44
45 +import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper; 46 import com.fasterxml.jackson.databind.ObjectMapper;
47 +import com.fasterxml.jackson.databind.node.ArrayNode;
46 import com.fasterxml.jackson.databind.node.ObjectNode; 48 import com.fasterxml.jackson.databind.node.ObjectNode;
47 49
48 /** 50 /**
...@@ -67,8 +69,13 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -67,8 +69,13 @@ public class PortPairWebResource extends AbstractWebResource {
67 public Response getPortPairs() { 69 public Response getPortPairs() {
68 Iterable<PortPair> portPairs = service.getPortPairs(); 70 Iterable<PortPair> portPairs = service.getPortPairs();
69 ObjectNode result = new ObjectMapper().createObjectNode(); 71 ObjectNode result = new ObjectMapper().createObjectNode();
70 - result.set("port_pairs", new PortPairCodec().encode(portPairs, this)); 72 + ArrayNode portPairEntry = result.putArray("port_pairs");
71 - return ok(result).build(); 73 + if (portPairs != null) {
74 + for (final PortPair portPair : portPairs) {
75 + portPairEntry.add(new PortPairCodec().encode(portPair, this));
76 + }
77 + }
78 + return ok(result.toString()).build();
72 } 79 }
73 80
74 /** 81 /**
...@@ -80,18 +87,15 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -80,18 +87,15 @@ public class PortPairWebResource extends AbstractWebResource {
80 @GET 87 @GET
81 @Path("{pair_id}") 88 @Path("{pair_id}")
82 @Produces(MediaType.APPLICATION_JSON) 89 @Produces(MediaType.APPLICATION_JSON)
83 - public Response getPortPair(@PathParam("portPairId") String id) { 90 + public Response getPortPair(@PathParam("pair_id") String id) {
84 91
85 if (!service.exists(PortPairId.of(id))) { 92 if (!service.exists(PortPairId.of(id))) {
86 - return Response.status(NOT_FOUND) 93 + return Response.status(NOT_FOUND).entity(PORT_PAIR_NOT_FOUND).build();
87 - .entity(PORT_PAIR_NOT_FOUND).build();
88 } 94 }
89 - PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), 95 + PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), PORT_PAIR_NOT_FOUND);
90 - PORT_PAIR_NOT_FOUND);
91 -
92 ObjectNode result = new ObjectMapper().createObjectNode(); 96 ObjectNode result = new ObjectMapper().createObjectNode();
93 result.set("port_pair", new PortPairCodec().encode(portPair, this)); 97 result.set("port_pair", new PortPairCodec().encode(portPair, this));
94 - return ok(result).build(); 98 + return ok(result.toString()).build();
95 } 99 }
96 100
97 /** 101 /**
...@@ -106,11 +110,11 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -106,11 +110,11 @@ public class PortPairWebResource extends AbstractWebResource {
106 @Produces(MediaType.APPLICATION_JSON) 110 @Produces(MediaType.APPLICATION_JSON)
107 public Response createPortPair(InputStream stream) { 111 public Response createPortPair(InputStream stream) {
108 try { 112 try {
109 - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); 113 + ObjectMapper mapper = new ObjectMapper();
110 - 114 + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
111 - PortPair portPair = codec(PortPair.class).decode(jsonTree, this); 115 + JsonNode port = jsonTree.get("port_pair");
112 - Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), 116 + PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this);
113 - PORT_PAIR_NOT_FOUND); 117 + Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), PORT_PAIR_NOT_FOUND);
114 return Response.status(OK).entity(isSuccess.toString()).build(); 118 return Response.status(OK).entity(isSuccess.toString()).build();
115 } catch (IOException e) { 119 } catch (IOException e) {
116 log.error("Exception while creating port pair {}.", e.toString()); 120 log.error("Exception while creating port pair {}.", e.toString());
...@@ -132,8 +136,10 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -132,8 +136,10 @@ public class PortPairWebResource extends AbstractWebResource {
132 public Response updatePortPair(@PathParam("pair_id") String id, 136 public Response updatePortPair(@PathParam("pair_id") String id,
133 final InputStream stream) { 137 final InputStream stream) {
134 try { 138 try {
135 - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); 139 + ObjectMapper mapper = new ObjectMapper();
136 - PortPair portPair = codec(PortPair.class).decode(jsonTree, this); 140 + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
141 + JsonNode port = jsonTree.get("port_pair");
142 + PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this);
137 Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND); 143 Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND);
138 return Response.status(OK).entity(isSuccess.toString()).build(); 144 return Response.status(OK).entity(isSuccess.toString()).build();
139 } catch (IOException e) { 145 } catch (IOException e) {
...@@ -152,8 +158,7 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -152,8 +158,7 @@ public class PortPairWebResource extends AbstractWebResource {
152 public void deletePortPair(@PathParam("pair_id") String id) { 158 public void deletePortPair(@PathParam("pair_id") String id) {
153 159
154 PortPairId portPairId = PortPairId.of(id); 160 PortPairId portPairId = PortPairId.of(id);
155 - Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), 161 + Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
156 - PORT_PAIR_NOT_FOUND);
157 if (!isSuccess) { 162 if (!isSuccess) {
158 log.debug("Port pair identifier {} does not exist", id); 163 log.debug("Port pair identifier {} does not exist", id);
159 } 164 }
......
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.Objects;
32 +import java.util.Set;
33 +
34 +import javax.ws.rs.core.MediaType;
35 +
36 +import org.junit.After;
37 +import org.junit.Before;
38 +import org.junit.Test;
39 +import org.onlab.osgi.ServiceDirectory;
40 +import org.onlab.osgi.TestServiceDirectory;
41 +import org.onlab.rest.BaseResource;
42 +import org.onosproject.vtnrsc.PortPair;
43 +import org.onosproject.vtnrsc.PortPairId;
44 +import org.onosproject.vtnrsc.TenantId;
45 +import org.onosproject.vtnrsc.portpair.PortPairService;
46 +
47 +import com.eclipsesource.json.JsonObject;
48 +import com.sun.jersey.api.client.ClientResponse;
49 +import com.sun.jersey.api.client.UniformInterfaceException;
50 +import com.sun.jersey.api.client.WebResource;
51 +/**
52 + * Unit tests for port pair REST APIs.
53 + */
54 +public class PortPairResourceTest extends VtnResourceTest {
55 +
56 + final PortPairService portPairService = createMock(PortPairService.class);
57 +
58 + PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
59 + TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
60 +
61 + final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1",
62 + "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
63 + "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
64 +
65 + /**
66 + * Mock class for a port pair.
67 + */
68 + private static class MockPortPair implements PortPair {
69 +
70 + private final PortPairId portPairId;
71 + private final TenantId tenantId;
72 + private final String name;
73 + private final String description;
74 + private final String ingress;
75 + private final String egress;
76 +
77 + public MockPortPair(PortPairId portPairId, TenantId tenantId,
78 + String name, String description,
79 + String ingress, String egress) {
80 +
81 + this.portPairId = portPairId;
82 + this.tenantId = tenantId;
83 + this.name = name;
84 + this.description = description;
85 + this.ingress = ingress;
86 + this.egress = egress;
87 + }
88 +
89 + @Override
90 + public PortPairId portPairId() {
91 + return portPairId;
92 + }
93 +
94 + @Override
95 + public TenantId tenantId() {
96 + return tenantId;
97 + }
98 +
99 + @Override
100 + public String name() {
101 + return name;
102 + }
103 +
104 + @Override
105 + public String description() {
106 + return description;
107 + }
108 +
109 + @Override
110 + public String ingress() {
111 + return ingress;
112 + }
113 +
114 + @Override
115 + public String egress() {
116 + return egress;
117 + }
118 +
119 + @Override
120 + public boolean exactMatch(PortPair portPair) {
121 + return this.equals(portPair) &&
122 + Objects.equals(this.portPairId, portPair.portPairId()) &&
123 + Objects.equals(this.tenantId, portPair.tenantId());
124 + }
125 + }
126 +
127 + /**
128 + * Sets up the global values for all the tests.
129 + */
130 + @Before
131 + public void setUpTest() {
132 + ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService);
133 + BaseResource.setServiceDirectory(testDirectory);
134 +
135 + }
136 +
137 + /**
138 + * Cleans up.
139 + */
140 + @After
141 + public void tearDownTest() {
142 + }
143 +
144 + /**
145 + * Tests the result of the rest api GET when there are no port pairs.
146 + */
147 + @Test
148 + public void testPortPairsEmpty() {
149 +
150 + expect(portPairService.getPortPairs()).andReturn(null).anyTimes();
151 + replay(portPairService);
152 + final WebResource rs = resource();
153 + final String response = rs.path("port_pairs").get(String.class);
154 + assertThat(response, is("{\"port_pairs\":[]}"));
155 + }
156 +
157 + /**
158 + * Tests the result of a rest api GET for port pair id.
159 + */
160 + @Test
161 + public void testGetPortPairId() {
162 +
163 + final Set<PortPair> portPairs = new HashSet<>();
164 + portPairs.add(portPair1);
165 +
166 + expect(portPairService.exists(anyObject())).andReturn(true).anyTimes();
167 + expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes();
168 + replay(portPairService);
169 +
170 + final WebResource rs = resource();
171 + final String response = rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae").get(String.class);
172 + final JsonObject result = JsonObject.readFrom(response);
173 + assertThat(result, notNullValue());
174 + }
175 +
176 + /**
177 + * Tests that a fetch of a non-existent port pair object throws an exception.
178 + */
179 + @Test
180 + public void testBadGet() {
181 + expect(portPairService.getPortPair(anyObject()))
182 + .andReturn(null).anyTimes();
183 + replay(portPairService);
184 + WebResource rs = resource();
185 + try {
186 + rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
187 + fail("Fetch of non-existent port pair did not throw an exception");
188 + } catch (UniformInterfaceException ex) {
189 + assertThat(ex.getMessage(),
190 + containsString("returned a response status of"));
191 + }
192 + }
193 +
194 + /**
195 + * Tests creating a port pair with POST.
196 + */
197 + @Test
198 + public void testPost() {
199 +
200 + expect(portPairService.createPortPair(anyObject()))
201 + .andReturn(true).anyTimes();
202 + replay(portPairService);
203 +
204 + WebResource rs = resource();
205 + InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json");
206 +
207 + ClientResponse response = rs.path("port_pairs")
208 + .type(MediaType.APPLICATION_JSON_TYPE)
209 + .post(ClientResponse.class, jsonStream);
210 + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
211 + }
212 +
213 + /**
214 + * Tests deleting a port pair.
215 + */
216 + @Test
217 + public void testDelete() {
218 + expect(portPairService.removePortPair(anyObject()))
219 + .andReturn(true).anyTimes();
220 + replay(portPairService);
221 +
222 + WebResource rs = resource();
223 +
224 + String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae";
225 +
226 + ClientResponse deleteResponse = rs.path(location)
227 + .type(MediaType.APPLICATION_JSON_TYPE)
228 + .delete(ClientResponse.class);
229 + assertThat(deleteResponse.getStatus(),
230 + is(HttpURLConnection.HTTP_NO_CONTENT));
231 + }
232 +}
1 +{"port_pair": {
2 + "id": "78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae",
3 + "name": "PP1",
4 + "tenant_id": "d382007aa9904763a801f68ecf065cf5",
5 + "description": "SF-A",
6 + "ingress": "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
7 + "egress": "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"
8 + }
9 +}