blob: 63e89b9cd1738318559160c65517c63b31b555a1 [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;
Phaneendra Manda568734d2015-12-01 20:30:39 +053025import org.onosproject.codec.CodecService;
Phaneendra Manda63d24702015-11-14 14:56:42 +053026import org.onosproject.vtnrsc.PortPair;
27import org.onosproject.vtnrsc.PortPairId;
28import org.onosproject.vtnrsc.TenantId;
29import org.onosproject.vtnrsc.portpair.PortPairService;
Phaneendra Manda568734d2015-12-01 20:30:39 +053030import org.onosproject.vtnweb.web.SfcCodecContext;
Phaneendra Manda63d24702015-11-14 14:56:42 +053031
Jian Li9d616492016-03-09 10:52:49 -080032import javax.ws.rs.NotFoundException;
33import javax.ws.rs.client.Entity;
34import javax.ws.rs.client.WebTarget;
35import javax.ws.rs.core.MediaType;
36import javax.ws.rs.core.Response;
37import java.io.InputStream;
38import java.net.HttpURLConnection;
39import java.util.HashSet;
40import java.util.Objects;
41import java.util.Set;
42
43import static org.easymock.EasyMock.anyObject;
44import static org.easymock.EasyMock.createMock;
45import static org.easymock.EasyMock.expect;
46import static org.easymock.EasyMock.replay;
47import static org.hamcrest.Matchers.containsString;
48import static org.hamcrest.Matchers.is;
49import static org.hamcrest.Matchers.notNullValue;
50import static org.junit.Assert.assertThat;
51import static org.junit.Assert.fail;
Phaneendra Manda63d24702015-11-14 14:56:42 +053052/**
53 * Unit tests for port pair REST APIs.
54 */
55public class PortPairResourceTest extends VtnResourceTest {
56
57 final PortPairService portPairService = createMock(PortPairService.class);
58
59 PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae");
60 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
61
62 final MockPortPair portPair1 = new MockPortPair(portPairId1, tenantId1, "portPair1",
63 "Mock port pair", "dace4513-24fc-4fae-af4b-321c5e2eb3d1",
64 "aef3478a-4a56-2a6e-cd3a-9dee4e2ec345");
65
66 /**
67 * Mock class for a port pair.
68 */
69 private static class MockPortPair implements PortPair {
70
71 private final PortPairId portPairId;
72 private final TenantId tenantId;
73 private final String name;
74 private final String description;
75 private final String ingress;
76 private final String egress;
77
78 public MockPortPair(PortPairId portPairId, TenantId tenantId,
79 String name, String description,
80 String ingress, String egress) {
81
82 this.portPairId = portPairId;
83 this.tenantId = tenantId;
84 this.name = name;
85 this.description = description;
86 this.ingress = ingress;
87 this.egress = egress;
88 }
89
90 @Override
91 public PortPairId portPairId() {
92 return portPairId;
93 }
94
95 @Override
96 public TenantId tenantId() {
97 return tenantId;
98 }
99
100 @Override
101 public String name() {
102 return name;
103 }
104
105 @Override
106 public String description() {
107 return description;
108 }
109
110 @Override
111 public String ingress() {
112 return ingress;
113 }
114
115 @Override
116 public String egress() {
117 return egress;
118 }
119
120 @Override
121 public boolean exactMatch(PortPair portPair) {
122 return this.equals(portPair) &&
123 Objects.equals(this.portPairId, portPair.portPairId()) &&
124 Objects.equals(this.tenantId, portPair.tenantId());
125 }
126 }
127
128 /**
129 * Sets up the global values for all the tests.
130 */
131 @Before
132 public void setUpTest() {
Phaneendra Manda568734d2015-12-01 20:30:39 +0530133
134 SfcCodecContext context = new SfcCodecContext();
135 ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService)
136 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -0800137 setServiceDirectory(testDirectory);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530138
139 }
140
141 /**
142 * Cleans up.
143 */
144 @After
145 public void tearDownTest() {
146 }
147
148 /**
149 * Tests the result of the rest api GET when there are no port pairs.
150 */
151 @Test
152 public void testPortPairsEmpty() {
153
154 expect(portPairService.getPortPairs()).andReturn(null).anyTimes();
155 replay(portPairService);
Jian Li9d616492016-03-09 10:52:49 -0800156 final WebTarget wt = target();
157 final String response = wt.path("port_pairs").request().get(String.class);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530158 assertThat(response, is("{\"port_pairs\":[]}"));
159 }
160
161 /**
162 * Tests the result of a rest api GET for port pair id.
163 */
164 @Test
165 public void testGetPortPairId() {
166
167 final Set<PortPair> portPairs = new HashSet<>();
168 portPairs.add(portPair1);
169
170 expect(portPairService.exists(anyObject())).andReturn(true).anyTimes();
171 expect(portPairService.getPortPair(anyObject())).andReturn(portPair1).anyTimes();
172 replay(portPairService);
173
Jian Li9d616492016-03-09 10:52:49 -0800174 final WebTarget wt = target();
175 final String response = wt.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae")
176 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800177 final JsonObject result = Json.parse(response).asObject();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530178 assertThat(result, notNullValue());
179 }
180
181 /**
182 * Tests that a fetch of a non-existent port pair object throws an exception.
183 */
184 @Test
185 public void testBadGet() {
186 expect(portPairService.getPortPair(anyObject()))
187 .andReturn(null).anyTimes();
188 replay(portPairService);
Jian Li9d616492016-03-09 10:52:49 -0800189 WebTarget wt = target();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530190 try {
Jian Li9d616492016-03-09 10:52:49 -0800191 wt.path("port_pairs/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
192 .request().get(String.class);
Phaneendra Manda63d24702015-11-14 14:56:42 +0530193 fail("Fetch of non-existent port pair did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800194 } catch (NotFoundException ex) {
Phaneendra Manda63d24702015-11-14 14:56:42 +0530195 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800196 containsString("HTTP 404 Not Found"));
Phaneendra Manda63d24702015-11-14 14:56:42 +0530197 }
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
Jian Li9d616492016-03-09 10:52:49 -0800210 WebTarget wt = target();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530211 InputStream jsonStream = PortPairResourceTest.class.getResourceAsStream("post-PortPair.json");
212
Jian Li9d616492016-03-09 10:52:49 -0800213 Response response = wt.path("port_pairs")
214 .request(MediaType.APPLICATION_JSON_TYPE)
215 .post(Entity.json(jsonStream));
Phaneendra Manda63d24702015-11-14 14:56:42 +0530216 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
Jian Li9d616492016-03-09 10:52:49 -0800228 WebTarget wt = target();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530229
230 String location = "port_pairs/78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae";
231
Jian Li9d616492016-03-09 10:52:49 -0800232 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700233 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800234 .delete();
Phaneendra Manda63d24702015-11-14 14:56:42 +0530235 assertThat(deleteResponse.getStatus(),
236 is(HttpURLConnection.HTTP_NO_CONTENT));
237 }
238}