Committed by
Thomas Vachuska
[ONOS-3162] UT for Port pair group web resource
Change-Id: I47cbadbd2586a52bbc1efebc043cf21feb2ea0b3
Showing
3 changed files
with
273 additions
and
23 deletions
... | @@ -16,7 +16,6 @@ | ... | @@ -16,7 +16,6 @@ |
16 | 16 | ||
17 | package org.onosproject.vtnweb.resources; | 17 | package org.onosproject.vtnweb.resources; |
18 | 18 | ||
19 | -import static javax.ws.rs.core.Response.Status.NOT_FOUND; | ||
20 | import static javax.ws.rs.core.Response.Status.OK; | 19 | import static javax.ws.rs.core.Response.Status.OK; |
21 | import static org.onlab.util.Tools.nullIsNotFound; | 20 | import static org.onlab.util.Tools.nullIsNotFound; |
22 | 21 | ||
... | @@ -38,11 +37,12 @@ import org.onosproject.rest.AbstractWebResource; | ... | @@ -38,11 +37,12 @@ import org.onosproject.rest.AbstractWebResource; |
38 | import org.onosproject.vtnrsc.PortPairGroup; | 37 | import org.onosproject.vtnrsc.PortPairGroup; |
39 | import org.onosproject.vtnrsc.PortPairGroupId; | 38 | import org.onosproject.vtnrsc.PortPairGroupId; |
40 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; | 39 | import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; |
41 | -import org.onosproject.vtnweb.web.PortPairGroupCodec; | ||
42 | import org.slf4j.Logger; | 40 | import org.slf4j.Logger; |
43 | import org.slf4j.LoggerFactory; | 41 | import org.slf4j.LoggerFactory; |
44 | 42 | ||
43 | +import com.fasterxml.jackson.databind.JsonNode; | ||
45 | import com.fasterxml.jackson.databind.ObjectMapper; | 44 | import com.fasterxml.jackson.databind.ObjectMapper; |
45 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
46 | import com.fasterxml.jackson.databind.node.ObjectNode; | 46 | import com.fasterxml.jackson.databind.node.ObjectNode; |
47 | 47 | ||
48 | /** | 48 | /** |
... | @@ -53,7 +53,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; | ... | @@ -53,7 +53,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
53 | public class PortPairGroupWebResource extends AbstractWebResource { | 53 | public class PortPairGroupWebResource extends AbstractWebResource { |
54 | 54 | ||
55 | private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class); | 55 | private final Logger log = LoggerFactory.getLogger(PortPairGroupWebResource.class); |
56 | - private final PortPairGroupService service = get(PortPairGroupService.class); | ||
57 | public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found"; | 56 | public static final String PORT_PAIR_GROUP_NOT_FOUND = "Port pair group not found"; |
58 | public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists"; | 57 | public static final String PORT_PAIR_GROUP_ID_EXIST = "Port pair group exists"; |
59 | public static final String PORT_PAIR_GROUP_ID_NOT_EXIST = "Port pair group does not exist with identifier"; | 58 | 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 { | ... | @@ -66,10 +65,15 @@ public class PortPairGroupWebResource extends AbstractWebResource { |
66 | @GET | 65 | @GET |
67 | @Produces(MediaType.APPLICATION_JSON) | 66 | @Produces(MediaType.APPLICATION_JSON) |
68 | public Response getPortPairGroups() { | 67 | public Response getPortPairGroups() { |
69 | - Iterable<PortPairGroup> portPairGroups = service.getPortPairGroups(); | 68 | + Iterable<PortPairGroup> portPairGroups = get(PortPairGroupService.class).getPortPairGroups(); |
70 | - ObjectNode result = new ObjectMapper().createObjectNode(); | 69 | + ObjectNode result = mapper().createObjectNode(); |
71 | - result.set("port_pair_groups", new PortPairGroupCodec().encode(portPairGroups, this)); | 70 | + ArrayNode portPairGroupEntry = result.putArray("port_pair_groups"); |
72 | - return ok(result).build(); | 71 | + if (portPairGroups != null) { |
72 | + for (final PortPairGroup portPairGroup : portPairGroups) { | ||
73 | + portPairGroupEntry.add(codec(PortPairGroup.class).encode(portPairGroup, this)); | ||
74 | + } | ||
75 | + } | ||
76 | + return ok(result.toString()).build(); | ||
73 | } | 77 | } |
74 | 78 | ||
75 | /** | 79 | /** |
... | @@ -82,17 +86,13 @@ public class PortPairGroupWebResource extends AbstractWebResource { | ... | @@ -82,17 +86,13 @@ public class PortPairGroupWebResource extends AbstractWebResource { |
82 | @Path("{group_id}") | 86 | @Path("{group_id}") |
83 | @Produces(MediaType.APPLICATION_JSON) | 87 | @Produces(MediaType.APPLICATION_JSON) |
84 | public Response getPortPairGroup(@PathParam("group_id") String id) { | 88 | public Response getPortPairGroup(@PathParam("group_id") String id) { |
85 | - | 89 | + PortPairGroup portPairGroup = nullIsNotFound(get(PortPairGroupService.class) |
86 | - if (!service.exists(PortPairGroupId.of(id))) { | 90 | + .getPortPairGroup(PortPairGroupId.of(id)), |
87 | - return Response.status(NOT_FOUND) | ||
88 | - .entity(PORT_PAIR_GROUP_NOT_FOUND).build(); | ||
89 | - } | ||
90 | - PortPairGroup portPairGroup = nullIsNotFound(service.getPortPairGroup(PortPairGroupId.of(id)), | ||
91 | PORT_PAIR_GROUP_NOT_FOUND); | 91 | PORT_PAIR_GROUP_NOT_FOUND); |
92 | 92 | ||
93 | - ObjectNode result = new ObjectMapper().createObjectNode(); | 93 | + ObjectNode result = mapper().createObjectNode(); |
94 | - result.set("port_pair_group", new PortPairGroupCodec().encode(portPairGroup, this)); | 94 | + result.set("port_pair_group", codec(PortPairGroup.class).encode(portPairGroup, this)); |
95 | - return ok(result).build(); | 95 | + return ok(result.toString()).build(); |
96 | } | 96 | } |
97 | 97 | ||
98 | /** | 98 | /** |
... | @@ -108,10 +108,12 @@ public class PortPairGroupWebResource extends AbstractWebResource { | ... | @@ -108,10 +108,12 @@ public class PortPairGroupWebResource extends AbstractWebResource { |
108 | public Response createPortPairGroup(InputStream stream) { | 108 | public Response createPortPairGroup(InputStream stream) { |
109 | 109 | ||
110 | try { | 110 | try { |
111 | - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | 111 | + ObjectMapper mapper = new ObjectMapper(); |
112 | + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); | ||
113 | + JsonNode port = jsonTree.get("port_pair_group"); | ||
112 | 114 | ||
113 | - PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this); | 115 | + PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this); |
114 | - Boolean issuccess = nullIsNotFound(service.createPortPairGroup(portPairGroup), | 116 | + Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).createPortPairGroup(portPairGroup), |
115 | PORT_PAIR_GROUP_NOT_FOUND); | 117 | PORT_PAIR_GROUP_NOT_FOUND); |
116 | return Response.status(OK).entity(issuccess.toString()).build(); | 118 | return Response.status(OK).entity(issuccess.toString()).build(); |
117 | } catch (IOException e) { | 119 | } catch (IOException e) { |
... | @@ -134,9 +136,12 @@ public class PortPairGroupWebResource extends AbstractWebResource { | ... | @@ -134,9 +136,12 @@ public class PortPairGroupWebResource extends AbstractWebResource { |
134 | public Response updatePortPairGroup(@PathParam("group_id") String id, | 136 | public Response updatePortPairGroup(@PathParam("group_id") String id, |
135 | final InputStream stream) { | 137 | final InputStream stream) { |
136 | try { | 138 | try { |
137 | - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | 139 | + ObjectMapper mapper = new ObjectMapper(); |
138 | - PortPairGroup portPairGroup = codec(PortPairGroup.class).decode(jsonTree, this); | 140 | + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); |
139 | - Boolean isSuccess = nullIsNotFound(service.updatePortPairGroup(portPairGroup), PORT_PAIR_GROUP_NOT_FOUND); | 141 | + JsonNode port = jsonTree.get("port_pair_group"); |
142 | + PortPairGroup portPairGroup = codec(PortPairGroup.class).decode((ObjectNode) port, this); | ||
143 | + Boolean isSuccess = nullIsNotFound(get(PortPairGroupService.class).updatePortPairGroup(portPairGroup), | ||
144 | + PORT_PAIR_GROUP_NOT_FOUND); | ||
140 | return Response.status(OK).entity(isSuccess.toString()).build(); | 145 | return Response.status(OK).entity(isSuccess.toString()).build(); |
141 | } catch (IOException e) { | 146 | } catch (IOException e) { |
142 | log.error("Update port pair group failed because of exception {}.", e.toString()); | 147 | log.error("Update port pair group failed because of exception {}.", e.toString()); |
... | @@ -154,7 +159,7 @@ public class PortPairGroupWebResource extends AbstractWebResource { | ... | @@ -154,7 +159,7 @@ public class PortPairGroupWebResource extends AbstractWebResource { |
154 | public void deletePortPairGroup(@PathParam("group_id") String id) { | 159 | public void deletePortPairGroup(@PathParam("group_id") String id) { |
155 | log.debug("Deletes port pair group by identifier {}.", id); | 160 | log.debug("Deletes port pair group by identifier {}.", id); |
156 | PortPairGroupId portPairGroupId = PortPairGroupId.of(id); | 161 | PortPairGroupId portPairGroupId = PortPairGroupId.of(id); |
157 | - Boolean issuccess = nullIsNotFound(service.removePortPairGroup(portPairGroupId), | 162 | + Boolean issuccess = nullIsNotFound(get(PortPairGroupService.class).removePortPairGroup(portPairGroupId), |
158 | PORT_PAIR_GROUP_NOT_FOUND); | 163 | PORT_PAIR_GROUP_NOT_FOUND); |
159 | if (!issuccess) { | 164 | if (!issuccess) { |
160 | log.debug("Port pair group identifier {} does not exist", id); | 165 | log.debug("Port pair group identifier {} does not exist", id); | ... | ... |
apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/PortPairGroupResourceTest.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.PortPairGroup; | ||
45 | +import org.onosproject.vtnrsc.PortPairGroupId; | ||
46 | +import org.onosproject.vtnrsc.PortPairId; | ||
47 | +import org.onosproject.vtnrsc.TenantId; | ||
48 | +import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService; | ||
49 | +import org.onosproject.vtnweb.web.SfcCodecContext; | ||
50 | + | ||
51 | +import com.eclipsesource.json.JsonObject; | ||
52 | +import com.google.common.collect.ImmutableList; | ||
53 | +import com.google.common.collect.Lists; | ||
54 | +import com.sun.jersey.api.client.ClientResponse; | ||
55 | +import com.sun.jersey.api.client.UniformInterfaceException; | ||
56 | +import com.sun.jersey.api.client.WebResource; | ||
57 | +/** | ||
58 | + * Unit tests for port pair group REST APIs. | ||
59 | + */ | ||
60 | +public class PortPairGroupResourceTest extends VtnResourceTest { | ||
61 | + | ||
62 | + final PortPairGroupService portPairGroupService = createMock(PortPairGroupService.class); | ||
63 | + | ||
64 | + PortPairGroupId portPairGroupId1 = PortPairGroupId.of("4512d643-24fc-4fae-af4b-321c5e2eb3d1"); | ||
65 | + TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); | ||
66 | + private final List<PortPairId> portPairList1 = Lists.newArrayList(); | ||
67 | + | ||
68 | + final MockPortPairGroup portPairGroup1 = new MockPortPairGroup(portPairGroupId1, tenantId1, "portPairGroup1", | ||
69 | + "Mock port pair group", portPairList1); | ||
70 | + | ||
71 | + /** | ||
72 | + * Mock class for a port pair group. | ||
73 | + */ | ||
74 | + private static class MockPortPairGroup implements PortPairGroup { | ||
75 | + | ||
76 | + private final PortPairGroupId portPairGroupId; | ||
77 | + private final TenantId tenantId; | ||
78 | + private final String name; | ||
79 | + private final String description; | ||
80 | + private final List<PortPairId> portPairList; | ||
81 | + | ||
82 | + public MockPortPairGroup(PortPairGroupId portPairGroupId, TenantId tenantId, | ||
83 | + String name, String description, | ||
84 | + List<PortPairId> portPairList) { | ||
85 | + | ||
86 | + this.portPairGroupId = portPairGroupId; | ||
87 | + this.tenantId = tenantId; | ||
88 | + this.name = name; | ||
89 | + this.description = description; | ||
90 | + this.portPairList = portPairList; | ||
91 | + } | ||
92 | + | ||
93 | + @Override | ||
94 | + public PortPairGroupId portPairGroupId() { | ||
95 | + return portPairGroupId; | ||
96 | + } | ||
97 | + | ||
98 | + @Override | ||
99 | + public TenantId tenantId() { | ||
100 | + return tenantId; | ||
101 | + } | ||
102 | + | ||
103 | + @Override | ||
104 | + public String name() { | ||
105 | + return name; | ||
106 | + } | ||
107 | + | ||
108 | + @Override | ||
109 | + public String description() { | ||
110 | + return description; | ||
111 | + } | ||
112 | + | ||
113 | + @Override | ||
114 | + public List<PortPairId> portPairs() { | ||
115 | + return ImmutableList.copyOf(portPairList); | ||
116 | + } | ||
117 | + | ||
118 | + @Override | ||
119 | + public boolean exactMatch(PortPairGroup portPairGroup) { | ||
120 | + return this.equals(portPairGroup) && | ||
121 | + Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) && | ||
122 | + Objects.equals(this.tenantId, portPairGroup.tenantId()); | ||
123 | + } | ||
124 | + } | ||
125 | + | ||
126 | + /** | ||
127 | + * Sets up the global values for all the tests. | ||
128 | + */ | ||
129 | + @Before | ||
130 | + public void setUpTest() { | ||
131 | + SfcCodecContext context = new SfcCodecContext(); | ||
132 | + ServiceDirectory testDirectory = new TestServiceDirectory() | ||
133 | + .add(PortPairGroupService.class, portPairGroupService) | ||
134 | + .add(CodecService.class, context.codecManager()); | ||
135 | + BaseResource.setServiceDirectory(testDirectory); | ||
136 | + | ||
137 | + } | ||
138 | + | ||
139 | + /** | ||
140 | + * Cleans up. | ||
141 | + */ | ||
142 | + @After | ||
143 | + public void tearDownTest() { | ||
144 | + } | ||
145 | + | ||
146 | + /** | ||
147 | + * Tests the result of the rest api GET when there are no port pair groups. | ||
148 | + */ | ||
149 | + @Test | ||
150 | + public void testPortPairGroupsEmpty() { | ||
151 | + | ||
152 | + expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes(); | ||
153 | + replay(portPairGroupService); | ||
154 | + final WebResource rs = resource(); | ||
155 | + final String response = rs.path("port_pair_groups").get(String.class); | ||
156 | + assertThat(response, is("{\"port_pair_groups\":[]}")); | ||
157 | + } | ||
158 | + | ||
159 | + /** | ||
160 | + * Tests the result of a rest api GET for port pair group id. | ||
161 | + */ | ||
162 | + @Test | ||
163 | + public void testGetPortPairGroupId() { | ||
164 | + | ||
165 | + final Set<PortPairGroup> portPairGroups = new HashSet<>(); | ||
166 | + portPairGroups.add(portPairGroup1); | ||
167 | + | ||
168 | + expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes(); | ||
169 | + expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes(); | ||
170 | + replay(portPairGroupService); | ||
171 | + | ||
172 | + final WebResource rs = resource(); | ||
173 | + final String response = rs.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1").get(String.class); | ||
174 | + final JsonObject result = JsonObject.readFrom(response); | ||
175 | + assertThat(result, notNullValue()); | ||
176 | + } | ||
177 | + | ||
178 | + /** | ||
179 | + * Tests that a fetch of a non-existent port pair group object throws an exception. | ||
180 | + */ | ||
181 | + @Test | ||
182 | + public void testBadGet() { | ||
183 | + expect(portPairGroupService.getPortPairGroup(anyObject())) | ||
184 | + .andReturn(null).anyTimes(); | ||
185 | + replay(portPairGroupService); | ||
186 | + WebResource rs = resource(); | ||
187 | + try { | ||
188 | + rs.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); | ||
189 | + fail("Fetch of non-existent port pair group did not throw an exception"); | ||
190 | + } catch (UniformInterfaceException ex) { | ||
191 | + assertThat(ex.getMessage(), | ||
192 | + containsString("returned a response status of")); | ||
193 | + } | ||
194 | + } | ||
195 | + | ||
196 | + /** | ||
197 | + * Tests creating a port pair group with POST. | ||
198 | + */ | ||
199 | + @Test | ||
200 | + public void testPost() { | ||
201 | + | ||
202 | + expect(portPairGroupService.createPortPairGroup(anyObject())) | ||
203 | + .andReturn(true).anyTimes(); | ||
204 | + replay(portPairGroupService); | ||
205 | + | ||
206 | + WebResource rs = resource(); | ||
207 | + InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json"); | ||
208 | + | ||
209 | + ClientResponse response = rs.path("port_pair_groups") | ||
210 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
211 | + .post(ClientResponse.class, jsonStream); | ||
212 | + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); | ||
213 | + } | ||
214 | + | ||
215 | + /** | ||
216 | + * Tests deleting a port pair group. | ||
217 | + */ | ||
218 | + @Test | ||
219 | + public void testDelete() { | ||
220 | + expect(portPairGroupService.removePortPairGroup(anyObject())) | ||
221 | + .andReturn(true).anyTimes(); | ||
222 | + replay(portPairGroupService); | ||
223 | + | ||
224 | + WebResource rs = resource(); | ||
225 | + | ||
226 | + String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1"; | ||
227 | + | ||
228 | + ClientResponse deleteResponse = rs.path(location) | ||
229 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
230 | + .delete(ClientResponse.class); | ||
231 | + assertThat(deleteResponse.getStatus(), | ||
232 | + is(HttpURLConnection.HTTP_NO_CONTENT)); | ||
233 | + } | ||
234 | +} |
apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-PortPairGroup.json
0 → 100644
1 | +{"port_pair_group": { | ||
2 | + "id": "4512d643-24fc-4fae-af4b-321c5e2eb3d1", | ||
3 | + "name": "portPairGroup1", | ||
4 | + "tenant_id": "d382007aa9904763a801f68ecf065cf5", | ||
5 | + "description": "Mock port pair group", | ||
6 | + "port_pairs": [ | ||
7 | + "875dfeda-43ed-23fe-454b-764feab2c342", | ||
8 | + "78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae" | ||
9 | + ] | ||
10 | +} | ||
11 | +} |
-
Please register or login to post a comment