blob: c781c88bddf05a7b6a6aceb30f419ebfea5b5e3d [file] [log] [blame]
Phaneendra Mandad44649e82015-12-01 21:11:57 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Phaneendra Mandad44649e82015-12-01 21:11:57 +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 Li9d616492016-03-09 10:52:49 -080018import com.eclipsesource.json.Json;
19import com.eclipsesource.json.JsonObject;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
Phaneendra Manda0c423422016-04-19 00:31:53 +053022import java.util.Map;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053023import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.osgi.ServiceDirectory;
27import org.onlab.osgi.TestServiceDirectory;
28import org.onlab.rest.BaseResource;
29import org.onosproject.codec.CodecService;
30import org.onosproject.vtnrsc.PortPairGroup;
31import org.onosproject.vtnrsc.PortPairGroupId;
32import org.onosproject.vtnrsc.PortPairId;
33import org.onosproject.vtnrsc.TenantId;
34import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
35import org.onosproject.vtnweb.web.SfcCodecContext;
36
Jian Li9d616492016-03-09 10:52:49 -080037import javax.ws.rs.NotFoundException;
38import javax.ws.rs.client.Entity;
39import javax.ws.rs.client.WebTarget;
40import javax.ws.rs.core.MediaType;
41import javax.ws.rs.core.Response;
42import java.io.InputStream;
43import java.net.HttpURLConnection;
44import java.util.HashSet;
45import java.util.List;
46import java.util.Objects;
47import java.util.Set;
48
49import static org.easymock.EasyMock.anyObject;
50import static org.easymock.EasyMock.createMock;
51import static org.easymock.EasyMock.expect;
52import static org.easymock.EasyMock.replay;
53import static org.hamcrest.Matchers.containsString;
54import static org.hamcrest.Matchers.is;
55import static org.hamcrest.Matchers.notNullValue;
56import static org.junit.Assert.assertThat;
57import static org.junit.Assert.fail;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053058/**
59 * Unit tests for port pair group REST APIs.
60 */
61public class PortPairGroupResourceTest extends VtnResourceTest {
62
63 final PortPairGroupService portPairGroupService = createMock(PortPairGroupService.class);
64
65 PortPairGroupId portPairGroupId1 = PortPairGroupId.of("4512d643-24fc-4fae-af4b-321c5e2eb3d1");
66 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
67 private final List<PortPairId> portPairList1 = Lists.newArrayList();
68
69 final MockPortPairGroup portPairGroup1 = new MockPortPairGroup(portPairGroupId1, tenantId1, "portPairGroup1",
70 "Mock port pair group", portPairList1);
71
72 /**
73 * Mock class for a port pair group.
74 */
75 private static class MockPortPairGroup implements PortPairGroup {
76
77 private final PortPairGroupId portPairGroupId;
78 private final TenantId tenantId;
79 private final String name;
80 private final String description;
81 private final List<PortPairId> portPairList;
82
83 public MockPortPairGroup(PortPairGroupId portPairGroupId, TenantId tenantId,
84 String name, String description,
85 List<PortPairId> portPairList) {
86
87 this.portPairGroupId = portPairGroupId;
88 this.tenantId = tenantId;
89 this.name = name;
90 this.description = description;
91 this.portPairList = portPairList;
92 }
93
94 @Override
95 public PortPairGroupId portPairGroupId() {
96 return portPairGroupId;
97 }
98
99 @Override
100 public TenantId tenantId() {
101 return tenantId;
102 }
103
104 @Override
105 public String name() {
106 return name;
107 }
108
109 @Override
110 public String description() {
111 return description;
112 }
113
114 @Override
115 public List<PortPairId> portPairs() {
116 return ImmutableList.copyOf(portPairList);
117 }
118
119 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +0530120 public void addLoad(PortPairId portPairId) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530121 }
122
123 @Override
124 public int getLoad(PortPairId portPairId) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530125 return 0;
126 }
Phaneendra Manda0c423422016-04-19 00:31:53 +0530127
128 @Override
129 public Map<PortPairId, Integer> portPairLoadMap() {
130 return null;
131 }
132
133 @Override
134 public boolean exactMatch(PortPairGroup portPairGroup) {
135 return this.equals(portPairGroup) &&
136 Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
137 Objects.equals(this.tenantId, portPairGroup.tenantId());
138 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530139
140 @Override
141 public void resetLoad() {
142 }
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530143 }
144
145 /**
146 * Sets up the global values for all the tests.
147 */
148 @Before
149 public void setUpTest() {
150 SfcCodecContext context = new SfcCodecContext();
151 ServiceDirectory testDirectory = new TestServiceDirectory()
152 .add(PortPairGroupService.class, portPairGroupService)
153 .add(CodecService.class, context.codecManager());
154 BaseResource.setServiceDirectory(testDirectory);
155
156 }
157
158 /**
159 * Cleans up.
160 */
161 @After
162 public void tearDownTest() {
163 }
164
165 /**
166 * Tests the result of the rest api GET when there are no port pair groups.
167 */
168 @Test
169 public void testPortPairGroupsEmpty() {
170
171 expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes();
172 replay(portPairGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800173 final WebTarget wt = target();
174 final String response = wt.path("port_pair_groups").request().get(String.class);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530175 assertThat(response, is("{\"port_pair_groups\":[]}"));
176 }
177
178 /**
179 * Tests the result of a rest api GET for port pair group id.
180 */
181 @Test
182 public void testGetPortPairGroupId() {
183
184 final Set<PortPairGroup> portPairGroups = new HashSet<>();
185 portPairGroups.add(portPairGroup1);
186
187 expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes();
188 expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes();
189 replay(portPairGroupService);
190
Jian Li9d616492016-03-09 10:52:49 -0800191 final WebTarget wt = target();
192 final String response = wt.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1")
193 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800194 final JsonObject result = Json.parse(response).asObject();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530195 assertThat(result, notNullValue());
196 }
197
198 /**
199 * Tests that a fetch of a non-existent port pair group object throws an exception.
200 */
201 @Test
202 public void testBadGet() {
203 expect(portPairGroupService.getPortPairGroup(anyObject()))
204 .andReturn(null).anyTimes();
205 replay(portPairGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800206 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530207 try {
Jian Li9d616492016-03-09 10:52:49 -0800208 wt.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
209 .request().get(String.class);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530210 fail("Fetch of non-existent port pair group did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800211 } catch (NotFoundException ex) {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530212 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800213 containsString("HTTP 404 Not Found"));
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530214 }
215 }
216
217 /**
218 * Tests creating a port pair group with POST.
219 */
220 @Test
221 public void testPost() {
222
223 expect(portPairGroupService.createPortPairGroup(anyObject()))
224 .andReturn(true).anyTimes();
225 replay(portPairGroupService);
226
Jian Li9d616492016-03-09 10:52:49 -0800227 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530228 InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json");
229
Jian Li9d616492016-03-09 10:52:49 -0800230 Response response = wt.path("port_pair_groups")
231 .request(MediaType.APPLICATION_JSON_TYPE)
232 .post(Entity.json(jsonStream));
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530233 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
234 }
235
236 /**
237 * Tests deleting a port pair group.
238 */
239 @Test
240 public void testDelete() {
241 expect(portPairGroupService.removePortPairGroup(anyObject()))
242 .andReturn(true).anyTimes();
243 replay(portPairGroupService);
244
Jian Li9d616492016-03-09 10:52:49 -0800245 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530246
247 String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1";
248
Jian Li9d616492016-03-09 10:52:49 -0800249 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700250 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800251 .delete();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530252 assertThat(deleteResponse.getStatus(),
253 is(HttpURLConnection.HTTP_NO_CONTENT));
254 }
255}