2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnweb.resources;
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;
28 import java.io.InputStream;
29 import java.net.HttpURLConnection;
30 import java.util.HashSet;
31 import java.util.Objects;
34 import javax.ws.rs.core.MediaType;
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;
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;
54 * Unit tests for flow classifier REST APIs.
56 public class FlowClassifierResourceTest extends VtnResourceTest {
58 final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class);
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");
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);
72 * Mock class for a flow classifier.
74 private static class MockFlowClassifier implements FlowClassifier {
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;
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;
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;
113 public FlowClassifierId flowClassifierId() {
114 return flowClassifierId;
118 public TenantId tenantId() {
123 public String name() {
128 public String description() {
133 public String etherType() {
138 public String protocol() {
143 public int minSrcPortRange() {
144 return minSrcPortRange;
148 public int maxSrcPortRange() {
149 return maxSrcPortRange;
153 public int minDstPortRange() {
154 return minDstPortRange;
158 public int maxDstPortRange() {
159 return maxDstPortRange;
163 public IpPrefix srcIpPrefix() {
168 public IpPrefix dstIpPrefix() {
173 public VirtualPortId srcPort() {
178 public VirtualPortId dstPort() {
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());
191 * Sets up the global values for all the tests.
194 public void setUpTest() {
195 ServiceDirectory testDirectory = new TestServiceDirectory().add(FlowClassifierService.class,
196 flowClassifierService);
197 BaseResource.setServiceDirectory(testDirectory);
205 public void tearDownTest() {
209 * Tests the result of the rest api GET when there are no flow classifiers.
212 public void testFlowClassifiersEmpty() {
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\":[]}"));
222 * Tests the result of a rest api GET for flow classifier id.
225 public void testGetFlowClassifierId() {
227 final Set<FlowClassifier> flowClassifiers = new HashSet<>();
228 flowClassifiers.add(flowClassifier1);
230 expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes();
231 expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes();
232 replay(flowClassifierService);
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());
241 * Tests that a fetch of a non-existent flow classifier object throws an exception.
244 public void testBadGet() {
245 expect(flowClassifierService.getFlowClassifier(anyObject()))
246 .andReturn(null).anyTimes();
247 replay(flowClassifierService);
248 WebResource rs = resource();
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"));
259 * Tests creating a flow classifier with POST.
262 public void testPost() {
264 expect(flowClassifierService.createFlowClassifier(anyObject()))
265 .andReturn(true).anyTimes();
266 replay(flowClassifierService);
268 WebResource rs = resource();
269 InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json");
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));
278 * Tests deleting a flow classifier.
281 public void testDelete() {
282 expect(flowClassifierService.removeFlowClassifier(anyObject()))
283 .andReturn(true).anyTimes();
284 replay(flowClassifierService);
286 WebResource rs = resource();
288 String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051";
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));