blob: dd81814d52869cd61d7cefc2105885f6ca82e383 [file] [log] [blame]
SureshBR47a52922015-12-01 20:51:10 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
SureshBR47a52922015-12-01 20:51:10 +05303 *
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 */
16package org.onosproject.vtnweb.resources;
17
Ray Milkey094a1352018-01-22 14:03:54 -080018import com.eclipsesource.json.Json;
19import com.eclipsesource.json.JsonObject;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
SureshBR47a52922015-12-01 20:51:10 +053022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.osgi.TestServiceDirectory;
SureshBR47a52922015-12-01 20:51:10 +053027import org.onosproject.codec.CodecService;
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +053028import org.onosproject.net.DeviceId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053029import org.onosproject.vtnrsc.FiveTuple;
SureshBR47a52922015-12-01 20:51:10 +053030import org.onosproject.vtnrsc.FlowClassifierId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053031import org.onosproject.vtnrsc.LoadBalanceId;
SureshBR47a52922015-12-01 20:51:10 +053032import org.onosproject.vtnrsc.PortChain;
33import org.onosproject.vtnrsc.PortChainId;
34import org.onosproject.vtnrsc.PortPairGroupId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053035import org.onosproject.vtnrsc.PortPairId;
SureshBR47a52922015-12-01 20:51:10 +053036import org.onosproject.vtnrsc.TenantId;
37import org.onosproject.vtnrsc.portchain.PortChainService;
38import org.onosproject.vtnweb.web.SfcCodecContext;
39
Ray Milkey094a1352018-01-22 14:03:54 -080040import javax.ws.rs.NotFoundException;
41import javax.ws.rs.client.Entity;
42import javax.ws.rs.client.WebTarget;
43import javax.ws.rs.core.MediaType;
44import javax.ws.rs.core.Response;
45import java.io.InputStream;
46import java.net.HttpURLConnection;
47import java.util.HashSet;
48import java.util.List;
49import java.util.Objects;
50import java.util.Set;
51
52import static org.easymock.EasyMock.anyObject;
53import static org.easymock.EasyMock.createMock;
54import static org.easymock.EasyMock.expect;
55import static org.easymock.EasyMock.replay;
56import static org.hamcrest.Matchers.containsString;
57import static org.hamcrest.Matchers.is;
58import static org.hamcrest.Matchers.notNullValue;
59import static org.junit.Assert.assertThat;
60import static org.junit.Assert.fail;
SureshBR47a52922015-12-01 20:51:10 +053061
62/**
63 * Unit tests for port chain REST APIs.
64 */
65public 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 Manda329a1272016-02-10 22:57:00 +0530140
141 @Override
142 public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530143 }
144
145 @Override
146 public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530147 return null;
148 }
149
150 @Override
151 public Set<FiveTuple> getLoadBalanceIdMapKeys() {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530152 return null;
153 }
154
155 @Override
156 public List<PortPairId> getLoadBalancePath(LoadBalanceId id) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530157 return null;
158 }
159
160 @Override
161 public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530162 return null;
163 }
164
165 @Override
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530166 public LoadBalanceId matchPath(List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530167 return null;
168 }
Phaneendra Manda275ff0c2016-02-25 11:50:24 +0530169
170 @Override
171 public int getLoadBalancePathSize() {
172 return 0;
173 }
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +0530174
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 Manda8db7d092016-06-04 00:17:24 +0530205
206 @Override
207 public PortChain oldPortChain() {
208 return null;
209 }
SureshBR47a52922015-12-01 20:51:10 +0530210 }
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 Milkey094a1352018-01-22 14:03:54 -0800221 setServiceDirectory(testDirectory);
SureshBR47a52922015-12-01 20:51:10 +0530222
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 Li9d616492016-03-09 10:52:49 -0800240 final WebTarget wt = target();
241 final String response = wt.path("port_chains").request().get(String.class);
SureshBR47a52922015-12-01 20:51:10 +0530242 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 Li9d616492016-03-09 10:52:49 -0800258 final WebTarget wt = target();
259 final String response = wt.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751")
260 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800261 final JsonObject result = Json.parse(response).asObject();
SureshBR47a52922015-12-01 20:51:10 +0530262 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 Li9d616492016-03-09 10:52:49 -0800273 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530274 try {
Jian Li9d616492016-03-09 10:52:49 -0800275 wt.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
276 .request().get(String.class);
SureshBR47a52922015-12-01 20:51:10 +0530277 fail("Fetch of non-existent port chain did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800278 } catch (NotFoundException ex) {
SureshBR47a52922015-12-01 20:51:10 +0530279 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800280 containsString("HTTP 404 Not Found"));
SureshBR47a52922015-12-01 20:51:10 +0530281 }
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 Li9d616492016-03-09 10:52:49 -0800294 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530295 InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json");
296
Jian Li9d616492016-03-09 10:52:49 -0800297 Response response = wt.path("port_chains")
298 .request(MediaType.APPLICATION_JSON_TYPE)
299 .post(Entity.json(jsonStream));
SureshBR47a52922015-12-01 20:51:10 +0530300 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 Li9d616492016-03-09 10:52:49 -0800312 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530313
314 String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751";
315
Jian Li9d616492016-03-09 10:52:49 -0800316 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700317 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800318 .delete();
SureshBR47a52922015-12-01 20:51:10 +0530319 assertThat(deleteResponse.getStatus(),
320 is(HttpURLConnection.HTTP_NO_CONTENT));
321 }
322}