SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +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 | |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 18 | import com.eclipsesource.json.Json; |
| 19 | import com.eclipsesource.json.JsonObject; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Lists; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.osgi.ServiceDirectory; |
| 26 | import org.onlab.osgi.TestServiceDirectory; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 27 | import org.onosproject.codec.CodecService; |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 29 | import org.onosproject.vtnrsc.FiveTuple; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 30 | import org.onosproject.vtnrsc.FlowClassifierId; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 31 | import org.onosproject.vtnrsc.LoadBalanceId; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 32 | import org.onosproject.vtnrsc.PortChain; |
| 33 | import org.onosproject.vtnrsc.PortChainId; |
| 34 | import org.onosproject.vtnrsc.PortPairGroupId; |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 35 | import org.onosproject.vtnrsc.PortPairId; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 36 | import org.onosproject.vtnrsc.TenantId; |
| 37 | import org.onosproject.vtnrsc.portchain.PortChainService; |
| 38 | import org.onosproject.vtnweb.web.SfcCodecContext; |
| 39 | |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 40 | import javax.ws.rs.NotFoundException; |
| 41 | import javax.ws.rs.client.Entity; |
| 42 | import javax.ws.rs.client.WebTarget; |
| 43 | import javax.ws.rs.core.MediaType; |
| 44 | import javax.ws.rs.core.Response; |
| 45 | import java.io.InputStream; |
| 46 | import java.net.HttpURLConnection; |
| 47 | import java.util.HashSet; |
| 48 | import java.util.List; |
| 49 | import java.util.Objects; |
| 50 | import java.util.Set; |
| 51 | |
| 52 | import static org.easymock.EasyMock.anyObject; |
| 53 | import static org.easymock.EasyMock.createMock; |
| 54 | import static org.easymock.EasyMock.expect; |
| 55 | import static org.easymock.EasyMock.replay; |
| 56 | import static org.hamcrest.Matchers.containsString; |
| 57 | import static org.hamcrest.Matchers.is; |
| 58 | import static org.hamcrest.Matchers.notNullValue; |
| 59 | import static org.junit.Assert.assertThat; |
| 60 | import static org.junit.Assert.fail; |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Unit tests for port chain REST APIs. |
| 64 | */ |
| 65 | public class PortChainResourceTest extends VtnResourceTest { |
| 66 | |
| 67 | final PortChainService portChainService = createMock(PortChainService.class); |
| 68 | |
| 69 | PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); |
| 70 | TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5"); |
| 71 | private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList(); |
| 72 | private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList(); |
| 73 | |
| 74 | |
| 75 | final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1", |
| 76 | "Mock port chain", portPairGroupList1, |
| 77 | flowClassifierList1); |
| 78 | |
| 79 | /** |
| 80 | * Mock class for a port chain. |
| 81 | */ |
| 82 | private static class MockPortChain implements PortChain { |
| 83 | |
| 84 | private final PortChainId portChainId; |
| 85 | private final TenantId tenantId; |
| 86 | private final String name; |
| 87 | private final String description; |
| 88 | private final List<PortPairGroupId> portPairGroupList; |
| 89 | private final List<FlowClassifierId> flowClassifierList; |
| 90 | |
| 91 | public MockPortChain(PortChainId portChainId, TenantId tenantId, |
| 92 | String name, String description, |
| 93 | List<PortPairGroupId> portPairGroupList, |
| 94 | List<FlowClassifierId> flowClassifierList) { |
| 95 | |
| 96 | this.portChainId = portChainId; |
| 97 | this.tenantId = tenantId; |
| 98 | this.name = name; |
| 99 | this.description = description; |
| 100 | this.portPairGroupList = portPairGroupList; |
| 101 | this.flowClassifierList = flowClassifierList; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public PortChainId portChainId() { |
| 106 | return portChainId; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public TenantId tenantId() { |
| 111 | return tenantId; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String name() { |
| 116 | return name; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String description() { |
| 121 | return description; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public List<PortPairGroupId> portPairGroups() { |
| 126 | return ImmutableList.copyOf(portPairGroupList); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public List<FlowClassifierId> flowClassifiers() { |
| 131 | return ImmutableList.copyOf(flowClassifierList); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean exactMatch(PortChain portChain) { |
| 136 | return this.equals(portChain) && |
| 137 | Objects.equals(this.portChainId, portChain.portChainId()) && |
| 138 | Objects.equals(this.tenantId, portChain.tenantId()); |
| 139 | } |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 140 | |
| 141 | @Override |
| 142 | public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, List<PortPairId> path) { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 147 | return null; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Set<FiveTuple> getLoadBalanceIdMapKeys() { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 152 | return null; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public List<PortPairId> getLoadBalancePath(LoadBalanceId id) { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 157 | return null; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 162 | return null; |
| 163 | } |
| 164 | |
| 165 | @Override |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 166 | public LoadBalanceId matchPath(List<PortPairId> path) { |
Phaneendra Manda | 329a127 | 2016-02-10 22:57:00 +0530 | [diff] [blame] | 167 | return null; |
| 168 | } |
Phaneendra Manda | 275ff0c | 2016-02-25 11:50:24 +0530 | [diff] [blame] | 169 | |
| 170 | @Override |
| 171 | public int getLoadBalancePathSize() { |
| 172 | return 0; |
| 173 | } |
Phaneendra Manda | ab7fdfa | 2016-04-19 01:24:09 +0530 | [diff] [blame] | 174 | |
| 175 | @Override |
| 176 | public void addSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) { |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public void addSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) { |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void removeSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) { |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void removeSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) { |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public List<DeviceId> getSfcClassifiers(LoadBalanceId id) { |
| 193 | return null; |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public List<DeviceId> getSfcForwarders(LoadBalanceId id) { |
| 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Set<LoadBalanceId> getLoadBalancePathMapKeys() { |
| 203 | return null; |
| 204 | } |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 205 | |
| 206 | @Override |
| 207 | public PortChain oldPortChain() { |
| 208 | return null; |
| 209 | } |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Sets up the global values for all the tests. |
| 214 | */ |
| 215 | @Before |
| 216 | public void setUpTest() { |
| 217 | SfcCodecContext context = new SfcCodecContext(); |
| 218 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 219 | .add(PortChainService.class, portChainService) |
| 220 | .add(CodecService.class, context.codecManager()); |
Ray Milkey | 094a135 | 2018-01-22 14:03:54 -0800 | [diff] [blame] | 221 | setServiceDirectory(testDirectory); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 222 | |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Cleans up. |
| 227 | */ |
| 228 | @After |
| 229 | public void tearDownTest() { |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Tests the result of the rest api GET when there are no port chains. |
| 234 | */ |
| 235 | @Test |
| 236 | public void testPortChainsEmpty() { |
| 237 | |
| 238 | expect(portChainService.getPortChains()).andReturn(null).anyTimes(); |
| 239 | replay(portChainService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 240 | final WebTarget wt = target(); |
| 241 | final String response = wt.path("port_chains").request().get(String.class); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 242 | assertThat(response, is("{\"port_chains\":[]}")); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Tests the result of a rest api GET for port chain id. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testGetPortChainId() { |
| 250 | |
| 251 | final Set<PortChain> portChains = new HashSet<>(); |
| 252 | portChains.add(portChain1); |
| 253 | |
| 254 | expect(portChainService.exists(anyObject())).andReturn(true).anyTimes(); |
| 255 | expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes(); |
| 256 | replay(portChainService); |
| 257 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 258 | final WebTarget wt = target(); |
| 259 | final String response = wt.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751") |
| 260 | .request().get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 261 | final JsonObject result = Json.parse(response).asObject(); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 262 | assertThat(result, notNullValue()); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Tests that a fetch of a non-existent port chain object throws an exception. |
| 267 | */ |
| 268 | @Test |
| 269 | public void testBadGet() { |
| 270 | expect(portChainService.getPortChain(anyObject())) |
| 271 | .andReturn(null).anyTimes(); |
| 272 | replay(portChainService); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 273 | WebTarget wt = target(); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 274 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 275 | wt.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae") |
| 276 | .request().get(String.class); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 277 | fail("Fetch of non-existent port chain did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 278 | } catch (NotFoundException ex) { |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 279 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 280 | containsString("HTTP 404 Not Found")); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Tests creating a port chain with POST. |
| 286 | */ |
| 287 | @Test |
| 288 | public void testPost() { |
| 289 | |
| 290 | expect(portChainService.createPortChain(anyObject())) |
| 291 | .andReturn(true).anyTimes(); |
| 292 | replay(portChainService); |
| 293 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 294 | WebTarget wt = target(); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 295 | InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json"); |
| 296 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 297 | Response response = wt.path("port_chains") |
| 298 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 299 | .post(Entity.json(jsonStream)); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 300 | assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK)); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Tests deleting a port chain. |
| 305 | */ |
| 306 | @Test |
| 307 | public void testDelete() { |
| 308 | expect(portChainService.removePortChain(anyObject())) |
| 309 | .andReturn(true).anyTimes(); |
| 310 | replay(portChainService); |
| 311 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 312 | WebTarget wt = target(); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 313 | |
| 314 | String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751"; |
| 315 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 316 | Response deleteResponse = wt.path(location) |
Ray Milkey | 7c25182 | 2016-04-06 17:38:25 -0700 | [diff] [blame] | 317 | .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE) |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 318 | .delete(); |
SureshBR | 47a5292 | 2015-12-01 20:51:10 +0530 | [diff] [blame] | 319 | assertThat(deleteResponse.getStatus(), |
| 320 | is(HttpURLConnection.HTTP_NO_CONTENT)); |
| 321 | } |
| 322 | } |