blob: be645be04c3ec9c141976ea41d10fa493e70b5e4 [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;
43import org.onosproject.vtnrsc.FlowClassifier;
44import org.onosproject.vtnrsc.FlowClassifierId;
45import org.onosproject.vtnrsc.TenantId;
46import org.onosproject.vtnrsc.VirtualPortId;
47import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
48
49import com.eclipsesource.json.JsonObject;
50import com.sun.jersey.api.client.ClientResponse;
51import com.sun.jersey.api.client.UniformInterfaceException;
52import com.sun.jersey.api.client.WebResource;
53/**
54 * Unit tests for flow classifier REST APIs.
55 */
56public 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
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530230 expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
Bharat saraswalf38e2262015-11-18 22:57:05 +0530231 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}