blob: 4ffafc1e4f44ac45fd09edf187e059c657ae0775 [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 rule REST API.
43 */
44public class OpenstackSecurityGroupRuleWebResourceTest extends ResourceTest {
45
46 final OpenstackSecurityGroupAdminService mockOpenstackSecurityGroupAdminService =
47 createMock(OpenstackSecurityGroupAdminService.class);
48 private static final String PATH = "security-group-rules";
49
50 /**
51 * Constructs an openstack security group rule test instance.
52 */
53 public OpenstackSecurityGroupRuleWebResourceTest() {
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 testCreateSecurityGroupRulesWithCreationOperation() {
74 mockOpenstackSecurityGroupAdminService.createSecurityGroupRule(anyObject());
75 replay(mockOpenstackSecurityGroupAdminService);
76
77 final WebTarget wt = target();
78 InputStream jsonStream = OpenstackNetworkWebResourceTest.class
79 .getResourceAsStream("openstack-security-group-rule.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 testCreateSecurityGroupRulesWithIncorrectInput() {
95 final WebTarget wt = target();
96 InputStream jsonStream = OpenstackSecurityGroupRuleWebResourceTest.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 rule ID.
108 */
109 @Test
110 public void testCreateSecurityGroupRulesWithDuplicatedId() {
111 mockOpenstackSecurityGroupAdminService.createSecurityGroupRule(anyObject());
112 expectLastCall().andThrow(new IllegalArgumentException());
113 replay(mockOpenstackSecurityGroupAdminService);
114
115 final WebTarget wt = target();
116 InputStream jsonStream = OpenstackNetworkWebResourceTest.class
117 .getResourceAsStream("openstack-security-group-rule.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 DELETE with deletion operation.
130 */
131 @Test
132 public void testDeleteSecurityGroupRuleWithDeletionOperation() {
133 mockOpenstackSecurityGroupAdminService.removeSecurityGroupRule(anyString());
134 replay(mockOpenstackSecurityGroupAdminService);
135
136 final WebTarget wt = target();
137
138 Response response = wt.path(PATH + "/2bc0accf-312e-429a-956e-e4407625eb62")
139 .request(MediaType.APPLICATION_JSON_TYPE)
140 .delete();
141 final int status = response.getStatus();
142
143 assertThat(status, is(204));
144
145 verify(mockOpenstackSecurityGroupAdminService);
146 }
147
148 /**
149 * Tests the results of the REST API DELETE with non-existing security group rule ID.
150 */
151 @Test
152 public void testDeleteSecurityGroupRuleWithNonexistId() {
153 mockOpenstackSecurityGroupAdminService.removeSecurityGroupRule(anyString());
154 expectLastCall().andThrow(new IllegalArgumentException());
155 replay(mockOpenstackSecurityGroupAdminService);
156
157 final WebTarget wt = target();
158
159 Response response = wt.path(PATH + "/non-exist-id")
160 .request(MediaType.APPLICATION_JSON_TYPE)
161 .delete();
162 final int status = response.getStatus();
163
164 assertThat(status, is(400));
165
166 verify(mockOpenstackSecurityGroupAdminService);
167 }
168}