blob: 7f73b83015f9ea5f6c6683ea666d92439750a250 [file] [log] [blame]
Phaneendra Manda63d24702015-11-14 14:56:42 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Manda63d24702015-11-14 14:56:42 +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 Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonObject;
Phaneendra Manda63d24702015-11-14 14:56:42 +053020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.osgi.ServiceDirectory;
24import org.onlab.osgi.TestServiceDirectory;
25import org.onlab.rest.BaseResource;
Phaneendra Manda568734d2015-12-01 20:30:39 +053026import org.onosproject.codec.CodecService;
Phaneendra Manda63d24702015-11-14 14:56:42 +053027import org.onosproject.vtnrsc.PortPair;
28import org.onosproject.vtnrsc.PortPairId;
29import org.onosproject.vtnrsc.TenantId;
30import org.onosproject.vtnrsc.portpair.PortPairService;
Phaneendra Manda568734d2015-12-01 20:30:39 +053031import org.onosproject.vtnweb.web.SfcCodecContext;
Phaneendra Manda63d24702015-11-14 14:56:42 +053032
Jian Li9d616492016-03-09 10:52:49 -080033import javax.ws.rs.NotFoundException;
34import javax.ws.rs.client.Entity;
35import javax.ws.rs.client.WebTarget;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.io.InputStream;
39import java.net.HttpURLConnection;
40import java.util.HashSet;
41import java.util.Objects;
42import java.util.Set;
43
44import static org.easymock.EasyMock.anyObject;
45import static org.easymock.EasyMock.createMock;
46import static org.easymock.EasyMock.expect;
47import static org.easymock.EasyMock.replay;
48import static org.hamcrest.Matchers.containsString;
49import static org.hamcrest.Matchers.is;
50import static org.hamcrest.Matchers.notNullValue;
51import static org.junit.Assert.assertThat;
52import static org.junit.Assert.fail;
Phaneendra Manda63d24702015-11-14 14:56:42 +053053/**
54 * Unit tests for port pair REST APIs.
55 */
56public class PortPairResourceTest extends VtnResourceTest {
57
58 final PortPairService portPairService = createMock(PortPairService.class);
59
60 PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
61 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
62
63 final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1",
64 "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
65 "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
66
67 /**
68 * Mock class for a port pair.
69 */
70 private static class MockPortPair implements PortPair {
71
72 private final PortPairId portPairId;
73 private final TenantId tenantId;
74 private final String name;
75 private final String description;
76 private final String ingress;
77 private final String egress;
78
79 public MockPortPair(PortPairId portPairId, TenantId tenantId,
80 String name, String description,
81 String ingress, String egress) {
82
83 this.portPairId = portPairId;
84 this.tenantId = tenantId;
85 this.name = name;
86 this.description = description;
87 this.ingress = ingress;
88 this.egress = egress;
89 }
90
91 @Override
92 public PortPairId portPairId() {
93 return portPairId;
94 }
95
96 @Override
97 public TenantId tenantId() {
98 return tenantId;
99 }
100
101 @Override
102 public String name() {
103 return name;
104 }
105
106 @Override
107 public String description() {
108 return description;
109 }
110
111 @Override
112 public String ingress() {
113 return ingress;
114 }
115
116 @Override
117 public String egress() {
118 return egress;
119 }
120
121 @Override
122 public boolean exactMatch(PortPair portPair) {
123 return this.equals(portPair) &&
124 Objects.equals(this.portPairId, portPair.portPairId()) &&
125 Objects.equals(this.tenantId, portPair.tenantId());
126 }
127 }
128
129 /**
130 * Sets up the global values for all the tests.
131 */
132 @Before
133 public void setUpTest() {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530134
135 SfcCodecContext context = new SfcCodecContext();
136 ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService)
137 .add(CodecService.class, context.codecManager());
Phaneendra Manda63d24702015-11-14 14:56:42 +0530138 BaseResource.setServiceDirectory(testDirectory);
139
140 }
141
142 /**
143 * Cleans up.
144 */
145 @After
146 public void tearDownTest() {
147 }
148
149 /**
150 * Tests the result of the rest api GET when there are no port pairs.
151 */
152 @Test
153 public void testPortPairsEmpty() {
154
155 expect(portPairService.getPortPairs()).andReturn(null).anyTimes();
156 replay(portPairService);
Jian Li9d616492016-03-09 10:52:49 -0800157 final WebTarget wt = target();
158 final String response = wt.path("port_pairs").request().get(String.class);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530159 assertThat(response, is("{\"port_pairs\":[]}"));
160 }
161
162 /**
163 * Tests the result of a rest api GET for port pair id.
164 */
165 @Test
166 public void testGetPortPairId() {
167
168 final Set<PortPair> portPairs = new HashSet<>();
169 portPairs.add(portPair1);
170
171 expect(portPairService.exists(anyObject())).andReturn(true).anyTimes();
172 expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes();
173 replay(portPairService);
174
Jian Li9d616492016-03-09 10:52:49 -0800175 final WebTarget wt = target();
176 final String response = wt.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae")
177 .request().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);
Jian Li9d616492016-03-09 10:52:49 -0800190 WebTarget wt = target();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530191 try {
Jian Li9d616492016-03-09 10:52:49 -0800192 wt.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
193 .request().get(String.class);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530194 fail("Fetch of non-existent port pair did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800195 } catch (NotFoundException ex) {
Phaneendra Manda63d24702015-11-14 14:56:42 +0530196 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800197 containsString("HTTP 404 Not Found"));
Phaneendra Manda63d24702015-11-14 14:56:42 +0530198 }
199 }
200
201 /**
202 * Tests creating a port pair with POST.
203 */
204 @Test
205 public void testPost() {
206
207 expect(portPairService.createPortPair(anyObject()))
208 .andReturn(true).anyTimes();
209 replay(portPairService);
210
Jian Li9d616492016-03-09 10:52:49 -0800211 WebTarget wt = target();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530212 InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json");
213
Jian Li9d616492016-03-09 10:52:49 -0800214 Response response = wt.path("port_pairs")
215 .request(MediaType.APPLICATION_JSON_TYPE)
216 .post(Entity.json(jsonStream));
Phaneendra Manda63d24702015-11-14 14:56:42 +0530217 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
218 }
219
220 /**
221 * Tests deleting a port pair.
222 */
223 @Test
224 public void testDelete() {
225 expect(portPairService.removePortPair(anyObject()))
226 .andReturn(true).anyTimes();
227 replay(portPairService);
228
Jian Li9d616492016-03-09 10:52:49 -0800229 WebTarget wt = target();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530230
231 String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae";
232
Jian Li9d616492016-03-09 10:52:49 -0800233 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700234 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800235 .delete();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530236 assertThat(deleteResponse.getStatus(),
237 is(HttpURLConnection.HTTP_NO_CONTENT));
238 }
239}