blob: 124290709ae1e7b139d5e9e47dbaeeb752553c52 [file] [log] [blame]
Bharat saraswalf38e2262015-11-18 22:57:05 +05301/*
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 */
16package org.onosproject.vtnweb.resources;
17
18import static org.easymock.EasyMock.anyObject;
19import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
21import static org.easymock.EasyMock.replay;
22import static org.hamcrest.Matchers.containsString;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.fail;
27
28import java.io.InputStream;
29import java.net.HttpURLConnection;
30import java.util.HashSet;
31import java.util.Objects;
32import java.util.Set;
33
34import javax.ws.rs.core.MediaType;
35
Jian Li80cfe452016-01-14 16:04:58 -080036import com.eclipsesource.json.Json;
Bharat saraswalf38e2262015-11-18 22:57:05 +053037import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.onlab.osgi.ServiceDirectory;
41import org.onlab.osgi.TestServiceDirectory;
42import org.onlab.packet.IpPrefix;
43import org.onlab.rest.BaseResource;
Phaneendra Mandad66dca42015-12-01 20:24:10 +053044import org.onosproject.codec.CodecService;
Bharat saraswalf38e2262015-11-18 22:57:05 +053045import org.onosproject.vtnrsc.FlowClassifier;
46import org.onosproject.vtnrsc.FlowClassifierId;
47import org.onosproject.vtnrsc.TenantId;
48import org.onosproject.vtnrsc.VirtualPortId;
49import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Phaneendra Mandad66dca42015-12-01 20:24:10 +053050import org.onosproject.vtnweb.web.SfcCodecContext;
Bharat saraswalf38e2262015-11-18 22:57:05 +053051
52import com.eclipsesource.json.JsonObject;
53import com.sun.jersey.api.client.ClientResponse;
54import com.sun.jersey.api.client.UniformInterfaceException;
55import com.sun.jersey.api.client.WebResource;
56/**
57 * Unit tests for flow classifier REST APIs.
58 */
59public class FlowClassifierResourceTest extends VtnResourceTest {
60
61 final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class);
62
63 FlowClassifierId flowClassifierId1 = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051");
64 TenantId tenantId1 = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1");
65 VirtualPortId srcPortId1 = VirtualPortId.portId("dace4513-24fc-4fae-af4b-321c5e2eb3d1");
66 VirtualPortId dstPortId1 = VirtualPortId.portId("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
67
68 final MockFlowClassifier flowClassifier1 = new MockFlowClassifier(flowClassifierId1, tenantId1, "flowClassifier1",
69 "Mock flow classifier", "IPv4", "IP", 1001, 1500,
70 5001, 6000, IpPrefix.valueOf("1.1.1.1/16"),
71 IpPrefix.valueOf("22.12.34.45/16"),
72 srcPortId1, dstPortId1);
73
74 /**
75 * Mock class for a flow classifier.
76 */
77 private static class MockFlowClassifier implements FlowClassifier {
78
79 private final FlowClassifierId flowClassifierId;
80 private final TenantId tenantId;
81 private final String name;
82 private final String description;
83 private final String etherType;
84 private final String protocol;
85 private final int minSrcPortRange;
86 private final int maxSrcPortRange;
87 private final int minDstPortRange;
88 private final int maxDstPortRange;
89 private final IpPrefix srcIpPrefix;
90 private final IpPrefix dstIpPrefix;
91 private final VirtualPortId srcPort;
92 private final VirtualPortId dstPort;
93
94 public MockFlowClassifier(FlowClassifierId flowClassifierId, TenantId tenantId, String name,
95 String description, String etherType, String protocol, int minSrcPortRange,
96 int maxSrcPortRange, int minDstPortRange, int maxDstPortRange, IpPrefix srcIpPrefix,
97 IpPrefix dstIpPrefix, VirtualPortId srcPort, VirtualPortId dstPort) {
98 this.flowClassifierId = flowClassifierId;
99 this.tenantId = tenantId;
100 this.name = name;
101 this.description = description;
102 this.etherType = etherType;
103 this.protocol = protocol;
104 this.minSrcPortRange = minSrcPortRange;
105 this.maxSrcPortRange = maxSrcPortRange;
106 this.minDstPortRange = minDstPortRange;
107 this.maxDstPortRange = maxDstPortRange;
108 this.srcIpPrefix = srcIpPrefix;
109 this.dstIpPrefix = dstIpPrefix;
110 this.srcPort = srcPort;
111 this.dstPort = dstPort;
112 }
113
114
115 @Override
116 public FlowClassifierId flowClassifierId() {
117 return flowClassifierId;
118 }
119
120 @Override
121 public TenantId tenantId() {
122 return tenantId;
123 }
124
125 @Override
126 public String name() {
127 return name;
128 }
129
130 @Override
131 public String description() {
132 return description;
133 }
134
135 @Override
136 public String etherType() {
137 return etherType;
138 }
139
140 @Override
141 public String protocol() {
142 return protocol;
143 }
144
145 @Override
146 public int minSrcPortRange() {
147 return minSrcPortRange;
148 }
149
150 @Override
151 public int maxSrcPortRange() {
152 return maxSrcPortRange;
153 }
154
155 @Override
156 public int minDstPortRange() {
157 return minDstPortRange;
158 }
159
160 @Override
161 public int maxDstPortRange() {
162 return maxDstPortRange;
163 }
164
165 @Override
166 public IpPrefix srcIpPrefix() {
167 return srcIpPrefix;
168 }
169
170 @Override
171 public IpPrefix dstIpPrefix() {
172 return dstIpPrefix;
173 }
174
175 @Override
176 public VirtualPortId srcPort() {
177 return srcPort;
178 }
179
180 @Override
181 public VirtualPortId dstPort() {
182 return dstPort;
183 }
184
185 @Override
186 public boolean exactMatch(FlowClassifier flowClassifier) {
187 return this.equals(flowClassifier) &&
188 Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) &&
189 Objects.equals(this.tenantId, flowClassifier.tenantId());
190 }
191 }
192
193 /**
194 * Sets up the global values for all the tests.
195 */
196 @Before
197 public void setUpTest() {
Phaneendra Mandad66dca42015-12-01 20:24:10 +0530198 SfcCodecContext context = new SfcCodecContext();
199
200 ServiceDirectory testDirectory = new TestServiceDirectory()
201 .add(FlowClassifierService.class, flowClassifierService)
202 .add(CodecService.class, context.codecManager());
Bharat saraswalf38e2262015-11-18 22:57:05 +0530203 BaseResource.setServiceDirectory(testDirectory);
204
205 }
206
207 /**
208 * Cleans up.
209 */
210 @After
211 public void tearDownTest() {
212 }
213
214 /**
215 * Tests the result of the rest api GET when there are no flow classifiers.
216 */
217 @Test
218 public void testFlowClassifiersEmpty() {
219
220 expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes();
221 replay(flowClassifierService);
222 final WebResource rs = resource();
223 final String response = rs.path("flow_classifiers").get(String.class);
224 assertThat(response, is("{\"flow_classifiers\":[]}"));
225 }
226
227 /**
228 * Tests the result of a rest api GET for flow classifier id.
229 */
230 @Test
231 public void testGetFlowClassifierId() {
232
233 final Set<FlowClassifier> flowClassifiers = new HashSet<>();
234 flowClassifiers.add(flowClassifier1);
235
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530236 expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530237 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
238 replay(flowClassifierService);
239
240 final WebResource rs = resource();
241 final String response = rs.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800242 final JsonObject result = Json.parse(response).asObject();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530243 assertThat(result, notNullValue());
244 }
245
246 /**
247 * Tests that a fetch of a non-existent flow classifier object throws an exception.
248 */
249 @Test
250 public void testBadGet() {
251 expect(flowClassifierService.getFlowClassifier(anyObject()))
252 .andReturn(null).anyTimes();
253 replay(flowClassifierService);
254 WebResource rs = resource();
255 try {
256 rs.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
257 fail("Fetch of non-existent flow classifier did not throw an exception");
258 } catch (UniformInterfaceException ex) {
259 assertThat(ex.getMessage(),
260 containsString("returned a response status of"));
261 }
262 }
263
264 /**
265 * Tests creating a flow classifier with POST.
266 */
267 @Test
268 public void testPost() {
269
270 expect(flowClassifierService.createFlowClassifier(anyObject()))
271 .andReturn(true).anyTimes();
272 replay(flowClassifierService);
273
274 WebResource rs = resource();
275 InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json");
276
277 ClientResponse response = rs.path("flow_classifiers")
278 .type(MediaType.APPLICATION_JSON_TYPE)
279 .post(ClientResponse.class, jsonStream);
280 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
281 }
282
283 /**
284 * Tests deleting a flow classifier.
285 */
286 @Test
287 public void testDelete() {
288 expect(flowClassifierService.removeFlowClassifier(anyObject()))
289 .andReturn(true).anyTimes();
290 replay(flowClassifierService);
291
292 WebResource rs = resource();
293
294 String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051";
295
296 ClientResponse deleteResponse = rs.path(location)
297 .type(MediaType.APPLICATION_JSON_TYPE)
298 .delete(ClientResponse.class);
299 assertThat(deleteResponse.getStatus(),
300 is(HttpURLConnection.HTTP_NO_CONTENT));
301 }
302}