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