blob: b631def68641f65a33f86620171859fbd94bcd7e [file] [log] [blame]
Phaneendra Mandad44649e82015-12-01 21:11:57 +05301/*
2 * Copyright 2014-2015 Open Networking Laboratory
3 *
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;
27import org.onlab.rest.BaseResource;
28import org.onosproject.codec.CodecService;
29import org.onosproject.vtnrsc.PortPairGroup;
30import org.onosproject.vtnrsc.PortPairGroupId;
31import org.onosproject.vtnrsc.PortPairId;
32import org.onosproject.vtnrsc.TenantId;
33import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
34import org.onosproject.vtnweb.web.SfcCodecContext;
35
Jian Li9d616492016-03-09 10:52:49 -080036import javax.ws.rs.NotFoundException;
37import javax.ws.rs.client.Entity;
38import javax.ws.rs.client.WebTarget;
39import javax.ws.rs.core.MediaType;
40import javax.ws.rs.core.Response;
41import java.io.InputStream;
42import java.net.HttpURLConnection;
43import java.util.HashSet;
44import java.util.List;
45import 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
119 public boolean exactMatch(PortPairGroup portPairGroup) {
120 return this.equals(portPairGroup) &&
121 Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
122 Objects.equals(this.tenantId, portPairGroup.tenantId());
123 }
Phaneendra Manda329a1272016-02-10 22:57:00 +0530124
125 @Override
126 public void addLoad(PortPairId portPairId) {
127 // TODO Auto-generated method stub
128 }
129
130 @Override
131 public int getLoad(PortPairId portPairId) {
132 // TODO Auto-generated method stub
133 return 0;
134 }
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530135 }
136
137 /**
138 * Sets up the global values for all the tests.
139 */
140 @Before
141 public void setUpTest() {
142 SfcCodecContext context = new SfcCodecContext();
143 ServiceDirectory testDirectory = new TestServiceDirectory()
144 .add(PortPairGroupService.class, portPairGroupService)
145 .add(CodecService.class, context.codecManager());
146 BaseResource.setServiceDirectory(testDirectory);
147
148 }
149
150 /**
151 * Cleans up.
152 */
153 @After
154 public void tearDownTest() {
155 }
156
157 /**
158 * Tests the result of the rest api GET when there are no port pair groups.
159 */
160 @Test
161 public void testPortPairGroupsEmpty() {
162
163 expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes();
164 replay(portPairGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800165 final WebTarget wt = target();
166 final String response = wt.path("port_pair_groups").request().get(String.class);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530167 assertThat(response, is("{\"port_pair_groups\":[]}"));
168 }
169
170 /**
171 * Tests the result of a rest api GET for port pair group id.
172 */
173 @Test
174 public void testGetPortPairGroupId() {
175
176 final Set<PortPairGroup> portPairGroups = new HashSet<>();
177 portPairGroups.add(portPairGroup1);
178
179 expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes();
180 expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes();
181 replay(portPairGroupService);
182
Jian Li9d616492016-03-09 10:52:49 -0800183 final WebTarget wt = target();
184 final String response = wt.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1")
185 .request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800186 final JsonObject result = Json.parse(response).asObject();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530187 assertThat(result, notNullValue());
188 }
189
190 /**
191 * Tests that a fetch of a non-existent port pair group object throws an exception.
192 */
193 @Test
194 public void testBadGet() {
195 expect(portPairGroupService.getPortPairGroup(anyObject()))
196 .andReturn(null).anyTimes();
197 replay(portPairGroupService);
Jian Li9d616492016-03-09 10:52:49 -0800198 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530199 try {
Jian Li9d616492016-03-09 10:52:49 -0800200 wt.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae")
201 .request().get(String.class);
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530202 fail("Fetch of non-existent port pair group did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -0800203 } catch (NotFoundException ex) {
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530204 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -0800205 containsString("HTTP 404 Not Found"));
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530206 }
207 }
208
209 /**
210 * Tests creating a port pair group with POST.
211 */
212 @Test
213 public void testPost() {
214
215 expect(portPairGroupService.createPortPairGroup(anyObject()))
216 .andReturn(true).anyTimes();
217 replay(portPairGroupService);
218
Jian Li9d616492016-03-09 10:52:49 -0800219 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530220 InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json");
221
Jian Li9d616492016-03-09 10:52:49 -0800222 Response response = wt.path("port_pair_groups")
223 .request(MediaType.APPLICATION_JSON_TYPE)
224 .post(Entity.json(jsonStream));
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530225 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
226 }
227
228 /**
229 * Tests deleting a port pair group.
230 */
231 @Test
232 public void testDelete() {
233 expect(portPairGroupService.removePortPairGroup(anyObject()))
234 .andReturn(true).anyTimes();
235 replay(portPairGroupService);
236
Jian Li9d616492016-03-09 10:52:49 -0800237 WebTarget wt = target();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530238
239 String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1";
240
Jian Li9d616492016-03-09 10:52:49 -0800241 Response deleteResponse = wt.path(location)
242 .request(MediaType.APPLICATION_JSON_TYPE)
243 .delete();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530244 assertThat(deleteResponse.getStatus(),
245 is(HttpURLConnection.HTTP_NO_CONTENT));
246 }
247}