blob: 3cb2c83f0decac074eea329b7add9189332d19f8 [file] [log] [blame]
SureshBR47a52922015-12-01 20:51:10 +05301/*
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 */
16package org.onosproject.vtnweb.resources;
17
18import 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.core.MediaType;
36
37import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.onlab.osgi.ServiceDirectory;
41import org.onlab.osgi.TestServiceDirectory;
42import org.onlab.rest.BaseResource;
43import org.onosproject.codec.CodecService;
44import org.onosproject.vtnrsc.FlowClassifierId;
45import org.onosproject.vtnrsc.PortChain;
46import org.onosproject.vtnrsc.PortChainId;
47import org.onosproject.vtnrsc.PortPairGroupId;
48import org.onosproject.vtnrsc.TenantId;
49import org.onosproject.vtnrsc.portchain.PortChainService;
50import org.onosproject.vtnweb.web.SfcCodecContext;
51
52import com.eclipsesource.json.JsonObject;
53import com.google.common.collect.ImmutableList;
54import com.google.common.collect.Lists;
55import com.sun.jersey.api.client.ClientResponse;
56import com.sun.jersey.api.client.UniformInterfaceException;
57import com.sun.jersey.api.client.WebResource;
58
59/**
60 * Unit tests for port chain REST APIs.
61 */
62public class PortChainResourceTest extends VtnResourceTest {
63
64 final PortChainService portChainService = createMock(PortChainService.class);
65
66 PortChainId portChainId1 = PortChainId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
67 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
68 private final List<PortPairGroupId> portPairGroupList1 = Lists.newArrayList();
69 private final List<FlowClassifierId> flowClassifierList1 = Lists.newArrayList();
70
71
72 final MockPortChain portChain1 = new MockPortChain(portChainId1, tenantId1, "portChain1",
73 "Mock port chain", portPairGroupList1,
74 flowClassifierList1);
75
76 /**
77 * Mock class for a port chain.
78 */
79 private static class MockPortChain implements PortChain {
80
81 private final PortChainId portChainId;
82 private final TenantId tenantId;
83 private final String name;
84 private final String description;
85 private final List<PortPairGroupId> portPairGroupList;
86 private final List<FlowClassifierId> flowClassifierList;
87
88 public MockPortChain(PortChainId portChainId, TenantId tenantId,
89 String name, String description,
90 List<PortPairGroupId> portPairGroupList,
91 List<FlowClassifierId> flowClassifierList) {
92
93 this.portChainId = portChainId;
94 this.tenantId = tenantId;
95 this.name = name;
96 this.description = description;
97 this.portPairGroupList = portPairGroupList;
98 this.flowClassifierList = flowClassifierList;
99 }
100
101 @Override
102 public PortChainId portChainId() {
103 return portChainId;
104 }
105
106 @Override
107 public TenantId tenantId() {
108 return tenantId;
109 }
110
111 @Override
112 public String name() {
113 return name;
114 }
115
116 @Override
117 public String description() {
118 return description;
119 }
120
121 @Override
122 public List<PortPairGroupId> portPairGroups() {
123 return ImmutableList.copyOf(portPairGroupList);
124 }
125
126 @Override
127 public List<FlowClassifierId> flowClassifiers() {
128 return ImmutableList.copyOf(flowClassifierList);
129 }
130
131 @Override
132 public boolean exactMatch(PortChain portChain) {
133 return this.equals(portChain) &&
134 Objects.equals(this.portChainId, portChain.portChainId()) &&
135 Objects.equals(this.tenantId, portChain.tenantId());
136 }
137 }
138
139 /**
140 * Sets up the global values for all the tests.
141 */
142 @Before
143 public void setUpTest() {
144 SfcCodecContext context = new SfcCodecContext();
145 ServiceDirectory testDirectory = new TestServiceDirectory()
146 .add(PortChainService.class, portChainService)
147 .add(CodecService.class, context.codecManager());
148 BaseResource.setServiceDirectory(testDirectory);
149
150 }
151
152 /**
153 * Cleans up.
154 */
155 @After
156 public void tearDownTest() {
157 }
158
159 /**
160 * Tests the result of the rest api GET when there are no port chains.
161 */
162 @Test
163 public void testPortChainsEmpty() {
164
165 expect(portChainService.getPortChains()).andReturn(null).anyTimes();
166 replay(portChainService);
167 final WebResource rs = resource();
168 final String response = rs.path("port_chains").get(String.class);
169 assertThat(response, is("{\"port_chains\":[]}"));
170 }
171
172 /**
173 * Tests the result of a rest api GET for port chain id.
174 */
175 @Test
176 public void testGetPortChainId() {
177
178 final Set<PortChain> portChains = new HashSet<>();
179 portChains.add(portChain1);
180
181 expect(portChainService.exists(anyObject())).andReturn(true).anyTimes();
182 expect(portChainService.getPortChain(anyObject())).andReturn(portChain1).anyTimes();
183 replay(portChainService);
184
185 final WebResource rs = resource();
186 final String response = rs.path("port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751").get(String.class);
187 final JsonObject result = JsonObject.readFrom(response);
188 assertThat(result, notNullValue());
189 }
190
191 /**
192 * Tests that a fetch of a non-existent port chain object throws an exception.
193 */
194 @Test
195 public void testBadGet() {
196 expect(portChainService.getPortChain(anyObject()))
197 .andReturn(null).anyTimes();
198 replay(portChainService);
199 WebResource rs = resource();
200 try {
201 rs.path("port_chains/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
202 fail("Fetch of non-existent port chain did not throw an exception");
203 } catch (UniformInterfaceException ex) {
204 assertThat(ex.getMessage(),
205 containsString("returned a response status of"));
206 }
207 }
208
209 /**
210 * Tests creating a port chain with POST.
211 */
212 @Test
213 public void testPost() {
214
215 expect(portChainService.createPortChain(anyObject()))
216 .andReturn(true).anyTimes();
217 replay(portChainService);
218
219 WebResource rs = resource();
220 InputStream jsonStream = PortChainResourceTest.class.getResourceAsStream("post-PortChain.json");
221
222 ClientResponse response = rs.path("port_chains")
223 .type(MediaType.APPLICATION_JSON_TYPE)
224 .post(ClientResponse.class, jsonStream);
225 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
226 }
227
228 /**
229 * Tests deleting a port chain.
230 */
231 @Test
232 public void testDelete() {
233 expect(portChainService.removePortChain(anyObject()))
234 .andReturn(true).anyTimes();
235 replay(portChainService);
236
237 WebResource rs = resource();
238
239 String location = "port_chains/1278dcd4-459f-62ed-754b-87fc5e4a6751";
240
241 ClientResponse deleteResponse = rs.path(location)
242 .type(MediaType.APPLICATION_JSON_TYPE)
243 .delete(ClientResponse.class);
244 assertThat(deleteResponse.getStatus(),
245 is(HttpURLConnection.HTTP_NO_CONTENT));
246 }
247}