blob: cd9a39d3f32b288ad56dcd60d4a046f0151ab24f [file] [log] [blame]
Jian Li96abb152018-02-21 17:06:58 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacknetworking.web;
17
18import org.glassfish.jersey.server.ResourceConfig;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.osgi.ServiceDirectory;
22import org.onlab.osgi.TestServiceDirectory;
Jian Li7b8c3682019-05-12 13:57:15 +090023import org.onosproject.openstacknetworking.api.OpenstackHaService;
Jian Li96abb152018-02-21 17:06:58 +090024import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
25import org.onosproject.rest.resources.ResourceTest;
26
27import javax.ws.rs.client.Entity;
28import javax.ws.rs.client.WebTarget;
29import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.Response;
31import java.io.InputStream;
32
33import static org.easymock.EasyMock.anyObject;
34import static org.easymock.EasyMock.anyString;
35import static org.easymock.EasyMock.createMock;
Jian Li7b8c3682019-05-12 13:57:15 +090036import static org.easymock.EasyMock.expect;
Jian Li96abb152018-02-21 17:06:58 +090037import static org.easymock.EasyMock.expectLastCall;
38import static org.easymock.EasyMock.replay;
39import static org.easymock.EasyMock.verify;
40import static org.hamcrest.Matchers.is;
41import static org.junit.Assert.assertThat;
42
43/**
44 * Unit test for openstack subnet REST API.
45 */
46public class OpenstackSubnetWebResourceTest extends ResourceTest {
47
48 final OpenstackNetworkAdminService mockOpenstackNetworkAdminService =
49 createMock(OpenstackNetworkAdminService.class);
Jian Li7b8c3682019-05-12 13:57:15 +090050 final OpenstackHaService mockOpenstackHaService = createMock(OpenstackHaService.class);
Jian Li96abb152018-02-21 17:06:58 +090051 private static final String PATH = "subnets";
52
53 /**
54 * Constructs an openstack subnet test instance.
55 */
56 public OpenstackSubnetWebResourceTest() {
57 super(ResourceConfig.forApplicationClass(OpenstackNetworkingWebApplication.class));
58 }
59
60 /**
61 * Sets up the global values for all tests.
62 */
63 @Before
64 public void setUpTest() {
65 ServiceDirectory testDirectory =
66 new TestServiceDirectory()
67 .add(OpenstackNetworkAdminService.class,
Jian Li7b8c3682019-05-12 13:57:15 +090068 mockOpenstackNetworkAdminService)
69 .add(OpenstackHaService.class, mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +090070 setServiceDirectory(testDirectory);
71
72 }
73
74 /**
75 * Tests the results of the REST API POST with creation operation.
76 */
77 @Test
78 public void testCreateSubnetWithCreationOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +090079 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
80 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +090081 mockOpenstackNetworkAdminService.createSubnet(anyObject());
82 replay(mockOpenstackNetworkAdminService);
83
84 final WebTarget wt = target();
85 InputStream jsonStream = OpenstackSubnetWebResourceTest.class
86 .getResourceAsStream("openstack-subnet.json");
87
88 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
89 .post(Entity.json(jsonStream));
90 final int status = response.getStatus();
91
92 assertThat(status, is(201));
93
94 verify(mockOpenstackNetworkAdminService);
95 }
96
97 /**
98 * Tests the results of the REST API POST with incorrect input.
99 */
100 @Test
101 public void testCreateSubnetWithIncorrectInput() {
Jian Li7b8c3682019-05-12 13:57:15 +0900102 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
103 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +0900104 final WebTarget wt = target();
105 InputStream jsonStream = OpenstackSubnetWebResourceTest.class
106 .getResourceAsStream("dummy.json");
107
108 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
109 .post(Entity.json(jsonStream));
110 final int status = response.getStatus();
111
112 assertThat(status, is(400));
113 }
114
115 /**
116 * Tests the results of the REST API POST with duplicated subnet ID.
117 */
118 @Test
119 public void testCreateSubnetWithDuplicatedId() {
Jian Li7b8c3682019-05-12 13:57:15 +0900120 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
121 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +0900122 mockOpenstackNetworkAdminService.createSubnet(anyObject());
123 expectLastCall().andThrow(new IllegalArgumentException());
124 replay(mockOpenstackNetworkAdminService);
125
126 final WebTarget wt = target();
127 InputStream jsonStream = OpenstackSubnetWebResourceTest.class
128 .getResourceAsStream("openstack-subnet.json");
129
130 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
131 .post(Entity.json(jsonStream));
132 final int status = response.getStatus();
133
134 assertThat(status, is(400));
135
136 verify(mockOpenstackNetworkAdminService);
137 }
138
139 /**
140 * Tests the results of the REST API PUT with updating operation.
141 */
142 @Test
143 public void testUpdateSubnetWithUpdatingOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +0900144 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
145 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +0900146 mockOpenstackNetworkAdminService.updateSubnet(anyObject());
147 replay(mockOpenstackNetworkAdminService);
148
149 final WebTarget wt = target();
150 InputStream jsonStream = OpenstackSubnetWebResourceTest.class
151 .getResourceAsStream("openstack-subnet.json");
152
153 Response response = wt.path(PATH + "/d32019d3-bc6e-4319-9c1d-6722fc136a22")
154 .request(MediaType.APPLICATION_JSON_TYPE)
155 .put(Entity.json(jsonStream));
156 final int status = response.getStatus();
157
158 assertThat(status, is(200));
159
160 verify(mockOpenstackNetworkAdminService);
161 }
162
163 /**
164 * Tests the results of the REST API PUT with incorrect input.
165 */
166 @Test
167 public void testUpdateSubnetWithIncorrectInput() {
Jian Li7b8c3682019-05-12 13:57:15 +0900168 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
169 replay(mockOpenstackHaService);
170
Jian Li96abb152018-02-21 17:06:58 +0900171 final WebTarget wt = target();
172 InputStream jsonStream = OpenstackSubnetWebResourceTest.class
173 .getResourceAsStream("dummy.json");
174
175 Response response = wt.path(PATH + "/d32019d3-bc6e-4319-9c1d-6722fc136a22")
176 .request(MediaType.APPLICATION_JSON_TYPE)
177 .put(Entity.json(jsonStream));
178 final int status = response.getStatus();
179
180 assertThat(status, is(400));
181 }
182
183 /**
184 * Tests the results of the REST API PUT with non-existing subnet ID.
185 */
186 @Test
187 public void testUpdateSubnetWithNonexistId() {
Jian Li7b8c3682019-05-12 13:57:15 +0900188 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
189 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +0900190 mockOpenstackNetworkAdminService.updateSubnet(anyObject());
191 expectLastCall().andThrow(new IllegalArgumentException());
192 replay(mockOpenstackNetworkAdminService);
193
194 final WebTarget wt = target();
195 InputStream jsonStream = OpenstackSubnetWebResourceTest.class
196 .getResourceAsStream("openstack-subnet.json");
197
198 Response response = wt.path(PATH + "/non-exist-id")
199 .request(MediaType.APPLICATION_JSON_TYPE)
200 .put(Entity.json(jsonStream));
201 final int status = response.getStatus();
202
203 assertThat(status, is(400));
204
205 verify(mockOpenstackNetworkAdminService);
206 }
207
208 /**
209 * Tests the results of the REST API DELETE with deletion operation.
210 */
211 @Test
212 public void testDeleteSubnetWithDeletionOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +0900213 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
214 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +0900215 mockOpenstackNetworkAdminService.removeSubnet(anyString());
216 replay(mockOpenstackNetworkAdminService);
217
218 final WebTarget wt = target();
219
220 Response response = wt.path(PATH + "/d32019d3-bc6e-4319-9c1d-6722fc136a22")
221 .request(MediaType.APPLICATION_JSON_TYPE)
222 .delete();
223 final int status = response.getStatus();
224
225 assertThat(status, is(204));
226
227 verify(mockOpenstackNetworkAdminService);
228 }
229
230 /**
231 * Tests the results of the REST API DELETE with non-existing subnet ID.
232 */
233 @Test
234 public void testDeleteSubnetWithNonexistId() {
Jian Li7b8c3682019-05-12 13:57:15 +0900235 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
236 replay(mockOpenstackHaService);
Jian Li96abb152018-02-21 17:06:58 +0900237 mockOpenstackNetworkAdminService.removeSubnet(anyString());
238 expectLastCall().andThrow(new IllegalArgumentException());
239 replay(mockOpenstackNetworkAdminService);
240
241 final WebTarget wt = target();
242
243 Response response = wt.path(PATH + "/non-exist-id")
244 .request(MediaType.APPLICATION_JSON_TYPE)
245 .delete();
246 final int status = response.getStatus();
247
248 assertThat(status, is(400));
249
250 verify(mockOpenstackNetworkAdminService);
251 }
252}