Committed by
Gerrit Code Review
[ONOS-3163] Flow classifier web resource UT. and web resource fixes.
Change-Id: Ie23a450e2944c969753af5f5afb38780833b5a7e
Showing
4 changed files
with
413 additions
and
76 deletions
... | @@ -15,15 +15,12 @@ | ... | @@ -15,15 +15,12 @@ |
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.INTERNAL_SERVER_ERROR; | ||
19 | -import static org.onlab.util.Tools.nullIsNotFound; | ||
20 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; | 18 | import static javax.ws.rs.core.Response.Status.NOT_FOUND; |
19 | +import static javax.ws.rs.core.Response.Status.OK; | ||
20 | +import static org.onlab.util.Tools.nullIsNotFound; | ||
21 | 21 | ||
22 | import java.io.IOException; | 22 | import java.io.IOException; |
23 | import java.io.InputStream; | 23 | import java.io.InputStream; |
24 | -import java.net.URI; | ||
25 | -import java.net.URISyntaxException; | ||
26 | -import java.util.UUID; | ||
27 | 24 | ||
28 | import javax.ws.rs.Consumes; | 25 | import javax.ws.rs.Consumes; |
29 | import javax.ws.rs.DELETE; | 26 | import javax.ws.rs.DELETE; |
... | @@ -36,13 +33,17 @@ import javax.ws.rs.Produces; | ... | @@ -36,13 +33,17 @@ import javax.ws.rs.Produces; |
36 | import javax.ws.rs.core.MediaType; | 33 | import javax.ws.rs.core.MediaType; |
37 | import javax.ws.rs.core.Response; | 34 | import javax.ws.rs.core.Response; |
38 | 35 | ||
36 | +import org.onosproject.rest.AbstractWebResource; | ||
39 | import org.onosproject.vtnrsc.FlowClassifier; | 37 | import org.onosproject.vtnrsc.FlowClassifier; |
40 | import org.onosproject.vtnrsc.FlowClassifierId; | 38 | import org.onosproject.vtnrsc.FlowClassifierId; |
41 | -import org.onosproject.rest.AbstractWebResource; | ||
42 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; | 39 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
43 | import org.onosproject.vtnweb.web.FlowClassifierCodec; | 40 | import org.onosproject.vtnweb.web.FlowClassifierCodec; |
41 | +import org.slf4j.Logger; | ||
42 | +import org.slf4j.LoggerFactory; | ||
44 | 43 | ||
44 | +import com.fasterxml.jackson.databind.JsonNode; | ||
45 | import com.fasterxml.jackson.databind.ObjectMapper; | 45 | import com.fasterxml.jackson.databind.ObjectMapper; |
46 | +import com.fasterxml.jackson.databind.node.ArrayNode; | ||
46 | import com.fasterxml.jackson.databind.node.ObjectNode; | 47 | import com.fasterxml.jackson.databind.node.ObjectNode; |
47 | 48 | ||
48 | /** | 49 | /** |
... | @@ -51,73 +52,51 @@ import com.fasterxml.jackson.databind.node.ObjectNode; | ... | @@ -51,73 +52,51 @@ import com.fasterxml.jackson.databind.node.ObjectNode; |
51 | @Path("flow_classifiers") | 52 | @Path("flow_classifiers") |
52 | public class FlowClassifierWebResource extends AbstractWebResource { | 53 | public class FlowClassifierWebResource extends AbstractWebResource { |
53 | 54 | ||
55 | + private final Logger log = LoggerFactory.getLogger(FlowClassifierWebResource.class); | ||
56 | + | ||
54 | final FlowClassifierService service = get(FlowClassifierService.class); | 57 | final FlowClassifierService service = get(FlowClassifierService.class); |
55 | - final ObjectNode root = mapper().createObjectNode(); | ||
56 | public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; | 58 | public static final String FLOW_CLASSIFIER_NOT_FOUND = "Flow classifier not found"; |
57 | 59 | ||
58 | /** | 60 | /** |
59 | - * Get all flow classifiers created. Returns list of all flow classifiers | 61 | + * Get all flow classifiers created. |
60 | - * created. | ||
61 | * | 62 | * |
62 | - * @return 200 OK | 63 | + * @return 200 OK, 404 if given flow classifier does not exist |
63 | */ | 64 | */ |
64 | @GET | 65 | @GET |
65 | @Produces(MediaType.APPLICATION_JSON) | 66 | @Produces(MediaType.APPLICATION_JSON) |
66 | public Response getFlowClassifiers() { | 67 | public Response getFlowClassifiers() { |
67 | - Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); | 68 | + final Iterable<FlowClassifier> flowClassifiers = service.getFlowClassifiers(); |
68 | ObjectNode result = new ObjectMapper().createObjectNode(); | 69 | ObjectNode result = new ObjectMapper().createObjectNode(); |
69 | - result.set("flow_classifiers", new FlowClassifierCodec().encode(flowClassifiers, this)); | 70 | + ArrayNode flowClassifierEntry = result.putArray("flow_classifiers"); |
71 | + if (flowClassifiers != null) { | ||
72 | + for (final FlowClassifier flowClassifier : flowClassifiers) { | ||
73 | + flowClassifierEntry.add(new FlowClassifierCodec().encode(flowClassifier, this)); | ||
74 | + } | ||
75 | + } | ||
70 | return ok(result.toString()).build(); | 76 | return ok(result.toString()).build(); |
71 | } | 77 | } |
72 | 78 | ||
73 | /** | 79 | /** |
74 | - * Get details of a flow classifier. Returns details of a specified flow | 80 | + * Get details of a flow classifier. |
75 | - * classifier id. | ||
76 | * | 81 | * |
77 | * @param id flow classifier id | 82 | * @param id flow classifier id |
78 | - * @return 200 OK | 83 | + * @return 200 OK , 404 if given identifier does not exist |
79 | */ | 84 | */ |
80 | @GET | 85 | @GET |
81 | @Path("{flow_id}") | 86 | @Path("{flow_id}") |
82 | @Produces(MediaType.APPLICATION_JSON) | 87 | @Produces(MediaType.APPLICATION_JSON) |
83 | public Response getFlowClassifier(@PathParam("flow_id") String id) { | 88 | public Response getFlowClassifier(@PathParam("flow_id") String id) { |
84 | 89 | ||
85 | - if (!service.hasFlowClassifier(FlowClassifierId.of(UUID.fromString(id)))) { | 90 | + if (!service.hasFlowClassifier(FlowClassifierId.of(id))) { |
86 | return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); | 91 | return Response.status(NOT_FOUND).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); |
87 | } | 92 | } |
88 | - FlowClassifier flowClassifier = nullIsNotFound( | 93 | + FlowClassifier flowClassifier = nullIsNotFound(service.getFlowClassifier(FlowClassifierId.of(id)), |
89 | - service.getFlowClassifier(FlowClassifierId.of(UUID.fromString(id))), | ||
90 | FLOW_CLASSIFIER_NOT_FOUND); | 94 | FLOW_CLASSIFIER_NOT_FOUND); |
91 | 95 | ||
92 | ObjectNode result = new ObjectMapper().createObjectNode(); | 96 | ObjectNode result = new ObjectMapper().createObjectNode(); |
93 | result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); | 97 | result.set("flow_classifier", new FlowClassifierCodec().encode(flowClassifier, this)); |
94 | - return ok(result.toString()).build(); | ||
95 | - } | ||
96 | - | ||
97 | - /** | ||
98 | - * Creates and stores a new flow classifier. | ||
99 | - * | ||
100 | - * @param flowClassifierId flow classifier identifier | ||
101 | - * @param stream flow classifier from JSON | ||
102 | - * @return status of the request - CREATED if the JSON is correct, | ||
103 | - * BAD_REQUEST if the JSON is invalid | ||
104 | - */ | ||
105 | - @POST | ||
106 | - @Path("{flow_id}") | ||
107 | - @Consumes(MediaType.APPLICATION_JSON) | ||
108 | - @Produces(MediaType.APPLICATION_JSON) | ||
109 | - public Response createFlowClassifier(@PathParam("flow_id") String flowClassifierId, InputStream stream) { | ||
110 | - URI location; | ||
111 | - try { | ||
112 | - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | ||
113 | 98 | ||
114 | - FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); | 99 | + return ok(result.toString()).build(); |
115 | - service.createFlowClassifier(flowClassifier); | ||
116 | - location = new URI(flowClassifierId); | ||
117 | - } catch (IOException | URISyntaxException ex) { | ||
118 | - throw new IllegalArgumentException(ex); | ||
119 | - } | ||
120 | - return Response.created(location).build(); | ||
121 | } | 100 | } |
122 | 101 | ||
123 | /** | 102 | /** |
... | @@ -125,32 +104,32 @@ public class FlowClassifierWebResource extends AbstractWebResource { | ... | @@ -125,32 +104,32 @@ public class FlowClassifierWebResource extends AbstractWebResource { |
125 | * | 104 | * |
126 | * @param stream flow classifier from JSON | 105 | * @param stream flow classifier from JSON |
127 | * @return status of the request - CREATED if the JSON is correct, | 106 | * @return status of the request - CREATED if the JSON is correct, |
128 | - * BAD_REQUEST if the JSON is invalid | 107 | + * BAD_REQUEST if the JSON is invalid |
129 | */ | 108 | */ |
130 | @POST | 109 | @POST |
131 | @Consumes(MediaType.APPLICATION_JSON) | 110 | @Consumes(MediaType.APPLICATION_JSON) |
132 | @Produces(MediaType.APPLICATION_JSON) | 111 | @Produces(MediaType.APPLICATION_JSON) |
133 | public Response createFlowClassifier(InputStream stream) { | 112 | public Response createFlowClassifier(InputStream stream) { |
134 | - URI location; | ||
135 | try { | 113 | try { |
136 | - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | 114 | + ObjectMapper mapper = new ObjectMapper(); |
137 | - | 115 | + ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream); |
138 | - FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); | 116 | + JsonNode flow = jsonTree.get("flow_classifier"); |
139 | - service.createFlowClassifier(flowClassifier); | 117 | + |
140 | - location = new URI(flowClassifier.flowClassifierId().toString()); | 118 | + FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); |
141 | - } catch (IOException | URISyntaxException ex) { | 119 | + Boolean issuccess = nullIsNotFound(service.createFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); |
120 | + return Response.status(OK).entity(issuccess.toString()).build(); | ||
121 | + } catch (IOException ex) { | ||
122 | + log.error("Exception while creating flow classifier {}.", ex.toString()); | ||
142 | throw new IllegalArgumentException(ex); | 123 | throw new IllegalArgumentException(ex); |
143 | } | 124 | } |
144 | - return Response.created(location).build(); | ||
145 | } | 125 | } |
146 | 126 | ||
147 | /** | 127 | /** |
148 | - * Update details of a flow classifier. Update details of a specified flow | 128 | + * Update details of a flow classifier. |
149 | - * classifier id. | ||
150 | * | 129 | * |
151 | * @param id flow classifier id | 130 | * @param id flow classifier id |
152 | * @param stream InputStream | 131 | * @param stream InputStream |
153 | - * @return 200 OK | 132 | + * @return 200 OK, 404 if given identifier does not exist |
154 | */ | 133 | */ |
155 | @PUT | 134 | @PUT |
156 | @Path("{flow_id}") | 135 | @Path("{flow_id}") |
... | @@ -158,35 +137,29 @@ public class FlowClassifierWebResource extends AbstractWebResource { | ... | @@ -158,35 +137,29 @@ public class FlowClassifierWebResource extends AbstractWebResource { |
158 | @Consumes(MediaType.APPLICATION_JSON) | 137 | @Consumes(MediaType.APPLICATION_JSON) |
159 | public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) { | 138 | public Response updateFlowClassifier(@PathParam("flow_id") String id, final InputStream stream) { |
160 | try { | 139 | try { |
161 | - ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream); | 140 | + |
162 | - FlowClassifier flowClassifier = codec(FlowClassifier.class).decode(jsonTree, this); | 141 | + JsonNode jsonTree = mapper().readTree(stream); |
142 | + JsonNode flow = jsonTree.get("flow_classifier"); | ||
143 | + FlowClassifier flowClassifier = new FlowClassifierCodec().decode((ObjectNode) flow, this); | ||
163 | Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); | 144 | Boolean result = nullIsNotFound(service.updateFlowClassifier(flowClassifier), FLOW_CLASSIFIER_NOT_FOUND); |
164 | - if (!result) { | 145 | + return Response.status(OK).entity(result.toString()).build(); |
165 | - return Response.status(204).entity(FLOW_CLASSIFIER_NOT_FOUND).build(); | 146 | + } catch (IOException e) { |
166 | - } | 147 | + log.error("Update flow classifier failed because of exception {}.", e.toString()); |
167 | - return Response.status(203).entity(result.toString()).build(); | 148 | + throw new IllegalArgumentException(e); |
168 | - } catch (Exception e) { | ||
169 | - return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); | ||
170 | } | 149 | } |
171 | } | 150 | } |
172 | 151 | ||
173 | /** | 152 | /** |
174 | - * Delete details of a flow classifier. Delete details of a specified flow | 153 | + * Delete details of a flow classifier. |
175 | - * classifier id. | ||
176 | * | 154 | * |
177 | * @param id flow classifier id | 155 | * @param id flow classifier id |
178 | - * @return 200 OK | ||
179 | - * @throws IOException when input doesn't match. | ||
180 | */ | 156 | */ |
181 | @Path("{flow_id}") | 157 | @Path("{flow_id}") |
182 | @DELETE | 158 | @DELETE |
183 | - public Response deleteFlowClassifier(@PathParam("flow_id") String id) throws IOException { | 159 | + public void deleteFlowClassifier(@PathParam("flow_id") String id) { |
184 | - try { | 160 | + log.debug("Deletes flow classifier by identifier {}.", id); |
185 | - FlowClassifierId flowClassifierId = FlowClassifierId.of(UUID.fromString(id)); | 161 | + FlowClassifierId flowClassifierId = FlowClassifierId.of(id); |
186 | - service.removeFlowClassifier(flowClassifierId); | 162 | + Boolean issuccess = nullIsNotFound(service.removeFlowClassifier(flowClassifierId), FLOW_CLASSIFIER_NOT_FOUND); |
187 | - return Response.status(201).entity("SUCCESS").build(); | 163 | + |
188 | - } catch (Exception e) { | ||
189 | - return Response.status(INTERNAL_SERVER_ERROR).entity(e.toString()).build(); | ||
190 | - } | ||
191 | } | 164 | } |
192 | } | 165 | } | ... | ... |
apps/vtn/vtnweb/src/test/java/org/onosproject/vtnweb/resources/FlowClassifierResourceTest.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.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.packet.IpPrefix; | ||
42 | +import org.onlab.rest.BaseResource; | ||
43 | +import org.onosproject.vtnrsc.FlowClassifier; | ||
44 | +import org.onosproject.vtnrsc.FlowClassifierId; | ||
45 | +import org.onosproject.vtnrsc.TenantId; | ||
46 | +import org.onosproject.vtnrsc.VirtualPortId; | ||
47 | +import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; | ||
48 | + | ||
49 | +import com.eclipsesource.json.JsonObject; | ||
50 | +import com.sun.jersey.api.client.ClientResponse; | ||
51 | +import com.sun.jersey.api.client.UniformInterfaceException; | ||
52 | +import com.sun.jersey.api.client.WebResource; | ||
53 | +/** | ||
54 | + * Unit tests for flow classifier REST APIs. | ||
55 | + */ | ||
56 | +public class FlowClassifierResourceTest extends VtnResourceTest { | ||
57 | + | ||
58 | + final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class); | ||
59 | + | ||
60 | + FlowClassifierId flowClassifierId1 = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051"); | ||
61 | + TenantId tenantId1 = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1"); | ||
62 | + VirtualPortId srcPortId1 = VirtualPortId.portId("dace4513-24fc-4fae-af4b-321c5e2eb3d1"); | ||
63 | + VirtualPortId dstPortId1 = VirtualPortId.portId("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"); | ||
64 | + | ||
65 | + final MockFlowClassifier flowClassifier1 = new MockFlowClassifier(flowClassifierId1, tenantId1, "flowClassifier1", | ||
66 | + "Mock flow classifier", "IPv4", "IP", 1001, 1500, | ||
67 | + 5001, 6000, IpPrefix.valueOf("1.1.1.1/16"), | ||
68 | + IpPrefix.valueOf("22.12.34.45/16"), | ||
69 | + srcPortId1, dstPortId1); | ||
70 | + | ||
71 | + /** | ||
72 | + * Mock class for a flow classifier. | ||
73 | + */ | ||
74 | + private static class MockFlowClassifier implements FlowClassifier { | ||
75 | + | ||
76 | + private final FlowClassifierId flowClassifierId; | ||
77 | + private final TenantId tenantId; | ||
78 | + private final String name; | ||
79 | + private final String description; | ||
80 | + private final String etherType; | ||
81 | + private final String protocol; | ||
82 | + private final int minSrcPortRange; | ||
83 | + private final int maxSrcPortRange; | ||
84 | + private final int minDstPortRange; | ||
85 | + private final int maxDstPortRange; | ||
86 | + private final IpPrefix srcIpPrefix; | ||
87 | + private final IpPrefix dstIpPrefix; | ||
88 | + private final VirtualPortId srcPort; | ||
89 | + private final VirtualPortId dstPort; | ||
90 | + | ||
91 | + public MockFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name, | ||
92 | + String description, String etherType, String protocol, int minSrcPortRange, | ||
93 | + int maxSrcPortRange, int minDstPortRange, int maxDstPortRange, IpPrefix srcIpPrefix, | ||
94 | + IpPrefix dstIpPrefix, VirtualPortId srcPort, VirtualPortId dstPort) { | ||
95 | + this.flowClassifierId = flowClassifierId; | ||
96 | + this.tenantId = tenantId; | ||
97 | + this.name = name; | ||
98 | + this.description = description; | ||
99 | + this.etherType = etherType; | ||
100 | + this.protocol = protocol; | ||
101 | + this.minSrcPortRange = minSrcPortRange; | ||
102 | + this.maxSrcPortRange = maxSrcPortRange; | ||
103 | + this.minDstPortRange = minDstPortRange; | ||
104 | + this.maxDstPortRange = maxDstPortRange; | ||
105 | + this.srcIpPrefix = srcIpPrefix; | ||
106 | + this.dstIpPrefix = dstIpPrefix; | ||
107 | + this.srcPort = srcPort; | ||
108 | + this.dstPort = dstPort; | ||
109 | + } | ||
110 | + | ||
111 | + | ||
112 | + @Override | ||
113 | + public FlowClassifierId flowClassifierId() { | ||
114 | + return flowClassifierId; | ||
115 | + } | ||
116 | + | ||
117 | + @Override | ||
118 | + public TenantId tenantId() { | ||
119 | + return tenantId; | ||
120 | + } | ||
121 | + | ||
122 | + @Override | ||
123 | + public String name() { | ||
124 | + return name; | ||
125 | + } | ||
126 | + | ||
127 | + @Override | ||
128 | + public String description() { | ||
129 | + return description; | ||
130 | + } | ||
131 | + | ||
132 | + @Override | ||
133 | + public String etherType() { | ||
134 | + return etherType; | ||
135 | + } | ||
136 | + | ||
137 | + @Override | ||
138 | + public String protocol() { | ||
139 | + return protocol; | ||
140 | + } | ||
141 | + | ||
142 | + @Override | ||
143 | + public int minSrcPortRange() { | ||
144 | + return minSrcPortRange; | ||
145 | + } | ||
146 | + | ||
147 | + @Override | ||
148 | + public int maxSrcPortRange() { | ||
149 | + return maxSrcPortRange; | ||
150 | + } | ||
151 | + | ||
152 | + @Override | ||
153 | + public int minDstPortRange() { | ||
154 | + return minDstPortRange; | ||
155 | + } | ||
156 | + | ||
157 | + @Override | ||
158 | + public int maxDstPortRange() { | ||
159 | + return maxDstPortRange; | ||
160 | + } | ||
161 | + | ||
162 | + @Override | ||
163 | + public IpPrefix srcIpPrefix() { | ||
164 | + return srcIpPrefix; | ||
165 | + } | ||
166 | + | ||
167 | + @Override | ||
168 | + public IpPrefix dstIpPrefix() { | ||
169 | + return dstIpPrefix; | ||
170 | + } | ||
171 | + | ||
172 | + @Override | ||
173 | + public VirtualPortId srcPort() { | ||
174 | + return srcPort; | ||
175 | + } | ||
176 | + | ||
177 | + @Override | ||
178 | + public VirtualPortId dstPort() { | ||
179 | + return dstPort; | ||
180 | + } | ||
181 | + | ||
182 | + @Override | ||
183 | + public boolean exactMatch(FlowClassifier flowClassifier) { | ||
184 | + return this.equals(flowClassifier) && | ||
185 | + Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) && | ||
186 | + Objects.equals(this.tenantId, flowClassifier.tenantId()); | ||
187 | + } | ||
188 | + } | ||
189 | + | ||
190 | + /** | ||
191 | + * Sets up the global values for all the tests. | ||
192 | + */ | ||
193 | + @Before | ||
194 | + public void setUpTest() { | ||
195 | + ServiceDirectory testDirectory = new TestServiceDirectory().add(FlowClassifierService.class, | ||
196 | + flowClassifierService); | ||
197 | + BaseResource.setServiceDirectory(testDirectory); | ||
198 | + | ||
199 | + } | ||
200 | + | ||
201 | + /** | ||
202 | + * Cleans up. | ||
203 | + */ | ||
204 | + @After | ||
205 | + public void tearDownTest() { | ||
206 | + } | ||
207 | + | ||
208 | + /** | ||
209 | + * Tests the result of the rest api GET when there are no flow classifiers. | ||
210 | + */ | ||
211 | + @Test | ||
212 | + public void testFlowClassifiersEmpty() { | ||
213 | + | ||
214 | + expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes(); | ||
215 | + replay(flowClassifierService); | ||
216 | + final WebResource rs = resource(); | ||
217 | + final String response = rs.path("flow_classifiers").get(String.class); | ||
218 | + assertThat(response, is("{\"flow_classifiers\":[]}")); | ||
219 | + } | ||
220 | + | ||
221 | + /** | ||
222 | + * Tests the result of a rest api GET for flow classifier id. | ||
223 | + */ | ||
224 | + @Test | ||
225 | + public void testGetFlowClassifierId() { | ||
226 | + | ||
227 | + final Set<FlowClassifier> flowClassifiers = new HashSet<>(); | ||
228 | + flowClassifiers.add(flowClassifier1); | ||
229 | + | ||
230 | + expect(flowClassifierService.hasFlowClassifier(anyObject())).andReturn(true).anyTimes(); | ||
231 | + expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes(); | ||
232 | + replay(flowClassifierService); | ||
233 | + | ||
234 | + final WebResource rs = resource(); | ||
235 | + final String response = rs.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051").get(String.class); | ||
236 | + final JsonObject result = JsonObject.readFrom(response); | ||
237 | + assertThat(result, notNullValue()); | ||
238 | + } | ||
239 | + | ||
240 | + /** | ||
241 | + * Tests that a fetch of a non-existent flow classifier object throws an exception. | ||
242 | + */ | ||
243 | + @Test | ||
244 | + public void testBadGet() { | ||
245 | + expect(flowClassifierService.getFlowClassifier(anyObject())) | ||
246 | + .andReturn(null).anyTimes(); | ||
247 | + replay(flowClassifierService); | ||
248 | + WebResource rs = resource(); | ||
249 | + try { | ||
250 | + rs.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class); | ||
251 | + fail("Fetch of non-existent flow classifier did not throw an exception"); | ||
252 | + } catch (UniformInterfaceException ex) { | ||
253 | + assertThat(ex.getMessage(), | ||
254 | + containsString("returned a response status of")); | ||
255 | + } | ||
256 | + } | ||
257 | + | ||
258 | + /** | ||
259 | + * Tests creating a flow classifier with POST. | ||
260 | + */ | ||
261 | + @Test | ||
262 | + public void testPost() { | ||
263 | + | ||
264 | + expect(flowClassifierService.createFlowClassifier(anyObject())) | ||
265 | + .andReturn(true).anyTimes(); | ||
266 | + replay(flowClassifierService); | ||
267 | + | ||
268 | + WebResource rs = resource(); | ||
269 | + InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json"); | ||
270 | + | ||
271 | + ClientResponse response = rs.path("flow_classifiers") | ||
272 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
273 | + .post(ClientResponse.class, jsonStream); | ||
274 | + assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); | ||
275 | + } | ||
276 | + | ||
277 | + /** | ||
278 | + * Tests deleting a flow classifier. | ||
279 | + */ | ||
280 | + @Test | ||
281 | + public void testDelete() { | ||
282 | + expect(flowClassifierService.removeFlowClassifier(anyObject())) | ||
283 | + .andReturn(true).anyTimes(); | ||
284 | + replay(flowClassifierService); | ||
285 | + | ||
286 | + WebResource rs = resource(); | ||
287 | + | ||
288 | + String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051"; | ||
289 | + | ||
290 | + ClientResponse deleteResponse = rs.path(location) | ||
291 | + .type(MediaType.APPLICATION_JSON_TYPE) | ||
292 | + .delete(ClientResponse.class); | ||
293 | + assertThat(deleteResponse.getStatus(), | ||
294 | + is(HttpURLConnection.HTTP_NO_CONTENT)); | ||
295 | + } | ||
296 | +} |
1 | +/* | ||
2 | + * Copyright 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 java.io.IOException; | ||
19 | +import java.net.ServerSocket; | ||
20 | + | ||
21 | +import com.sun.jersey.test.framework.AppDescriptor; | ||
22 | +import com.sun.jersey.test.framework.JerseyTest; | ||
23 | +import com.sun.jersey.test.framework.WebAppDescriptor; | ||
24 | + | ||
25 | +/** | ||
26 | + * Base class for VTN REST API tests. Performs common configuration operations. | ||
27 | + */ | ||
28 | +public class VtnResourceTest extends JerseyTest { | ||
29 | + | ||
30 | + /** | ||
31 | + * Assigns an available port for the test. | ||
32 | + * | ||
33 | + * @param defaultPort If a port cannot be determined, this one is used. | ||
34 | + * @return free port | ||
35 | + */ | ||
36 | + @Override | ||
37 | + public int getPort(int defaultPort) { | ||
38 | + try { | ||
39 | + ServerSocket socket = new ServerSocket(0); | ||
40 | + socket.setReuseAddress(true); | ||
41 | + int port = socket.getLocalPort(); | ||
42 | + socket.close(); | ||
43 | + return port; | ||
44 | + } catch (IOException ioe) { | ||
45 | + return defaultPort; | ||
46 | + } | ||
47 | + } | ||
48 | + | ||
49 | + @Override | ||
50 | + public AppDescriptor configure() { | ||
51 | + return new WebAppDescriptor.Builder("org.onosproject.vtnweb.resources").build(); | ||
52 | + } | ||
53 | + | ||
54 | +} |
apps/vtn/vtnweb/src/test/resources/org/onosproject/vtnweb/resources/post-FlowClassifier.json
0 → 100644
1 | +{"flow_classifier": { | ||
2 | + "id": "4a334cd4-fe9c-4fae-af4b-321c5e2eb051", | ||
3 | + "name": "flow1", | ||
4 | + "tenant_id": "1814726e2d22407b8ca76db5e567dcf1", | ||
5 | + "description": "flow classifier", | ||
6 | + "ethertype": "IPv4", | ||
7 | + "protocol": "tcp", | ||
8 | + "source_port_range_min": 22, "source_port_range_max": 4000, | ||
9 | + "destination_port_range_min": 80, "destination_port_range_max": 80, | ||
10 | + "source_ip_prefix": "1.1.1.1/16" , "destination_ip_prefix": "22.12.34.45/16", | ||
11 | + "logical_destination_port": "dace4513-24fc-4fae-af4b-321c5e2eb3d1", | ||
12 | + "logical_source_port": "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345" | ||
13 | + } | ||
14 | +} |
-
Please register or login to post a comment