Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 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.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 19 | import com.google.common.util.concurrent.MoreExecutors; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreServiceAdapter; |
| 26 | import org.onosproject.core.DefaultApplicationId; |
| 27 | import org.onosproject.event.Event; |
| 28 | import org.onosproject.openstacknetworking.api.OpenstackRouterEvent; |
| 29 | import org.onosproject.openstacknetworking.api.OpenstackRouterListener; |
| 30 | import org.onosproject.store.service.TestStorageService; |
| 31 | import org.openstack4j.model.network.Router; |
| 32 | import org.openstack4j.openstack.networking.domain.NeutronRouter; |
| 33 | |
| 34 | import java.util.List; |
| 35 | |
| 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.junit.Assert.assertTrue; |
| 38 | import static org.onosproject.openstacknetworking.api.OpenstackRouterEvent.Type.*; |
| 39 | |
| 40 | /** |
| 41 | * Unit tests for OpenStack router manager. |
| 42 | */ |
| 43 | public class OpenstackRouterManagerTest { |
| 44 | |
| 45 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 46 | |
| 47 | private static final String UNKNOWN_ID = "unknown_id"; |
| 48 | private static final String ROUTER_ID = "router_1"; |
| 49 | private static final String ROUTER_NAME = "router_1"; |
| 50 | private static final Router ROUTER = NeutronRouter.builder() |
| 51 | .id(ROUTER_ID) |
| 52 | .name(ROUTER_NAME) |
| 53 | .build(); |
| 54 | |
| 55 | private final TestOpenstackRouterListener testListener = new TestOpenstackRouterListener(); |
| 56 | |
| 57 | private OpenstackRouterManager target; |
| 58 | private DistributedOpenstackRouterStore osRouterStore; |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() throws Exception { |
| 62 | osRouterStore = new DistributedOpenstackRouterStore(); |
| 63 | TestUtils.setField(osRouterStore, "coreService", new TestCoreService()); |
| 64 | TestUtils.setField(osRouterStore, "storageService", new TestStorageService()); |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 65 | TestUtils.setField(osRouterStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 66 | osRouterStore.activate(); |
| 67 | |
| 68 | target = new OpenstackRouterManager(); |
| 69 | target.coreService = new TestCoreService(); |
| 70 | target.osRouterStore = osRouterStore; |
| 71 | target.addListener(testListener); |
| 72 | target.activate(); |
| 73 | } |
| 74 | |
| 75 | @After |
| 76 | public void tearDown() { |
| 77 | target.removeListener(testListener); |
| 78 | osRouterStore.deactivate(); |
| 79 | target.deactivate(); |
| 80 | osRouterStore = null; |
| 81 | target = null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests if getting all routers returns correct set of values. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testGetRouters() { |
| 89 | createBasicRouters(); |
| 90 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests if getting a router with ID returns correct value. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testGetRouterById() { |
| 98 | createBasicRouters(); |
| 99 | assertTrue("Router did not exist", target.router(ROUTER_ID) != null); |
| 100 | assertTrue("Router did not exist", target.router(UNKNOWN_ID) == null); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Tests creating and removing a router, and checks if proper event is triggered. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testCreateAndRemoveRouter() { |
| 108 | target.createRouter(ROUTER); |
| 109 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 110 | assertTrue("Router was not created", target.router(ROUTER_ID) != null); |
| 111 | |
| 112 | target.removeRouter(ROUTER.getId()); |
| 113 | assertEquals("Number of router did not match", 0, target.routers().size()); |
| 114 | assertTrue("Router was not removed", target.router(ROUTER_ID) == null); |
| 115 | |
| 116 | validateEvents(OPENSTACK_ROUTER_CREATED, OPENSTACK_ROUTER_REMOVED); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Tests creating and updating a router, and checks if proper event is triggered. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testCreateAndUpdateRouter() { |
| 124 | target.createRouter(ROUTER); |
| 125 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 126 | assertEquals("Router did not match", ROUTER_NAME, target.router(ROUTER_ID).getName()); |
| 127 | |
| 128 | final Router updated = NeutronRouter.builder() |
| 129 | .id(ROUTER_ID) |
| 130 | .name("updated-name") |
| 131 | .build(); |
| 132 | |
| 133 | target.updateRouter(updated); |
| 134 | assertEquals("Number of router did not match", 1, target.routers().size()); |
| 135 | assertEquals("Router did not match", "updated-name", target.router(ROUTER_ID).getName()); |
| 136 | |
| 137 | validateEvents(OPENSTACK_ROUTER_CREATED, OPENSTACK_ROUTER_UPDATED); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Tests adding and removing external gateway to a router, and checks if |
| 142 | * proper events are triggered. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testAddAndRemoveExternalGateway() { |
| 146 | target.createRouter(ROUTER); |
| 147 | assertTrue("Router did not match", target.router(ROUTER_ID).getExternalGatewayInfo() == null); |
| 148 | |
| 149 | Router updated = NeutronRouter.builder() |
| 150 | .id(ROUTER_ID) |
| 151 | .name(ROUTER_NAME) |
| 152 | .externalGateway("test-network-id") |
| 153 | .build(); |
| 154 | |
| 155 | target.updateRouter(updated); |
| 156 | assertEquals("Router did not match", |
| 157 | "test-network-id", |
| 158 | target.router(ROUTER_ID).getExternalGatewayInfo().getNetworkId()); |
| 159 | |
| 160 | target.updateRouter(ROUTER); |
| 161 | assertTrue("Router did not match", target.router(ROUTER_ID).getExternalGatewayInfo() == null); |
| 162 | |
| 163 | validateEvents(OPENSTACK_ROUTER_CREATED, OPENSTACK_ROUTER_UPDATED, |
| 164 | OPENSTACK_ROUTER_GATEWAY_ADDED, |
| 165 | OPENSTACK_ROUTER_UPDATED, |
| 166 | OPENSTACK_ROUTER_GATEWAY_REMOVED); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Tests if creating a router with null value fails with an exception. |
| 171 | */ |
| 172 | @Test(expected = NullPointerException.class) |
| 173 | public void testCreateRouterWithNull() { |
| 174 | target.createRouter(null); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Tests if creating a router with null ID fails with an exception. |
| 179 | */ |
| 180 | @Test(expected = IllegalArgumentException.class) |
| 181 | public void testCreateRouterWithNullId() { |
| 182 | final Router testRouter = NeutronRouter.builder() |
| 183 | .id(null) |
| 184 | .name(ROUTER_NAME) |
| 185 | .build(); |
| 186 | target.createRouter(testRouter); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Tests if updating a router with null name fails with an exception. |
| 191 | */ |
| 192 | @Test(expected = IllegalArgumentException.class) |
| 193 | public void testCreateRouterWithNullName() { |
| 194 | final Router testRouter = NeutronRouter.builder() |
| 195 | .id(ROUTER_ID) |
| 196 | .name(null) |
| 197 | .build(); |
| 198 | target.createRouter(testRouter); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Tests if creating a duplicate router fails with an exception. |
| 203 | */ |
| 204 | @Test(expected = IllegalArgumentException.class) |
| 205 | public void testCreateDuplicateRouter() { |
| 206 | target.createRouter(ROUTER); |
| 207 | target.createRouter(ROUTER); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Tests if updating a router with null value fails with an exception. |
| 212 | */ |
| 213 | @Test(expected = NullPointerException.class) |
| 214 | public void testUpdateRouterWithNull() { |
| 215 | target.updateRouter(null); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Tests if updating a router with null ID fails with an exception. |
| 220 | */ |
| 221 | @Test(expected = IllegalArgumentException.class) |
| 222 | public void testUpdateRouterWithNullId() { |
| 223 | final Router testRouter = NeutronRouter.builder() |
| 224 | .id(null) |
| 225 | .name(ROUTER_NAME) |
| 226 | .build(); |
| 227 | target.updateRouter(testRouter); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Tests if updating a router with null name fails with an exception. |
| 232 | */ |
| 233 | @Test(expected = IllegalArgumentException.class) |
| 234 | public void testUpdateRouterWithNullName() { |
| 235 | final Router testRouter = NeutronRouter.builder() |
| 236 | .id(ROUTER_ID) |
| 237 | .name(null) |
| 238 | .build(); |
| 239 | target.updateRouter(testRouter); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Tests if updating an unregistered router fails with an exception. |
| 244 | */ |
| 245 | @Test(expected = IllegalArgumentException.class) |
| 246 | public void testUpdateUnregisteredRouter() { |
| 247 | target.updateRouter(ROUTER); |
| 248 | } |
| 249 | |
| 250 | // TODO fix openstack4j floating IP data model and add unit tests |
| 251 | |
| 252 | private void createBasicRouters() { |
| 253 | target.createRouter(ROUTER); |
| 254 | } |
| 255 | |
| 256 | private static class TestCoreService extends CoreServiceAdapter { |
| 257 | |
| 258 | @Override |
| 259 | public ApplicationId registerApplication(String name) { |
| 260 | return TEST_APP_ID; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | private static class TestOpenstackRouterListener implements OpenstackRouterListener { |
| 265 | private List<OpenstackRouterEvent> events = Lists.newArrayList(); |
| 266 | |
| 267 | @Override |
| 268 | public void event(OpenstackRouterEvent event) { |
| 269 | events.add(event); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private void validateEvents(Enum... types) { |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 274 | int i = 0; |
| 275 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 276 | for (Event event : testListener.events) { |
| 277 | assertEquals("Incorrect event received", types[i], event.type()); |
| 278 | i++; |
| 279 | } |
| 280 | testListener.events.clear(); |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 281 | } |
| 282 | } |