2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.vtnweb.resources;
18 import static org.easymock.EasyMock.anyObject;
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.hamcrest.Matchers.containsString;
23 import static org.hamcrest.Matchers.is;
24 import static org.hamcrest.Matchers.notNullValue;
25 import static org.junit.Assert.assertThat;
26 import static org.junit.Assert.fail;
28 import java.io.InputStream;
29 import java.net.HttpURLConnection;
30 import java.util.HashSet;
31 import java.util.Objects;
34 import javax.ws.rs.core.MediaType;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onlab.osgi.ServiceDirectory;
40 import org.onlab.osgi.TestServiceDirectory;
41 import org.onlab.rest.BaseResource;
42 import org.onosproject.vtnrsc.PortPair;
43 import org.onosproject.vtnrsc.PortPairId;
44 import org.onosproject.vtnrsc.TenantId;
45 import org.onosproject.vtnrsc.portpair.PortPairService;
47 import com.eclipsesource.json.JsonObject;
48 import com.sun.jersey.api.client.ClientResponse;
49 import com.sun.jersey.api.client.UniformInterfaceException;
50 import com.sun.jersey.api.client.WebResource;
52 * Unit tests for port pair REST APIs.
54 public class PortPairResourceTest extends VtnResourceTest {
56 final PortPairService portPairService = createMock(PortPairService.class);
58 PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
59 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
61 final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1",
62 "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
63 "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
66 * Mock class for a port pair.
68 private static class MockPortPair implements PortPair {
70 private final PortPairId portPairId;
71 private final TenantId tenantId;
72 private final String name;
73 private final String description;
74 private final String ingress;
75 private final String egress;
77 public MockPortPair(PortPairId portPairId, TenantId tenantId,
78 String name, String description,
79 String ingress, String egress) {
81 this.portPairId = portPairId;
82 this.tenantId = tenantId;
84 this.description = description;
85 this.ingress = ingress;
90 public PortPairId portPairId() {
95 public TenantId tenantId() {
100 public String name() {
105 public String description() {
110 public String ingress() {
115 public String egress() {
120 public boolean exactMatch(PortPair portPair) {
121 return this.equals(portPair) &&
122 Objects.equals(this.portPairId, portPair.portPairId()) &&
123 Objects.equals(this.tenantId, portPair.tenantId());
128 * Sets up the global values for all the tests.
131 public void setUpTest() {
132 ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService);
133 BaseResource.setServiceDirectory(testDirectory);
141 public void tearDownTest() {
145 * Tests the result of the rest api GET when there are no port pairs.
148 public void testPortPairsEmpty() {
150 expect(portPairService.getPortPairs()).andReturn(null).anyTimes();
151 replay(portPairService);
152 final WebResource rs = resource();
153 final String response = rs.path("port_pairs").get(String.class);
154 assertThat(response, is("{\"port_pairs\":[]}"));
158 * Tests the result of a rest api GET for port pair id.
161 public void testGetPortPairId() {
163 final Set<PortPair> portPairs = new HashSet<>();
164 portPairs.add(portPair1);
166 expect(portPairService.exists(anyObject())).andReturn(true).anyTimes();
167 expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes();
168 replay(portPairService);
170 final WebResource rs = resource();
171 final String response = rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae").get(String.class);
172 final JsonObject result = JsonObject.readFrom(response);
173 assertThat(result, notNullValue());
177 * Tests that a fetch of a non-existent port pair object throws an exception.
180 public void testBadGet() {
181 expect(portPairService.getPortPair(anyObject()))
182 .andReturn(null).anyTimes();
183 replay(portPairService);
184 WebResource rs = resource();
186 rs.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
187 fail("Fetch of non-existent port pair did not throw an exception");
188 } catch (UniformInterfaceException ex) {
189 assertThat(ex.getMessage(),
190 containsString("returned a response status of"));
195 * Tests creating a port pair with POST.
198 public void testPost() {
200 expect(portPairService.createPortPair(anyObject()))
201 .andReturn(true).anyTimes();
202 replay(portPairService);
204 WebResource rs = resource();
205 InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json");
207 ClientResponse response = rs.path("port_pairs")
208 .type(MediaType.APPLICATION_JSON_TYPE)
209 .post(ClientResponse.class, jsonStream);
210 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
214 * Tests deleting a port pair.
217 public void testDelete() {
218 expect(portPairService.removePortPair(anyObject()))
219 .andReturn(true).anyTimes();
220 replay(portPairService);
222 WebResource rs = resource();
224 String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae";
226 ClientResponse deleteResponse = rs.path(location)
227 .type(MediaType.APPLICATION_JSON_TYPE)
228 .delete(ClientResponse.class);
229 assertThat(deleteResponse.getStatus(),
230 is(HttpURLConnection.HTTP_NO_CONTENT));