Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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; |
| 26 | import org.onlab.rest.BaseResource; |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 27 | import org.onosproject.codec.CodecService; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 28 | import org.onosproject.vtnrsc.FlowClassifier; |
| 29 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 30 | import org.onosproject.vtnrsc.TenantId; |
| 31 | import org.onosproject.vtnrsc.VirtualPortId; |
| 32 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
Phaneendra Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 33 | import org.onosproject.vtnweb.web.SfcCodecContext; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 34 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 35 | import javax.ws.rs.NotFoundException; |
| 36 | import javax.ws.rs.client.Entity; |
| 37 | import javax.ws.rs.client.WebTarget; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import java.io.InputStream; |
| 41 | import java.net.HttpURLConnection; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.Objects; |
| 44 | import java.util.Set; |
| 45 | |
| 46 | import static org.easymock.EasyMock.anyObject; |
| 47 | import static org.easymock.EasyMock.createMock; |
| 48 | import static org.easymock.EasyMock.expect; |
| 49 | import static org.easymock.EasyMock.replay; |
| 50 | import static org.hamcrest.Matchers.containsString; |
| 51 | import static org.hamcrest.Matchers.is; |
| 52 | import static org.hamcrest.Matchers.notNullValue; |
| 53 | import static org.junit.Assert.assertThat; |
| 54 | import static org.junit.Assert.fail; |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 55 | /** |
| 56 | * Unit tests for flow classifier REST APIs. |
| 57 | */ |
| 58 | public 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 Manda | d66dca4 | 2015-12-01 20:24:10 +0530 | [diff] [blame] | 197 | SfcCodecContext context = new SfcCodecContext(); |
| 198 | |
| 199 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 200 | .add(FlowClassifierService.class, flowClassifierService) |
| 201 | .add(CodecService.class, context.codecManager()); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 202 | 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); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 221 | final WebTarget wt = target(); |
| 222 | final String response = wt.path("flow_classifiers").request().get(String.class); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 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 S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 235 | expect(flowClassifierService.exists(anyObject())).andReturn(true).anyTimes(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 236 | expect(flowClassifierService.getFlowClassifier(anyObject())).andReturn(flowClassifier1).anyTimes(); |
| 237 | replay(flowClassifierService); |
| 238 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 239 | final WebTarget wt = target(); |
| 240 | final String response = wt.path("flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051") |
| 241 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 242 | final JsonObject result = Json.parse(response).asObject(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 243 | 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); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 254 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 255 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 256 | wt.path("flow_classifiers/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae") |
| 257 | .request().get(String.class); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 258 | fail("Fetch of non-existent flow classifier did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 259 | } catch (NotFoundException ex) { |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 260 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 261 | containsString("HTTP 404 Not Found")); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Tests creating a flow classifier with POST. |
| 267 | */ |
| 268 | @Test |
| 269 | public void testPost() { |
| 270 | |
| 271 | expect(flowClassifierService.createFlowClassifier(anyObject())) |
| 272 | .andReturn(true).anyTimes(); |
| 273 | replay(flowClassifierService); |
| 274 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 275 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 276 | InputStream jsonStream = FlowClassifierResourceTest.class.getResourceAsStream("post-FlowClassifier.json"); |
| 277 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 278 | Response response = wt.path("flow_classifiers") |
| 279 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 280 | .post(Entity.json(jsonStream)); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 281 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Tests deleting a flow classifier. |
| 286 | */ |
| 287 | @Test |
| 288 | public void testDelete() { |
| 289 | expect(flowClassifierService.removeFlowClassifier(anyObject())) |
| 290 | .andReturn(true).anyTimes(); |
| 291 | replay(flowClassifierService); |
| 292 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 293 | WebTarget wt = target(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 294 | |
| 295 | String location = "flow_classifiers/4a334cd4-fe9c-4fae-af4b-321c5e2eb051"; |
| 296 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 297 | Response deleteResponse = wt.path(location) |
| 298 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 299 | .delete(); |
Bharat saraswal | f38e226 | 2015-11-18 22:57:05 +0530 | [diff] [blame] | 300 | assertThat(deleteResponse.getStatus(), |
| 301 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 302 | } |
| 303 | } |