blob: 9b7c9878147904aa73efb71ce22c6c8484649c2f [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
18import static org.easymock.EasyMock.anyObject;
19import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
21import static org.easymock.EasyMock.replay;
22import static org.hamcrest.Matchers.containsString;
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.fail;
27
28import java.io.InputStream;
29import java.net.HttpURLConnection;
30import java.util.HashSet;
31import java.util.List;
32import java.util.Objects;
33import java.util.Set;
34
35import javax.ws.rs.core.MediaType;
36
37import org.junit.After;
38import org.junit.Before;
39import org.junit.Test;
40import org.onlab.osgi.ServiceDirectory;
41import org.onlab.osgi.TestServiceDirectory;
42import org.onlab.rest.BaseResource;
43import org.onosproject.codec.CodecService;
44import org.onosproject.vtnrsc.PortPairGroup;
45import org.onosproject.vtnrsc.PortPairGroupId;
46import org.onosproject.vtnrsc.PortPairId;
47import org.onosproject.vtnrsc.TenantId;
48import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
49import org.onosproject.vtnweb.web.SfcCodecContext;
50
Phaneendra Manda329a1272016-02-10 22:57:00 +053051import com.eclipsesource.json.Json;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053052import com.eclipsesource.json.JsonObject;
53import com.google.common.collect.ImmutableList;
54import com.google.common.collect.Lists;
55import com.sun.jersey.api.client.ClientResponse;
56import com.sun.jersey.api.client.UniformInterfaceException;
57import com.sun.jersey.api.client.WebResource;
58/**
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
120 public boolean exactMatch(PortPairGroup portPairGroup) {
121 return this.equals(portPairGroup) &&
122 Objects.equals(this.portPairGroupId, portPairGroup.portPairGroupId()) &&
123 Objects.equals(this.tenantId, portPairGroup.tenantId());
124 }
Phaneendra Manda329a1272016-02-10 22:57:00 +0530125
126 @Override
127 public void addLoad(PortPairId portPairId) {
128 // TODO Auto-generated method stub
129 }
130
131 @Override
132 public int getLoad(PortPairId portPairId) {
133 // TODO Auto-generated method stub
134 return 0;
135 }
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530136 }
137
138 /**
139 * Sets up the global values for all the tests.
140 */
141 @Before
142 public void setUpTest() {
143 SfcCodecContext context = new SfcCodecContext();
144 ServiceDirectory testDirectory = new TestServiceDirectory()
145 .add(PortPairGroupService.class, portPairGroupService)
146 .add(CodecService.class, context.codecManager());
147 BaseResource.setServiceDirectory(testDirectory);
148
149 }
150
151 /**
152 * Cleans up.
153 */
154 @After
155 public void tearDownTest() {
156 }
157
158 /**
159 * Tests the result of the rest api GET when there are no port pair groups.
160 */
161 @Test
162 public void testPortPairGroupsEmpty() {
163
164 expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes();
165 replay(portPairGroupService);
166 final WebResource rs = resource();
167 final String response = rs.path("port_pair_groups").get(String.class);
168 assertThat(response, is("{\"port_pair_groups\":[]}"));
169 }
170
171 /**
172 * Tests the result of a rest api GET for port pair group id.
173 */
174 @Test
175 public void testGetPortPairGroupId() {
176
177 final Set<PortPairGroup> portPairGroups = new HashSet<>();
178 portPairGroups.add(portPairGroup1);
179
180 expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes();
181 expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes();
182 replay(portPairGroupService);
183
184 final WebResource rs = resource();
185 final String response = rs.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1").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);
198 WebResource rs = resource();
199 try {
200 rs.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
201 fail("Fetch of non-existent port pair group did not throw an exception");
202 } catch (UniformInterfaceException ex) {
203 assertThat(ex.getMessage(),
204 containsString("returned a response status of"));
205 }
206 }
207
208 /**
209 * Tests creating a port pair group with POST.
210 */
211 @Test
212 public void testPost() {
213
214 expect(portPairGroupService.createPortPairGroup(anyObject()))
215 .andReturn(true).anyTimes();
216 replay(portPairGroupService);
217
218 WebResource rs = resource();
219 InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json");
220
221 ClientResponse response = rs.path("port_pair_groups")
222 .type(MediaType.APPLICATION_JSON_TYPE)
223 .post(ClientResponse.class, jsonStream);
224 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
225 }
226
227 /**
228 * Tests deleting a port pair group.
229 */
230 @Test
231 public void testDelete() {
232 expect(portPairGroupService.removePortPairGroup(anyObject()))
233 .andReturn(true).anyTimes();
234 replay(portPairGroupService);
235
236 WebResource rs = resource();
237
238 String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1";
239
240 ClientResponse deleteResponse = rs.path(location)
241 .type(MediaType.APPLICATION_JSON_TYPE)
242 .delete(ClientResponse.class);
243 assertThat(deleteResponse.getStatus(),
244 is(HttpURLConnection.HTTP_NO_CONTENT));
245 }
246}