blob: 70335ba3fa84adbf04638112a782621ee5f3ad3d [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
Jian Li80cfe452016-01-14 16:04:58 -080037import com.eclipsesource.json.Json;
Phaneendra Mandad44649e82015-12-01 21:11:57 +053038import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
41import org.onlab.osgi.ServiceDirectory;
42import org.onlab.osgi.TestServiceDirectory;
43import org.onlab.rest.BaseResource;
44import org.onosproject.codec.CodecService;
45import org.onosproject.vtnrsc.PortPairGroup;
46import org.onosproject.vtnrsc.PortPairGroupId;
47import org.onosproject.vtnrsc.PortPairId;
48import org.onosproject.vtnrsc.TenantId;
49import org.onosproject.vtnrsc.portpairgroup.PortPairGroupService;
50import org.onosproject.vtnweb.web.SfcCodecContext;
51
52import 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 }
125 }
126
127 /**
128 * Sets up the global values for all the tests.
129 */
130 @Before
131 public void setUpTest() {
132 SfcCodecContext context = new SfcCodecContext();
133 ServiceDirectory testDirectory = new TestServiceDirectory()
134 .add(PortPairGroupService.class, portPairGroupService)
135 .add(CodecService.class, context.codecManager());
136 BaseResource.setServiceDirectory(testDirectory);
137
138 }
139
140 /**
141 * Cleans up.
142 */
143 @After
144 public void tearDownTest() {
145 }
146
147 /**
148 * Tests the result of the rest api GET when there are no port pair groups.
149 */
150 @Test
151 public void testPortPairGroupsEmpty() {
152
153 expect(portPairGroupService.getPortPairGroups()).andReturn(null).anyTimes();
154 replay(portPairGroupService);
155 final WebResource rs = resource();
156 final String response = rs.path("port_pair_groups").get(String.class);
157 assertThat(response, is("{\"port_pair_groups\":[]}"));
158 }
159
160 /**
161 * Tests the result of a rest api GET for port pair group id.
162 */
163 @Test
164 public void testGetPortPairGroupId() {
165
166 final Set<PortPairGroup> portPairGroups = new HashSet<>();
167 portPairGroups.add(portPairGroup1);
168
169 expect(portPairGroupService.exists(anyObject())).andReturn(true).anyTimes();
170 expect(portPairGroupService.getPortPairGroup(anyObject())).andReturn(portPairGroup1).anyTimes();
171 replay(portPairGroupService);
172
173 final WebResource rs = resource();
174 final String response = rs.path("port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1").get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -0800175 final JsonObject result = Json.parse(response).asObject();
Phaneendra Mandad44649e82015-12-01 21:11:57 +0530176 assertThat(result, notNullValue());
177 }
178
179 /**
180 * Tests that a fetch of a non-existent port pair group object throws an exception.
181 */
182 @Test
183 public void testBadGet() {
184 expect(portPairGroupService.getPortPairGroup(anyObject()))
185 .andReturn(null).anyTimes();
186 replay(portPairGroupService);
187 WebResource rs = resource();
188 try {
189 rs.path("port_pair_groups/78dcd363-fc23-aeb6-f44b-56dc5aafb3ae").get(String.class);
190 fail("Fetch of non-existent port pair group did not throw an exception");
191 } catch (UniformInterfaceException ex) {
192 assertThat(ex.getMessage(),
193 containsString("returned a response status of"));
194 }
195 }
196
197 /**
198 * Tests creating a port pair group with POST.
199 */
200 @Test
201 public void testPost() {
202
203 expect(portPairGroupService.createPortPairGroup(anyObject()))
204 .andReturn(true).anyTimes();
205 replay(portPairGroupService);
206
207 WebResource rs = resource();
208 InputStream jsonStream = PortPairGroupResourceTest.class.getResourceAsStream("post-PortPairGroup.json");
209
210 ClientResponse response = rs.path("port_pair_groups")
211 .type(MediaType.APPLICATION_JSON_TYPE)
212 .post(ClientResponse.class, jsonStream);
213 assertThat(response.getStatus(), is(HttpURLConnection.HTTP_OK));
214 }
215
216 /**
217 * Tests deleting a port pair group.
218 */
219 @Test
220 public void testDelete() {
221 expect(portPairGroupService.removePortPairGroup(anyObject()))
222 .andReturn(true).anyTimes();
223 replay(portPairGroupService);
224
225 WebResource rs = resource();
226
227 String location = "port_pair_groups/4512d643-24fc-4fae-af4b-321c5e2eb3d1";
228
229 ClientResponse deleteResponse = rs.path(location)
230 .type(MediaType.APPLICATION_JSON_TYPE)
231 .delete(ClientResponse.class);
232 assertThat(deleteResponse.getStatus(),
233 is(HttpURLConnection.HTTP_NO_CONTENT));
234 }
235}