blob: a5f2150c831b1b9e5923fa630a40bfbe2f9e4a59 [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 Mandad44649e82015-12-01 21:11:57 +053022import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.osgi.ServiceDirectory;
26import org.onlab.osgi.TestServiceDirectory;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053027import org.onosproject.codec.CodecService;
28import org.onosproject.vtnrsc.PortPairGroup;
29import org.onosproject.vtnrsc.PortPairGroupId;
30import org.onosproject.vtnrsc.PortPairId;
31import org.onosproject.vtnrsc.TenantId;
32import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
33import org.onosproject.vtnweb.web.SfcCodecContext;
34
Jian Li9d616492016-03-09 10:52:49 -080035import javax.ws.rs.NotFoundException;
36import javax.ws.rs.client.Entity;
37import javax.ws.rs.client.WebTarget;
38import javax.ws.rs.core.MediaType;
39import javax.ws.rs.core.Response;
40import java.io.InputStream;
41import java.net.HttpURLConnection;
42import java.util.HashSet;
43import java.util.List;
Ray Milkey094a1352018-01-22 14:03:54 -080044import java.util.Map;
Jian Li9d616492016-03-09 10:52:49 -080045import java.util.Objects;
46import java.util.Set;
47
48import static org.easymock.EasyMock.anyObject;
49import static org.easymock.EasyMock.createMock;
50import static org.easymock.EasyMock.expect;
51import static org.easymock.EasyMock.replay;
52import static org.hamcrest.Matchers.containsString;
53import static org.hamcrest.Matchers.is;
54import static org.hamcrest.Matchers.notNullValue;
55import static org.junit.Assert.assertThat;
56import static org.junit.Assert.fail;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053057/**
58 * Unit tests for port pair group REST APIs.
59 */
60public class PortPairGroupResourceTest extends VtnResourceTest {
61
62 final PortPairGroupService portPairGroupService = createMock(PortPairGroupService.class);
63
64 PortPairGroupId portPairGroupId1 = PortPairGroupId.of("4512d643-24fc-4fae-af4b-321c5e2eb3d1");
65 TenantId tenantId1 = TenantId.tenantId("d382007aa9904763a801f68ecf065cf5");
66 private final List<PortPairId> portPairList1 = Lists.newArrayList();
67
68 final MockPortPairGroup portPairGroup1 = new MockPortPairGroup(portPairGroupId1, tenantId1, "portPairGroup1",
69 "Mock port pair group", portPairList1);
70
71 /**
72 * Mock class for a port pair group.
73 */
74 private static class MockPortPairGroup implements PortPairGroup {
75
76 private final PortPairGroupId portPairGroupId;
77 private final TenantId tenantId;
78 private final String name;
79 private final String description;
80 private final List<PortPairId> portPairList;
81
82 public MockPortPairGroup(PortPairGroupId portPairGroupId, TenantId tenantId,
83 String name, String description,
84 List<PortPairId> portPairList) {
85
86 this.portPairGroupId = portPairGroupId;
87 this.tenantId = tenantId;
88 this.name = name;
89 this.description = description;
90 this.portPairList = portPairList;
91 }
92
93 @Override
94 public PortPairGroupId portPairGroupId() {
95 return portPairGroupId;
96 }
97
98 @Override
99 public TenantId tenantId() {
100 return tenantId;
101 }
102
103 @Override
104 public String name() {
105 return name;
106 }
107
108 @Override
109 public String description() {
110 return description;
111 }
112
113 @Override
114 public List<PortPairId> portPairs() {
115 return ImmutableList.copyOf(portPairList);
116 }
117
118 @Override
Phaneendra Manda329a1272016-02-10 22:57:00 +0530119 public void addLoad(PortPairId portPairId) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530120 }
121
122 @Override
123 public int getLoad(PortPairId portPairId) {
Phaneendra Manda329a1272016-02-10 22:57:00 +0530124 return 0;
125 }
Phaneendra Manda0c423422016-04-19 00:31:53 +0530126
127 @Override
128 public Map<PortPairId, Integer> portPairLoadMap() {
129 return null;
130 }
131
132 @Override
133 public boolean exactMatch(PortPairGroup portPairGroup) {
134 return this.equals(portPairGroup) &&
135 Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
136 Objects.equals(this.tenantId, portPairGroup.tenantId());
137 }
Phaneendra Manda8db7d092016-06-04 00:17:24 +0530138
139 @Override
140 public void resetLoad() {
141 }
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530142 }
143
144 /**
145 * Sets up the global values for all the tests.
146 */
147 @Before
148 public void setUpTest() {
149 SfcCodecContext context = new SfcCodecContext();
150 ServiceDirectory testDirectory = new TestServiceDirectory()
151 .add(PortPairGroupService.class, portPairGroupService)
152 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -0800153 setServiceDirectory(testDirectory);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530154
155 }
156
157 /**
158 * Cleans up.
159 */
160 @After
161 public void tearDownTest() {
162 }
163
164 /**
165 * Tests the result of the rest api GET when there are no port pair groups.
166 */
167 @Test
168 public void testPortPairGroupsEmpty() {
169
170 expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes();
171 replay(portPairGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800172 final WebTarget wt = target();
173 final String response = wt.path("port_pair_groups").request().get(String.class);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530174 assertThat(response, is("{\"port_pair_groups\":[]}"));
175 }
176
177 /**
178 * Tests the result of a rest api GET for port pair group id.
179 */
180 @Test
181 public void testGetPortPairGroupId() {
182
183 final Set<PortPairGroup> portPairGroups = new HashSet<>();
184 portPairGroups.add(portPairGroup1);
185
186 expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes();
187 expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes();
188 replay(portPairGroupService);
189
Jian Li9d616492016-03-09 10:52:49 -0800190 final WebTarget wt = target();
191 final String response = wt.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1")
192 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800193 final JsonObject result = Json.parse(response).asObject();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530194 assertThat(result, notNullValue());
195 }
196
197 /**
198 * Tests that a fetch of a non-existent port pair group object throws an exception.
199 */
200 @Test
201 public void testBadGet() {
202 expect(portPairGroupService.getPortPairGroup(anyObject()))
203 .andReturn(null).anyTimes();
204 replay(portPairGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800205 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530206 try {
Jian Li9d616492016-03-09 10:52:49 -0800207 wt.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
208 .request().get(String.class);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530209 fail("Fetch of non-existent port pair group did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800210 } catch (NotFoundException ex) {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530211 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800212 containsString("HTTP 404 Not Found"));
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530213 }
214 }
215
216 /**
217 * Tests creating a port pair group with POST.
218 */
219 @Test
220 public void testPost() {
221
222 expect(portPairGroupService.createPortPairGroup(anyObject()))
223 .andReturn(true).anyTimes();
224 replay(portPairGroupService);
225
Jian Li9d616492016-03-09 10:52:49 -0800226 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530227 InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json");
228
Jian Li9d616492016-03-09 10:52:49 -0800229 Response response = wt.path("port_pair_groups")
230 .request(MediaType.APPLICATION_JSON_TYPE)
231 .post(Entity.json(jsonStream));
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530232 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
233 }
234
235 /**
236 * Tests deleting a port pair group.
237 */
238 @Test
239 public void testDelete() {
240 expect(portPairGroupService.removePortPairGroup(anyObject()))
241 .andReturn(true).anyTimes();
242 replay(portPairGroupService);
243
Jian Li9d616492016-03-09 10:52:49 -0800244 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530245
246 String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1";
247
Jian Li9d616492016-03-09 10:52:49 -0800248 Response deleteResponse = wt.path(location)
Ray Milkey7c251822016-04-06 17:38:25 -0700249 .request(MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE)
Jian Li9d616492016-03-09 10:52:49 -0800250 .delete();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530251 assertThat(deleteResponse.getStatus(),
252 is(HttpURLConnection.HTTP_NO_CONTENT));
253 }
254}