Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 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 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 19 | import com.eclipsesource.json.JsonObject; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onlab.osgi.TestServiceDirectory; |
| 25 | import org.onlab.packet.IpPrefix; |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 26 | import org.onosproject.codec.CodecService; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 27 | import org.onosproject.vtnrsc.FlowClassifier; |
| 28 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 29 | import org.onosproject.vtnrsc.TenantId; |
| 30 | import org.onosproject.vtnrsc.VirtualPortId; |
| 31 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 32 | import org.onosproject.vtnweb.web.SfcCodecContext; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 33 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 34 | import javax.ws.rs.NotFoundException; |
| 35 | import javax.ws.rs.client.Entity; |
| 36 | import javax.ws.rs.client.WebTarget; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.io.InputStream; |
| 40 | import java.net.HttpURLConnection; |
| 41 | import java.util.HashSet; |
| 42 | import java.util.Objects; |
| 43 | import java.util.Set; |
| 44 | |
| 45 | import static org.easymock.EasyMock.anyObject; |
| 46 | import static org.easymock.EasyMock.createMock; |
| 47 | import static org.easymock.EasyMock.expect; |
| 48 | import static org.easymock.EasyMock.replay; |
| 49 | import static org.hamcrest.Matchers.containsString; |
| 50 | import static org.hamcrest.Matchers.is; |
| 51 | import static org.hamcrest.Matchers.notNullValue; |
| 52 | import static org.junit.Assert.assertThat; |
| 53 | import static org.junit.Assert.fail; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 54 | /** |
| 55 | * Unit tests for flow classifier REST APIs. |
| 56 | */ |
| 57 | public class FlowClassifierResourceTest extends VtnResourceTest { |
| 58 | |
| 59 | final FlowClassifierService flowClassifierService = createMock(FlowClassifierService.class); |
| 60 | |
| 61 | FlowClassifierId flowClassifierId1 = FlowClassifierId.of("4a334cd4-fe9c-4fae-af4b-321c5e2eb051"); |
| 62 | TenantId tenantId1 = TenantId.tenantId("1814726e2d22407b8ca76db5e567dcf1"); |
| 63 | VirtualPortId srcPortId1 = VirtualPortId.portId("dace4513-24fc-4fae-af4b-321c5e2eb3d1"); |
| 64 | VirtualPortId dstPortId1 = VirtualPortId.portId("aef3478a-4a56-2a6e-cd3a-9dee4e2ec345"); |
| 65 | |
| 66 | final MockFlowClassifier flowClassifier1 = new MockFlowClassifier(flowClassifierId1, tenantId1, "flowClassifier1", |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 67 | "Mock flow classifier", "IPv4", "IP", 10000, |
| 68 | 1001, 1500, 5001, 6000, |
| 69 | IpPrefix.valueOf("1.1.1.1/16"), |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 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; |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 84 | private final int priority; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 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, |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 95 | String description, String etherType, String protocol, int priority, |
| 96 | int minSrcPortRange, int maxSrcPortRange, int minDstPortRange, int maxDstPortRange, |
| 97 | IpPrefix srcIpPrefix, IpPrefix dstIpPrefix, VirtualPortId srcPort, |
| 98 | VirtualPortId dstPort) { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 99 | this.flowClassifierId = flowClassifierId; |
| 100 | this.tenantId = tenantId; |
| 101 | this.name = name; |
| 102 | this.description = description; |
| 103 | this.etherType = etherType; |
| 104 | this.protocol = protocol; |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 105 | this.priority = priority; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 106 | this.minSrcPortRange = minSrcPortRange; |
| 107 | this.maxSrcPortRange = maxSrcPortRange; |
| 108 | this.minDstPortRange = minDstPortRange; |
| 109 | this.maxDstPortRange = maxDstPortRange; |
| 110 | this.srcIpPrefix = srcIpPrefix; |
| 111 | this.dstIpPrefix = dstIpPrefix; |
| 112 | this.srcPort = srcPort; |
| 113 | this.dstPort = dstPort; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | @Override |
| 118 | public FlowClassifierId flowClassifierId() { |
| 119 | return flowClassifierId; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public TenantId tenantId() { |
| 124 | return tenantId; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String name() { |
| 129 | return name; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String description() { |
| 134 | return description; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public String etherType() { |
| 139 | return etherType; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public String protocol() { |
| 144 | return protocol; |
| 145 | } |
| 146 | |
| 147 | @Override |
Phaneendra Manda | 299877f | 2016-04-13 23:28:03 +0530 | [diff] [blame] | 148 | public int priority() { |
| 149 | return priority; |
| 150 | } |
| 151 | |
| 152 | @Override |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 153 | public int minSrcPortRange() { |
| 154 | return minSrcPortRange; |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public int maxSrcPortRange() { |
| 159 | return maxSrcPortRange; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public int minDstPortRange() { |
| 164 | return minDstPortRange; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public int maxDstPortRange() { |
| 169 | return maxDstPortRange; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public IpPrefix srcIpPrefix() { |
| 174 | return srcIpPrefix; |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public IpPrefix dstIpPrefix() { |
| 179 | return dstIpPrefix; |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public VirtualPortId srcPort() { |
| 184 | return srcPort; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public VirtualPortId dstPort() { |
| 189 | return dstPort; |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public boolean exactMatch(FlowClassifier flowClassifier) { |
| 194 | return this.equals(flowClassifier) && |
| 195 | Objects.equals(this.flowClassifierId, flowClassifier.flowClassifierId()) && |
| 196 | Objects.equals(this.tenantId, flowClassifier.tenantId()); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Sets up the global values for all the tests. |
| 202 | */ |
| 203 | @Before |
| 204 | public void setUpTest() { |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 205 | SfcCodecContext context = new SfcCodecContext(); |
| 206 | |
| 207 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 208 | .add(FlowClassifierService.class, flowClassifierService) |
| 209 | .add(CodecService.class, context.codecManager()); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame^] | 210 | setServiceDirectory(testDirectory); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 211 | |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Cleans up. |
| 216 | */ |
| 217 | @After |
| 218 | public void tearDownTest() { |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests the result of the rest api GET when there are no flow classifiers. |
| 223 | */ |
| 224 | @Test |
| 225 | public void testFlowClassifiersEmpty() { |
| 226 | |
| 227 | expect(flowClassifierService.getFlowClassifiers()).andReturn(null).anyTimes(); |
| 228 | replay(flowClassifierService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 229 | final WebTarget wt = target(); |
| 230 | final String response = wt.path("flow_classifiers").request().get(String.class); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 231 | assertThat(response, is("{\"flow_classifiers\":[]}")); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Tests the result of a rest api GET for flow classifier id. |
| 236 | */ |
| 237 | @Test |
| 238 | public void testGetFlowClassifierId() { |
| 239 | |
| 240 | final Set<FlowClassifier> flowClassifiers = new HashSet<>(); |
| 241 | flowClassifiers.add(flowClassifier1); |
| 242 | |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 243 | expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 244 | expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes(); |
| 245 | replay(flowClassifierService); |
| 246 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 247 | final WebTarget wt = target(); |
| 248 | final String response = wt.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051") |
| 249 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 250 | final JsonObject result = Json.parse(response).asObject(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 251 | assertThat(result, notNullValue()); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Tests that a fetch of a non-existent flow classifier object throws an exception. |
| 256 | */ |
| 257 | @Test |
| 258 | public void testBadGet() { |
| 259 | expect(flowClassifierService.getFlowClassifier(anyObject())) |
| 260 | .andReturn(null).anyTimes(); |
| 261 | replay(flowClassifierService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 262 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 263 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 264 | wt.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae") |
| 265 | .request().get(String.class); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 266 | fail("Fetch of non-existent flow classifier did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 267 | } catch (NotFoundException ex) { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 268 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 269 | containsString("HTTP 404 Not Found")); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Tests creating a flow classifier with POST. |
| 275 | */ |
| 276 | @Test |
| 277 | public void testPost() { |
| 278 | |
| 279 | expect(flowClassifierService.createFlowClassifier(anyObject())) |
| 280 | .andReturn(true).anyTimes(); |
| 281 | replay(flowClassifierService); |
| 282 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 283 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 284 | InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json"); |
| 285 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 286 | Response response = wt.path("flow_classifiers") |
| 287 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 288 | .post(Entity.json(jsonStream)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 289 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Tests deleting a flow classifier. |
| 294 | */ |
| 295 | @Test |
| 296 | public void testDelete() { |
| 297 | expect(flowClassifierService.removeFlowClassifier(anyObject())) |
| 298 | .andReturn(true).anyTimes(); |
| 299 | replay(flowClassifierService); |
| 300 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 301 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 302 | |
| 303 | String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051"; |
| 304 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 305 | Response deleteResponse = wt.path(location) |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 306 | .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 307 | .delete(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 308 | assertThat(deleteResponse.getStatus(), |
| 309 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 310 | } |
| 311 | } |