blob: d8b78444e9e9636221345c99c311c4365475cc50 [file] [log] [blame]
Jian Li40e63612018-02-21 13:26:20 +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 Li40e63612018-02-21 13:26:20 +090024import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
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 Li40e63612018-02-21 13:26:20 +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 security group REST API.
45 */
46public class OpenstackSecurityGroupWebResourceTest extends ResourceTest {
47
48 final OpenstackSecurityGroupAdminService mockOpenstackSecurityGroupAdminService =
49 createMock(OpenstackSecurityGroupAdminService.class);
Jian Li7b8c3682019-05-12 13:57:15 +090050 final OpenstackHaService mockOpenstackHaService = createMock(OpenstackHaService.class);
Jian Li40e63612018-02-21 13:26:20 +090051 private static final String PATH = "security-groups";
52
53 /**
54 * Constructs an openstack security group test instance.
55 */
56 public OpenstackSecurityGroupWebResourceTest() {
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(OpenstackSecurityGroupAdminService.class,
Jian Li7b8c3682019-05-12 13:57:15 +090068 mockOpenstackSecurityGroupAdminService)
69 .add(OpenstackHaService.class, mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +090070 setServiceDirectory(testDirectory);
71 }
72
73 /**
74 * Tests the results of the REST API POST with creation operation.
75 */
76 @Test
77 public void testCreateSecurityGroupWithCreationOperation() {
78 mockOpenstackSecurityGroupAdminService.createSecurityGroup(anyObject());
79 replay(mockOpenstackSecurityGroupAdminService);
Jian Li7b8c3682019-05-12 13:57:15 +090080 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
81 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +090082
83 final WebTarget wt = target();
84 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
85 .getResourceAsStream("openstack-security-group.json");
86
87 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
88 .post(Entity.json(jsonStream));
89 final int status = response.getStatus();
90
91 assertThat(status, is(201));
92
93 verify(mockOpenstackSecurityGroupAdminService);
94 }
95
96 /**
97 * Tests the results of the REST API POST with incorrect input.
98 */
99 @Test
100 public void testCreateSecurityGroupWithIncorrectInput() {
Jian Li7b8c3682019-05-12 13:57:15 +0900101 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
102 replay(mockOpenstackHaService);
103
Jian Li40e63612018-02-21 13:26:20 +0900104 final WebTarget wt = target();
105 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.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 security group ID.
117 */
118 @Test
119 public void testCreateSecurityGroupWithDuplicatedId() {
Jian Li7b8c3682019-05-12 13:57:15 +0900120 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
121 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +0900122 mockOpenstackSecurityGroupAdminService.createSecurityGroup(anyObject());
123 expectLastCall().andThrow(new IllegalArgumentException());
124 replay(mockOpenstackSecurityGroupAdminService);
125
126 final WebTarget wt = target();
127 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
128 .getResourceAsStream("openstack-security-group.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(mockOpenstackSecurityGroupAdminService);
137 }
138
139 /**
140 * Tests the results of the REST API PUT with updating operation.
141 */
142 @Test
143 public void testUpdateSecurityGroupWithUpdatingOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +0900144 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
145 replay(mockOpenstackHaService);
146
Jian Li40e63612018-02-21 13:26:20 +0900147 final WebTarget wt = target();
148 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
149 .getResourceAsStream("openstack-security-group.json");
150
151 Response response = wt.path(PATH)
152 .request(MediaType.APPLICATION_JSON_TYPE)
153 .put(Entity.json(jsonStream));
154 final int status = response.getStatus();
155
156 assertThat(status, is(200));
157 }
158
159 /**
160 * Tests the results of the REST API DELETE with deletion operation.
161 */
162 @Test
163 public void testDeleteSecurityGroupWithDeletionOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +0900164 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
165 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +0900166 mockOpenstackSecurityGroupAdminService.removeSecurityGroup(anyString());
167 replay(mockOpenstackSecurityGroupAdminService);
168
169 final WebTarget wt = target();
170
171 Response response = wt.path(PATH + "/2076db17-a522-4506-91de-c6dd8e837028")
172 .request(MediaType.APPLICATION_JSON_TYPE)
173 .delete();
174 final int status = response.getStatus();
175
176 assertThat(status, is(204));
177
178 verify(mockOpenstackSecurityGroupAdminService);
179 }
180
181 /**
182 * Tests the results of the REST API DELETE with non-existing security group ID.
183 */
184 @Test
185 public void testDeleteSecurityGroupWithNonexistId() {
Jian Li7b8c3682019-05-12 13:57:15 +0900186 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
187 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +0900188 mockOpenstackSecurityGroupAdminService.removeSecurityGroup(anyString());
189 expectLastCall().andThrow(new IllegalArgumentException());
190 replay(mockOpenstackSecurityGroupAdminService);
191
192 final WebTarget wt = target();
193
194 Response response = wt.path(PATH + "/non-exist-id")
195 .request(MediaType.APPLICATION_JSON_TYPE)
196 .delete();
197 final int status = response.getStatus();
198
199 assertThat(status, is(400));
200
201 verify(mockOpenstackSecurityGroupAdminService);
202 }
203}