blob: 41ce59785d7155b6beee0fd814e2471dad5beb6c [file] [log] [blame]
SureshBR47a52922015-12-01 20:51:10 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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
Jian Li9d616492016-03-09 10:52:49 -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;
27import org.onlab.rest.BaseResource;
28import org.onosproject.codec.CodecService;
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
Jian Li9d616492016-03-09 10:52:49 -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.Optional;
51import java.util.Set;
52
53import static org.easymock.EasyMock.anyObject;
54import static org.easymock.EasyMock.createMock;
55import static org.easymock.EasyMock.expect;
56import static org.easymock.EasyMock.replay;
57import static org.hamcrest.Matchers.containsString;
58import static org.hamcrest.Matchers.is;
59import static org.hamcrest.Matchers.notNullValue;
60import static org.junit.Assert.assertThat;
61import static org.junit.Assert.fail;
SureshBR47a52922015-12-01 20:51:10 +053062
63/**
64 * Unit tests for port chain REST APIs.
65 */
66public 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 Manda329a1272016-02-10 22:57:00 +0530141
142 @Override
143 public void addLoadBalancePath(FiveTuple fiveTuple, LoadBalanceId id, List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530144 }
145
146 @Override
147 public LoadBalanceId getLoadBalanceId(FiveTuple fiveTuple) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530148 return null;
149 }
150
151 @Override
152 public Set<FiveTuple> getLoadBalanceIdMapKeys() {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530153 return null;
154 }
155
156 @Override
157 public List<PortPairId> getLoadBalancePath(LoadBalanceId id) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530158 return null;
159 }
160
161 @Override
162 public List<PortPairId> getLoadBalancePath(FiveTuple fiveTuple) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530163 return null;
164 }
165
166 @Override
167 public Optional<LoadBalanceId> matchPath(List<PortPairId> path) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530168 return null;
169 }
Phaneendra Manda275ff0c2016-02-25 11:50:24 +0530170
171 @Override
172 public int getLoadBalancePathSize() {
173 return 0;
174 }
SureshBR47a52922015-12-01 20:51:10 +0530175 }
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 Li9d616492016-03-09 10:52:49 -0800205 final WebTarget wt = target();
206 final String response = wt.path("port_chains").request().get(String.class);
SureshBR47a52922015-12-01 20:51:10 +0530207 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 Li9d616492016-03-09 10:52:49 -0800223 final WebTarget wt = target();
224 final String response = wt.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751")
225 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800226 final JsonObject result = Json.parse(response).asObject();
SureshBR47a52922015-12-01 20:51:10 +0530227 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 Li9d616492016-03-09 10:52:49 -0800238 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530239 try {
Jian Li9d616492016-03-09 10:52:49 -0800240 wt.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
241 .request().get(String.class);
SureshBR47a52922015-12-01 20:51:10 +0530242 fail("Fetch of non-existent port chain did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800243 } catch (NotFoundException ex) {
SureshBR47a52922015-12-01 20:51:10 +0530244 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800245 containsString("HTTP 404 Not Found"));
SureshBR47a52922015-12-01 20:51:10 +0530246 }
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 Li9d616492016-03-09 10:52:49 -0800259 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530260 InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json");
261
Jian Li9d616492016-03-09 10:52:49 -0800262 Response response = wt.path("port_chains")
263 .request(MediaType.APPLICATION_JSON_TYPE)
264 .post(Entity.json(jsonStream));
SureshBR47a52922015-12-01 20:51:10 +0530265 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 Li9d616492016-03-09 10:52:49 -0800277 WebTarget wt = target();
SureshBR47a52922015-12-01 20:51:10 +0530278
279 String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751";
280
Jian Li9d616492016-03-09 10:52:49 -0800281 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700282 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800283 .delete();
SureshBR47a52922015-12-01 20:51:10 +0530284 assertThat(deleteResponse.getStatus(),
285 is(HttpURLConnection.HTTP_NO_CONTENT));
286 }
287}