blob: c4b02ed4996ce4326af3f16acf86dbd3334f179e [file] [log] [blame]
Phaneendra Manda63d24702015-11-14 14:56:42 +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.Objects;
32import java.util.Set;
33
34import javax.ws.rs.core.MediaType;
35
Jian Li80cfe452016-01-14 16:04:58 -080036import com.eclipsesource.json.Json;
Phaneendra Manda63d24702015-11-14 14:56:42 +053037import 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;
Phaneendra Manda568734d2015-12-01 20:30:39 +053043import org.onosproject.codec.CodecService;
Phaneendra Manda63d24702015-11-14 14:56:42 +053044import org.onosproject.vtnrsc.PortPair;
45import org.onosproject.vtnrsc.PortPairId;
46import org.onosproject.vtnrsc.TenantId;
47import org.onosproject.vtnrsc.portpair.PortPairService;
Phaneendra Manda568734d2015-12-01 20:30:39 +053048import org.onosproject.vtnweb.web.SfcCodecContext;
Phaneendra Manda63d24702015-11-14 14:56:42 +053049
50import com.eclipsesource.json.JsonObject;
51import com.sun.jersey.api.client.ClientResponse;
52import com.sun.jersey.api.client.UniformInterfaceException;
53import com.sun.jersey.api.client.WebResource;
54/**
55 * Unit tests for port pair REST APIs.
56 */
57public class PortPairResourceTest extends VtnResourceTest {
58
59 final PortPairService portPairService = createMock(PortPairService.class);
60
61 PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
62 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
63
64 final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1",
65 "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
66 "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
67
68 /**
69 * Mock class for a port pair.
70 */
71 private static class MockPortPair implements PortPair {
72
73 private final PortPairId portPairId;
74 private final TenantId tenantId;
75 private final String name;
76 private final String description;
77 private final String ingress;
78 private final String egress;
79
80 public MockPortPair(PortPairId portPairId, TenantId tenantId,
81 String name, String description,
82 String ingress, String egress) {
83
84 this.portPairId = portPairId;
85 this.tenantId = tenantId;
86 this.name = name;
87 this.description = description;
88 this.ingress = ingress;
89 this.egress = egress;
90 }
91
92 @Override
93 public PortPairId portPairId() {
94 return portPairId;
95 }
96
97 @Override
98 public TenantId tenantId() {
99 return tenantId;
100 }
101
102 @Override
103 public String name() {
104 return name;
105 }
106
107 @Override
108 public String description() {
109 return description;
110 }
111
112 @Override
113 public String ingress() {
114 return ingress;
115 }
116
117 @Override
118 public String egress() {
119 return egress;
120 }
121
122 @Override
123 public boolean exactMatch(PortPair portPair) {
124 return this.equals(portPair) &&
125 Objects.equals(this.portPairId, portPair.portPairId()) &&
126 Objects.equals(this.tenantId, portPair.tenantId());
127 }
128 }
129
130 /**
131 * Sets up the global values for all the tests.
132 */
133 @Before
134 public void setUpTest() {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530135
136 SfcCodecContext context = new SfcCodecContext();
137 ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService)
138 .add(CodecService.class, context.codecManager());
Phaneendra Manda63d24702015-11-14 14:56:42 +0530139 BaseResource.setServiceDirectory(testDirectory);
140
141 }
142
143 /**
144 * Cleans up.
145 */
146 @After
147 public void tearDownTest() {
148 }
149
150 /**
151 * Tests the result of the rest api GET when there are no port pairs.
152 */
153 @Test
154 public void testPortPairsEmpty() {
155
156 expect(portPairService.getPortPairs()).andReturn(null).anyTimes();
157 replay(portPairService);
158 final WebResource rs = resource();
159 final String response = rs.path("port_pairs").get(String.class);
160 assertThat(response, is("{\"port_pairs\":[]}"));
161 }
162
163 /**
164 * Tests the result of a rest api GET for port pair id.
165 */
166 @Test
167 public void testGetPortPairId() {
168
169 final Set<PortPair> portPairs = new HashSet<>();
170 portPairs.add(portPair1);
171
172 expect(portPairService.exists(anyObject())).andReturn(true).anyTimes();
173 expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes();
174 replay(portPairService);
175
176 final WebResource rs = resource();
177 final String response = rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800178 final JsonObject result = Json.parse(response).asObject();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530179 assertThat(result, notNullValue());
180 }
181
182 /**
183 * Tests that a fetch of a non-existent port pair object throws an exception.
184 */
185 @Test
186 public void testBadGet() {
187 expect(portPairService.getPortPair(anyObject()))
188 .andReturn(null).anyTimes();
189 replay(portPairService);
190 WebResource rs = resource();
191 try {
192 rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
193 fail("Fetch of non-existent port pair did not throw an exception");
194 } catch (UniformInterfaceException ex) {
195 assertThat(ex.getMessage(),
196 containsString("returned a response status of"));
197 }
198 }
199
200 /**
201 * Tests creating a port pair with POST.
202 */
203 @Test
204 public void testPost() {
205
206 expect(portPairService.createPortPair(anyObject()))
207 .andReturn(true).anyTimes();
208 replay(portPairService);
209
210 WebResource rs = resource();
211 InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json");
212
213 ClientResponse response = rs.path("port_pairs")
214 .type(MediaType.APPLICATION_JSON_TYPE)
215 .post(ClientResponse.class, jsonStream);
216 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
217 }
218
219 /**
220 * Tests deleting a port pair.
221 */
222 @Test
223 public void testDelete() {
224 expect(portPairService.removePortPair(anyObject()))
225 .andReturn(true).anyTimes();
226 replay(portPairService);
227
228 WebResource rs = resource();
229
230 String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae";
231
232 ClientResponse deleteResponse = rs.path(location)
233 .type(MediaType.APPLICATION_JSON_TYPE)
234 .delete(ClientResponse.class);
235 assertThat(deleteResponse.getStatus(),
236 is(HttpURLConnection.HTTP_NO_CONTENT));
237 }
238}