blob: 49f85b41ff470c3900aa672d11f054749729ad83 [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;
23import org.onosproject.openstacknetworking.api.OpenstackSecurityGroupAdminService;
24import org.onosproject.rest.resources.ResourceTest;
25
26import javax.ws.rs.client.Entity;
27import javax.ws.rs.client.WebTarget;
28import javax.ws.rs.core.MediaType;
29import javax.ws.rs.core.Response;
30import java.io.InputStream;
31
32import static org.easymock.EasyMock.anyObject;
33import static org.easymock.EasyMock.anyString;
34import static org.easymock.EasyMock.createMock;
35import static org.easymock.EasyMock.expectLastCall;
36import static org.easymock.EasyMock.replay;
37import static org.easymock.EasyMock.verify;
38import static org.hamcrest.Matchers.is;
39import static org.junit.Assert.assertThat;
40
41/**
42 * Unit test for openstack security group REST API.
43 */
44public class OpenstackSecurityGroupWebResourceTest extends ResourceTest {
45
46 final OpenstackSecurityGroupAdminService mockOpenstackSecurityGroupAdminService =
47 createMock(OpenstackSecurityGroupAdminService.class);
48 private static final String PATH = "security-groups";
49
50 /**
51 * Constructs an openstack security group test instance.
52 */
53 public OpenstackSecurityGroupWebResourceTest() {
54 super(ResourceConfig.forApplicationClass(OpenstackNetworkingWebApplication.class));
55 }
56
57 /**
58 * Sets up the global values for all tests.
59 */
60 @Before
61 public void setUpTest() {
62 ServiceDirectory testDirectory =
63 new TestServiceDirectory()
64 .add(OpenstackSecurityGroupAdminService.class,
65 mockOpenstackSecurityGroupAdminService);
66 setServiceDirectory(testDirectory);
67 }
68
69 /**
70 * Tests the results of the REST API POST with creation operation.
71 */
72 @Test
73 public void testCreateSecurityGroupWithCreationOperation() {
74 mockOpenstackSecurityGroupAdminService.createSecurityGroup(anyObject());
75 replay(mockOpenstackSecurityGroupAdminService);
76
77 final WebTarget wt = target();
78 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
79 .getResourceAsStream("openstack-security-group.json");
80
81 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
82 .post(Entity.json(jsonStream));
83 final int status = response.getStatus();
84
85 assertThat(status, is(201));
86
87 verify(mockOpenstackSecurityGroupAdminService);
88 }
89
90 /**
91 * Tests the results of the REST API POST with incorrect input.
92 */
93 @Test
94 public void testCreateSecurityGroupWithIncorrectInput() {
95 final WebTarget wt = target();
96 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
97 .getResourceAsStream("dummy.json");
98
99 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
100 .post(Entity.json(jsonStream));
101 final int status = response.getStatus();
102
103 assertThat(status, is(400));
104 }
105
106 /**
107 * Tests the results of the REST API POST with duplicated security group ID.
108 */
109 @Test
110 public void testCreateSecurityGroupWithDuplicatedId() {
111 mockOpenstackSecurityGroupAdminService.createSecurityGroup(anyObject());
112 expectLastCall().andThrow(new IllegalArgumentException());
113 replay(mockOpenstackSecurityGroupAdminService);
114
115 final WebTarget wt = target();
116 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
117 .getResourceAsStream("openstack-security-group.json");
118
119 Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
120 .post(Entity.json(jsonStream));
121 final int status = response.getStatus();
122
123 assertThat(status, is(400));
124
125 verify(mockOpenstackSecurityGroupAdminService);
126 }
127
128 /**
129 * Tests the results of the REST API PUT with updating operation.
130 */
131 @Test
132 public void testUpdateSecurityGroupWithUpdatingOperation() {
133 final WebTarget wt = target();
134 InputStream jsonStream = OpenstackSecurityGroupWebResourceTest.class
135 .getResourceAsStream("openstack-security-group.json");
136
137 Response response = wt.path(PATH)
138 .request(MediaType.APPLICATION_JSON_TYPE)
139 .put(Entity.json(jsonStream));
140 final int status = response.getStatus();
141
142 assertThat(status, is(200));
143 }
144
145 /**
146 * Tests the results of the REST API DELETE with deletion operation.
147 */
148 @Test
149 public void testDeleteSecurityGroupWithDeletionOperation() {
150 mockOpenstackSecurityGroupAdminService.removeSecurityGroup(anyString());
151 replay(mockOpenstackSecurityGroupAdminService);
152
153 final WebTarget wt = target();
154
155 Response response = wt.path(PATH + "/2076db17-a522-4506-91de-c6dd8e837028")
156 .request(MediaType.APPLICATION_JSON_TYPE)
157 .delete();
158 final int status = response.getStatus();
159
160 assertThat(status, is(204));
161
162 verify(mockOpenstackSecurityGroupAdminService);
163 }
164
165 /**
166 * Tests the results of the REST API DELETE with non-existing security group ID.
167 */
168 @Test
169 public void testDeleteSecurityGroupWithNonexistId() {
170 mockOpenstackSecurityGroupAdminService.removeSecurityGroup(anyString());
171 expectLastCall().andThrow(new IllegalArgumentException());
172 replay(mockOpenstackSecurityGroupAdminService);
173
174 final WebTarget wt = target();
175
176 Response response = wt.path(PATH + "/non-exist-id")
177 .request(MediaType.APPLICATION_JSON_TYPE)
178 .delete();
179 final int status = response.getStatus();
180
181 assertThat(status, is(400));
182
183 verify(mockOpenstackSecurityGroupAdminService);
184 }
185}