blob: 4fbc63f72292ee88bb3efcb9684fb0eff2aff829 [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
Phaneendra Manda8db7d092016-06-04 00:17:24 +053018import static org.easymock.EasyMock.anyObject;
19import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
21import static org.easymock.EasyMock.replay;
22import static org.hamcrest.Matchers.containsString;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.fail;
27
28import java.io.InputStream;
29import java.net.HttpURLConnection;
30import java.util.HashSet;
31import java.util.List;
32import java.util.Objects;
33import java.util.Set;
34
35import javax.ws.rs.NotFoundException;
36import javax.ws.rs.client.Entity;
37import javax.ws.rs.client.WebTarget;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40
SureshBR47a52922015-12-01 20:51:10 +053041import org.junit.After;
42import org.junit.Before;
43import org.junit.Test;
44import org.onlab.osgi.ServiceDirectory;
45import org.onlab.osgi.TestServiceDirectory;
46import org.onlab.rest.BaseResource;
47import org.onosproject.codec.CodecService;
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +053048import org.onosproject.net.DeviceId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053049import org.onosproject.vtnrsc.FiveTuple;
SureshBR47a52922015-12-01 20:51:10 +053050import org.onosproject.vtnrsc.FlowClassifierId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053051import org.onosproject.vtnrsc.LoadBalanceId;
SureshBR47a52922015-12-01 20:51:10 +053052import org.onosproject.vtnrsc.PortChain;
53import org.onosproject.vtnrsc.PortChainId;
54import org.onosproject.vtnrsc.PortPairGroupId;
Phaneendra Manda329a1272016-02-10 22:57:00 +053055import org.onosproject.vtnrsc.PortPairId;
SureshBR47a52922015-12-01 20:51:10 +053056import org.onosproject.vtnrsc.TenantId;
57import org.onosproject.vtnrsc.portchain.PortChainService;
58import org.onosproject.vtnweb.web.SfcCodecContext;
59
Phaneendra Manda8db7d092016-06-04 00:17:24 +053060import com.eclipsesource.json.Json;
61import com.eclipsesource.json.JsonObject;
62import com.google.common.collect.ImmutableList;
63import com.google.common.collect.Lists;
SureshBR47a52922015-12-01 20:51:10 +053064
65/**
66 * Unit tests for port chain REST APIs.
67 */
68public class PortChainResourceTest extends VtnResourceTest {
69
70 final PortChainService portChainService = createMock(PortChainService.class);
71
72 PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
73 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
74 private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList();
75 private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList();
76
77
78 final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1",
79 "Mock port chain", portPairGroupList1,
80 flowClassifierList1);
81
82 /**
83 * Mock class for a port chain.
84 */
85 private static class MockPortChain implements PortChain {
86
87 private final PortChainId portChainId;
88 private final TenantId tenantId;
89 private final String name;
90 private final String description;
91 private final List<PortPairGroupId> portPairGroupList;
92 private final List<FlowClassifierId> flowClassifierList;
93
94 public MockPortChain(PortChainId portChainId, TenantId tenantId,
95 String name, String description,
96 List<PortPairGroupId> portPairGroupList,
97 List<FlowClassifierId> flowClassifierList) {
98
99 this.portChainId = portChainId;
100 this.tenantId = tenantId;
101 this.name = name;
102 this.description = description;
103 this.portPairGroupList = portPairGroupList;
104 this.flowClassifierList = flowClassifierList;
105 }
106
107 @Override
108 public PortChainId portChainId() {
109 return portChainId;
110 }
111
112 @Override
113 public TenantId tenantId() {
114 return tenantId;
115 }
116
117 @Override
118 public String name() {
119 return name;
120 }
121
122 @Override
123 public String description() {
124 return description;
125 }
126
127 @Override
128 public List<PortPairGroupId> portPairGroups() {
129 return ImmutableList.copyOf(portPairGroupList);
130 }
131
132 @Override
133 public List<FlowClassifierId> flowClassifiers() {
134 return ImmutableList.copyOf(flowClassifierList);
135 }
136
137 @Override
138 public boolean exactMatch(PortChain portChain) {
139 return this.equals(portChain) &&
140 Objects.equals(this.portChainId, portChain.portChainId()) &&
141 Objects.equals(this.tenantId, portChain.tenantId());
142 }
Phaneendra Manda329a1272016-02-10 22:57:00 +0530143
144 @Override
145 public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530146 }
147
148 @Override
149 public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530150 return null;
151 }
152
153 @Override
154 public Set<FiveTuple> getLoadBalanceIdMapKeys() {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530155 return null;
156 }
157
158 @Override
159 public List<PortPairId> getLoadBalancePath(LoadBalanceId id) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530160 return null;
161 }
162
163 @Override
164 public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530165 return null;
166 }
167
168 @Override
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530169 public LoadBalanceId matchPath(List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530170 return null;
171 }
Phaneendra Manda275ff0c2016-02-25 11:50:24 +0530172
173 @Override
174 public int getLoadBalancePathSize() {
175 return 0;
176 }
Phaneendra Mandaab7fdfa2016-04-19 01:24:09 +0530177
178 @Override
179 public void addSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) {
180 }
181
182 @Override
183 public void addSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) {
184 }
185
186 @Override
187 public void removeSfcClassifiers(LoadBalanceId id, List<DeviceId> classifierList) {
188 }
189
190 @Override
191 public void removeSfcForwarders(LoadBalanceId id, List<DeviceId> forwarderList) {
192 }
193
194 @Override
195 public List<DeviceId> getSfcClassifiers(LoadBalanceId id) {
196 return null;
197 }
198
199 @Override
200 public List<DeviceId> getSfcForwarders(LoadBalanceId id) {
201 return null;
202 }
203
204 @Override
205 public Set<LoadBalanceId> getLoadBalancePathMapKeys() {
206 return null;
207 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530208
209 @Override
210 public PortChain oldPortChain() {
211 return null;
212 }
SureshBR47a52922015-12-01 20:51:10 +0530213 }
214
215 /**
216 * Sets up the global values for all the tests.
217 */
218 @Before
219 public void setUpTest() {
220 SfcCodecContext context = new SfcCodecContext();
221 ServiceDirectory testDirectory = new TestServiceDirectory()
222 .add(PortChainService.class, portChainService)
223 .add(CodecService.class, context.codecManager());
224 BaseResource.setServiceDirectory(testDirectory);
225
226 }
227
228 /**
229 * Cleans up.
230 */
231 @After
232 public void tearDownTest() {
233 }
234
235 /**
236 * Tests the result of the rest api GET when there are no port chains.
237 */
238 @Test
239 public void testPortChainsEmpty() {
240
241 expect(portChainService.getPortChains()).andReturn(null).anyTimes();
242 replay(portChainService);
Jian Li9d616492016-03-09 10:52:49 -0800243 final WebTarget wt = target();
244 final String response = wt.path("port_chains").request().get(String.class);
SureshBR47a52922015-12-01 20:51:10 +0530245 assertThat(response, is("{\"port_chains\":[]}"));
246 }
247
248 /**
249 * Tests the result of a rest api GET for port chain id.
250 */
251 @Test
252 public void testGetPortChainId() {
253
254 final Set<PortChain> portChains = new HashSet<>();
255 portChains.add(portChain1);
256
257 expect(portChainService.exists(anyObject())).andReturn(true).anyTimes();
258 expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes();
259 replay(portChainService);
260
Jian Li9d616492016-03-09 10:52:49 -0800261 final WebTarget wt = target();
262 final String response = wt.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751")
263 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800264 final JsonObject result = Json.parse(response).asObject();
SureshBR47a52922015-12-01 20:51:10 +0530265 assertThat(result, notNullValue());
266 }
267
268 /**
269 * Tests that a fetch of a non-existent port chain object throws an exception.
270 */
271 @Test
272 public void testBadGet() {
273 expect(portChainService.getPortChain(anyObject()))
274 .andReturn(null).anyTimes();
275 replay(portChainService);
Jian Li9d616492016-03-09 10:52:49 -0800276 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530277 try {
Jian Li9d616492016-03-09 10:52:49 -0800278 wt.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
279 .request().get(String.class);
SureshBR47a52922015-12-01 20:51:10 +0530280 fail("Fetch of non-existent port chain did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800281 } catch (NotFoundException ex) {
SureshBR47a52922015-12-01 20:51:10 +0530282 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800283 containsString("HTTP 404 Not Found"));
SureshBR47a52922015-12-01 20:51:10 +0530284 }
285 }
286
287 /**
288 * Tests creating a port chain with POST.
289 */
290 @Test
291 public void testPost() {
292
293 expect(portChainService.createPortChain(anyObject()))
294 .andReturn(true).anyTimes();
295 replay(portChainService);
296
Jian Li9d616492016-03-09 10:52:49 -0800297 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530298 InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json");
299
Jian Li9d616492016-03-09 10:52:49 -0800300 Response response = wt.path("port_chains")
301 .request(MediaType.APPLICATION_JSON_TYPE)
302 .post(Entity.json(jsonStream));
SureshBR47a52922015-12-01 20:51:10 +0530303 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
304 }
305
306 /**
307 * Tests deleting a port chain.
308 */
309 @Test
310 public void testDelete() {
311 expect(portChainService.removePortChain(anyObject()))
312 .andReturn(true).anyTimes();
313 replay(portChainService);
314
Jian Li9d616492016-03-09 10:52:49 -0800315 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530316
317 String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751";
318
Jian Li9d616492016-03-09 10:52:49 -0800319 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700320 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800321 .delete();
SureshBR47a52922015-12-01 20:51:10 +0530322 assertThat(deleteResponse.getStatus(),
323 is(HttpURLConnection.HTTP_NO_CONTENT));
324 }
325}