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