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; |
| 23 | import org.onosproject.openstacknetworking.api.OpenstackRouterAdminService; |
| 24 | import org.onosproject.rest.resources.ResourceTest; |
| 25 | |
| 26 | import javax.ws.rs.client.Entity; |
| 27 | import javax.ws.rs.client.WebTarget; |
| 28 | import javax.ws.rs.core.MediaType; |
| 29 | import javax.ws.rs.core.Response; |
| 30 | import java.io.InputStream; |
| 31 | |
| 32 | import static org.easymock.EasyMock.anyObject; |
| 33 | import static org.easymock.EasyMock.anyString; |
| 34 | import static org.easymock.EasyMock.createMock; |
| 35 | import static org.easymock.EasyMock.expectLastCall; |
| 36 | import static org.easymock.EasyMock.replay; |
| 37 | import static org.easymock.EasyMock.verify; |
| 38 | import static org.hamcrest.Matchers.is; |
| 39 | import static org.junit.Assert.assertThat; |
| 40 | |
| 41 | /** |
| 42 | * Unit test for openstack floating IP REST API. |
| 43 | */ |
| 44 | public class OpenstackFloatingIpWebResourceTest extends ResourceTest { |
| 45 | |
| 46 | final OpenstackRouterAdminService mockOpenstackRouterAdminService = |
| 47 | createMock(OpenstackRouterAdminService.class); |
| 48 | private static final String PATH = "floatingips"; |
| 49 | |
| 50 | /** |
| 51 | * Constructs an openstack floating IP test instance. |
| 52 | */ |
| 53 | public OpenstackFloatingIpWebResourceTest() { |
| 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(OpenstackRouterAdminService.class, |
| 65 | mockOpenstackRouterAdminService); |
| 66 | setServiceDirectory(testDirectory); |
| 67 | |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests the results of the REST API POST with creation operation. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testCreateFloatingIpWithCreationOperation() { |
| 75 | mockOpenstackRouterAdminService.createFloatingIp(anyObject()); |
| 76 | replay(mockOpenstackRouterAdminService); |
| 77 | |
| 78 | final WebTarget wt = target(); |
| 79 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 80 | .getResourceAsStream("openstack-floatingip.json"); |
| 81 | |
| 82 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 83 | .post(Entity.json(jsonStream)); |
| 84 | final int status = response.getStatus(); |
| 85 | |
| 86 | assertThat(status, is(201)); |
| 87 | |
| 88 | verify(mockOpenstackRouterAdminService); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Tests the results of the REST API POST with incorrect input. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testCreateFloatingIpWithIncorrectInput() { |
| 96 | final WebTarget wt = target(); |
| 97 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 98 | .getResourceAsStream("dummy.json"); |
| 99 | |
| 100 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 101 | .post(Entity.json(jsonStream)); |
| 102 | final int status = response.getStatus(); |
| 103 | |
| 104 | assertThat(status, is(400)); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Tests the results of the REST API POST with duplicated floating IP. |
| 109 | */ |
| 110 | @Test |
| 111 | public void testCreateFloatingIpWithDuplicatedIp() { |
| 112 | mockOpenstackRouterAdminService.createFloatingIp(anyObject()); |
| 113 | expectLastCall().andThrow(new IllegalArgumentException()); |
| 114 | replay(mockOpenstackRouterAdminService); |
| 115 | |
| 116 | final WebTarget wt = target(); |
| 117 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 118 | .getResourceAsStream("openstack-floatingip.json"); |
| 119 | |
| 120 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 121 | .post(Entity.json(jsonStream)); |
| 122 | final int status = response.getStatus(); |
| 123 | |
| 124 | assertThat(status, is(400)); |
| 125 | |
| 126 | verify(mockOpenstackRouterAdminService); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Tests the results of the REST API PUT with updating operation. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testUpdateFloatingIpWithUpdatingOperation() { |
| 134 | mockOpenstackRouterAdminService.updateFloatingIp(anyObject()); |
| 135 | replay(mockOpenstackRouterAdminService); |
| 136 | |
| 137 | final WebTarget wt = target(); |
| 138 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 139 | .getResourceAsStream("openstack-floatingip.json"); |
| 140 | |
| 141 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 142 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 143 | .put(Entity.json(jsonStream)); |
| 144 | final int status = response.getStatus(); |
| 145 | |
| 146 | assertThat(status, is(200)); |
| 147 | |
| 148 | verify(mockOpenstackRouterAdminService); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tests the results of the REST API PUT with incorrect input. |
| 153 | */ |
| 154 | @Test |
| 155 | public void testUpdateFloatingIpWithIncorrectInput() { |
| 156 | final WebTarget wt = target(); |
| 157 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 158 | .getResourceAsStream("dummy.json"); |
| 159 | |
| 160 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 161 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 162 | .put(Entity.json(jsonStream)); |
| 163 | final int status = response.getStatus(); |
| 164 | |
| 165 | assertThat(status, is(400)); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Tests the results of the REST API PUT with non-existing ID. |
| 170 | */ |
| 171 | @Test |
| 172 | public void testUpdateFloatingIpWithNonexistId() { |
| 173 | mockOpenstackRouterAdminService.updateFloatingIp(anyObject()); |
| 174 | expectLastCall().andThrow(new IllegalArgumentException()); |
| 175 | replay(mockOpenstackRouterAdminService); |
| 176 | |
| 177 | final WebTarget wt = target(); |
| 178 | InputStream jsonStream = OpenstackFloatingIpWebResourceTest.class |
| 179 | .getResourceAsStream("openstack-floatingip.json"); |
| 180 | |
| 181 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 182 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 183 | .put(Entity.json(jsonStream)); |
| 184 | final int status = response.getStatus(); |
| 185 | |
| 186 | assertThat(status, is(400)); |
| 187 | |
| 188 | verify(mockOpenstackRouterAdminService); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Tests the results of the REST API DELETE with deletion operation. |
| 193 | */ |
| 194 | @Test |
| 195 | public void testDeleteFloatingIpDeletionOperation() { |
| 196 | mockOpenstackRouterAdminService.removeFloatingIp(anyString()); |
| 197 | replay(mockOpenstackRouterAdminService); |
| 198 | |
| 199 | final WebTarget wt = target(); |
| 200 | |
| 201 | Response response = wt.path(PATH + "/2f245a7b-796b-4f26-9cf9-9e82d248fda7") |
| 202 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 203 | .delete(); |
| 204 | final int status = response.getStatus(); |
| 205 | |
| 206 | assertThat(status, is(204)); |
| 207 | |
| 208 | verify(mockOpenstackRouterAdminService); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Tests the results of the REST API DELETE with non-existing ID. |
| 213 | */ |
| 214 | @Test |
| 215 | public void testDeleteFloatingIpWithNonexistId() { |
| 216 | mockOpenstackRouterAdminService.removeFloatingIp(anyString()); |
| 217 | expectLastCall().andThrow(new IllegalArgumentException()); |
| 218 | replay(mockOpenstackRouterAdminService); |
| 219 | |
| 220 | final WebTarget wt = target(); |
| 221 | |
| 222 | Response response = wt.path(PATH + "/non-exist-id") |
| 223 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 224 | .delete(); |
| 225 | final int status = response.getStatus(); |
| 226 | |
| 227 | assertThat(status, is(400)); |
| 228 | |
| 229 | verify(mockOpenstackRouterAdminService); |
| 230 | } |
| 231 | } |