Jian Li | 5c51d90a | 2018-02-20 14:10:25 +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 | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 24 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
| 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 | 5c51d90a | 2018-02-20 14:10:25 +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 floating IP REST API. |
| 45 | */ |
| 46 | public class OpenstackFloatingIpWebResourceTest extends ResourceTest { |
| 47 | |
| 48 | final OpenstackRouterAdminService mockOpenstackRouterAdminService = |
| 49 | createMock(OpenstackRouterAdminService.class); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 50 | final OpenstackHaService mockOpenstackHaService = createMock(OpenstackHaService.class); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 51 | private static final String PATH = "floatingips"; |
| 52 | |
| 53 | /** |
| 54 | * Constructs an openstack floating IP test instance. |
| 55 | */ |
| 56 | public OpenstackFloatingIpWebResourceTest() { |
| 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(OpenstackRouterAdminService.class, |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 68 | mockOpenstackRouterAdminService) |
| 69 | .add(OpenstackHaService.class, mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 70 | setServiceDirectory(testDirectory); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests the results of the REST API POST with creation operation. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testCreateFloatingIpWithCreationOperation() { |
| 78 | mockOpenstackRouterAdminService.createFloatingIp(anyObject()); |
| 79 | replay(mockOpenstackRouterAdminService); |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 80 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 81 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 82 | |
| 83 | final WebTarget wt = target(); |
| 84 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 85 | .getResourceAsStream("openstack-floatingip1.json"); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 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(mockOpenstackRouterAdminService); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests the results of the REST API POST with incorrect input. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testCreateFloatingIpWithIncorrectInput() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 101 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 102 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 103 | final WebTarget wt = target(); |
| 104 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 105 | .getResourceAsStream("dummy.json"); |
| 106 | |
| 107 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 108 | .post(Entity.json(jsonStream)); |
| 109 | final int status = response.getStatus(); |
| 110 | |
| 111 | assertThat(status, is(400)); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Tests the results of the REST API POST with duplicated floating IP. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testCreateFloatingIpWithDuplicatedIp() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 119 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 120 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 121 | mockOpenstackRouterAdminService.createFloatingIp(anyObject()); |
| 122 | expectLastCall().andThrow(new IllegalArgumentException()); |
| 123 | replay(mockOpenstackRouterAdminService); |
| 124 | |
| 125 | final WebTarget wt = target(); |
| 126 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 127 | .getResourceAsStream("openstack-floatingip1.json"); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 128 | |
| 129 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 130 | .post(Entity.json(jsonStream)); |
| 131 | final int status = response.getStatus(); |
| 132 | |
| 133 | assertThat(status, is(400)); |
| 134 | |
| 135 | verify(mockOpenstackRouterAdminService); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Tests the results of the REST API PUT with updating operation. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testUpdateFloatingIpWithUpdatingOperation() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 143 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 144 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 145 | mockOpenstackRouterAdminService.updateFloatingIp(anyObject()); |
| 146 | replay(mockOpenstackRouterAdminService); |
| 147 | |
| 148 | final WebTarget wt = target(); |
| 149 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 150 | .getResourceAsStream("openstack-floatingip1.json"); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 151 | |
| 152 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 153 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 154 | .put(Entity.json(jsonStream)); |
| 155 | final int status = response.getStatus(); |
| 156 | |
| 157 | assertThat(status, is(200)); |
| 158 | |
| 159 | verify(mockOpenstackRouterAdminService); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests the results of the REST API PUT with incorrect input. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testUpdateFloatingIpWithIncorrectInput() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 167 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 168 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 169 | final WebTarget wt = target(); |
| 170 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 171 | .getResourceAsStream("dummy.json"); |
| 172 | |
| 173 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 174 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 175 | .put(Entity.json(jsonStream)); |
| 176 | final int status = response.getStatus(); |
| 177 | |
| 178 | assertThat(status, is(400)); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Tests the results of the REST API PUT with non-existing ID. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testUpdateFloatingIpWithNonexistId() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 186 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 187 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 188 | mockOpenstackRouterAdminService.updateFloatingIp(anyObject()); |
| 189 | expectLastCall().andThrow(new IllegalArgumentException()); |
| 190 | replay(mockOpenstackRouterAdminService); |
| 191 | |
| 192 | final WebTarget wt = target(); |
| 193 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
Daniel Park | 9598538 | 2018-07-23 11:38:07 +0900 | [diff] [blame] | 194 | .getResourceAsStream("openstack-floatingip1.json"); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 195 | |
| 196 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 197 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 198 | .put(Entity.json(jsonStream)); |
| 199 | final int status = response.getStatus(); |
| 200 | |
| 201 | assertThat(status, is(400)); |
| 202 | |
| 203 | verify(mockOpenstackRouterAdminService); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Tests the results of the REST API DELETE with deletion operation. |
| 208 | */ |
| 209 | @Test |
Jian Li | 5fe3447 | 2018-02-21 10:24:31 +0900 | [diff] [blame] | 210 | public void testDeleteFloatingIpWithDeletionOperation() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 211 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 212 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 213 | mockOpenstackRouterAdminService.removeFloatingIp(anyString()); |
| 214 | replay(mockOpenstackRouterAdminService); |
| 215 | |
| 216 | final WebTarget wt = target(); |
| 217 | |
| 218 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 219 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 220 | .delete(); |
| 221 | final int status = response.getStatus(); |
| 222 | |
| 223 | assertThat(status, is(204)); |
| 224 | |
| 225 | verify(mockOpenstackRouterAdminService); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests the results of the REST API DELETE with non-existing ID. |
| 230 | */ |
| 231 | @Test |
| 232 | public void testDeleteFloatingIpWithNonexistId() { |
Jian Li | 7b8c368 | 2019-05-12 13:57:15 +0900 | [diff] [blame] | 233 | expect(mockOpenstackHaService.isActive()).andReturn(true).anyTimes(); |
| 234 | replay(mockOpenstackHaService); |
Jian Li | 5c51d90a | 2018-02-20 14:10:25 +0900 | [diff] [blame] | 235 | mockOpenstackRouterAdminService.removeFloatingIp(anyString()); |
| 236 | expectLastCall().andThrow(new IllegalArgumentException()); |
| 237 | replay(mockOpenstackRouterAdminService); |
| 238 | |
| 239 | final WebTarget wt = target(); |
| 240 | |
| 241 | Response response = wt.path(PATH + "/non-exist-id") |
| 242 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 243 | .delete(); |
| 244 | final int status = response.getStatus(); |
| 245 | |
| 246 | assertThat(status, is(400)); |
| 247 | |
| 248 | verify(mockOpenstackRouterAdminService); |
| 249 | } |
| 250 | } |