blob: 57caf0bc1f2d9815b81abf147a2a91eb8d5d57e4 [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 rule REST API.
45 */
46public class OpenstackSecurityGroupRuleWebResourceTest 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-group-rules";
52
53 /**
54 * Constructs an openstack security group rule test instance.
55 */
56 public OpenstackSecurityGroupRuleWebResourceTest() {
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 testCreateSecurityGroupRulesWithCreationOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +090078 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
79 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +090080 mockOpenstackSecurityGroupAdminService.createSecurityGroupRule(anyObject());
81 replay(mockOpenstackSecurityGroupAdminService);
82
83 final WebTarget wt = target();
84 InputStream jsonStream = OpenstackNetworkWebResourceTest.class
85 .getResourceAsStream("openstack-security-group-rule.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 testCreateSecurityGroupRulesWithIncorrectInput() {
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 = OpenstackSecurityGroupRuleWebResourceTest.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 rule ID.
117 */
118 @Test
119 public void testCreateSecurityGroupRulesWithDuplicatedId() {
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.createSecurityGroupRule(anyObject());
123 expectLastCall().andThrow(new IllegalArgumentException());
124 replay(mockOpenstackSecurityGroupAdminService);
125
126 final WebTarget wt = target();
127 InputStream jsonStream = OpenstackNetworkWebResourceTest.class
128 .getResourceAsStream("openstack-security-group-rule.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 DELETE with deletion operation.
141 */
142 @Test
143 public void testDeleteSecurityGroupRuleWithDeletionOperation() {
Jian Li7b8c3682019-05-12 13:57:15 +0900144 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
145 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +0900146 mockOpenstackSecurityGroupAdminService.removeSecurityGroupRule(anyString());
147 replay(mockOpenstackSecurityGroupAdminService);
148
149 final WebTarget wt = target();
150
151 Response response = wt.path(PATH + "/2bc0accf-312e-429a-956e-e4407625eb62")
152 .request(MediaType.APPLICATION_JSON_TYPE)
153 .delete();
154 final int status = response.getStatus();
155
156 assertThat(status, is(204));
157
158 verify(mockOpenstackSecurityGroupAdminService);
159 }
160
161 /**
162 * Tests the results of the REST API DELETE with non-existing security group rule ID.
163 */
164 @Test
165 public void testDeleteSecurityGroupRuleWithNonexistId() {
Jian Li7b8c3682019-05-12 13:57:15 +0900166 expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes();
167 replay(mockOpenstackHaService);
Jian Li40e63612018-02-21 13:26:20 +0900168 mockOpenstackSecurityGroupAdminService.removeSecurityGroupRule(anyString());
169 expectLastCall().andThrow(new IllegalArgumentException());
170 replay(mockOpenstackSecurityGroupAdminService);
171
172 final WebTarget wt = target();
173
174 Response response = wt.path(PATH + "/non-exist-id")
175 .request(MediaType.APPLICATION_JSON_TYPE)
176 .delete();
177 final int status = response.getStatus();
178
179 assertThat(status, is(400));
180
181 verify(mockOpenstackSecurityGroupAdminService);
182 }
183}