Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.web; |
| 17 | |
| 18 | import org.glassfish.jersey.server.ResourceConfig; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.osgi.TestServiceDirectory; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 23 | import org.onosproject.openstacknetworking.api.OpenstackHaService; |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
| 25 | import org.onosproject.rest.resources.ResourceTest; |
| 26 | |
| 27 | import javax.ws.rs.client.Entity; |
| 28 | import javax.ws.rs.client.WebTarget; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | import static org.easymock.EasyMock.anyObject; |
| 34 | import static org.easymock.EasyMock.anyString; |
| 35 | import static org.easymock.EasyMock.createMock; |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 36 | import static org.easymock.EasyMock.expect; |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 37 | import static org.easymock.EasyMock.expectLastCall; |
| 38 | import static org.easymock.EasyMock.replay; |
| 39 | import static org.easymock.EasyMock.verify; |
| 40 | import static org.hamcrest.Matchers.is; |
| 41 | import static org.junit.Assert.assertThat; |
| 42 | |
| 43 | /** |
| 44 | * Unit test for openstack subnet REST API. |
| 45 | */ |
| 46 | public class OpenstackSubnetWebResourceTest extends ResourceTest { |
| 47 | |
| 48 | final OpenstackNetworkAdminService mockOpenstackNetworkAdminService = |
| 49 | createMock(OpenstackNetworkAdminService.class); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 50 | final OpenstackHaService mockOpenstackHaService = createMock(OpenstackHaService.class); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 51 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 68 | mockOpenstackNetworkAdminService) |
| 69 | .add(OpenstackHaService.class, mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 70 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 79 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 80 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 81 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 102 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 103 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 104 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 120 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 121 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 122 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 144 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 145 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 146 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 168 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 169 | replay(mockOpenstackHaService); |
| 170 | |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 171 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 188 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 189 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 190 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 213 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 214 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 215 | 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 Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 235 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 236 | replay(mockOpenstackHaService); |
Jian Li | 96abb15 | 2018-02-21 17:06:58 +0900 | [diff] [blame] | 237 | 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 | } |