Georgios Katsikas | 8360098 | 2017-05-28 20:41:45 +0200 | [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 | |
| 17 | package org.onosproject.drivers.server; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.behaviour.ControllerConfig; |
| 21 | import org.onosproject.net.behaviour.ControllerInfo; |
| 22 | import org.onosproject.net.driver.Behaviour; |
| 23 | import org.onosproject.net.driver.DefaultDriverData; |
| 24 | import org.onosproject.net.driver.Driver; |
| 25 | import org.onosproject.net.driver.DriverData; |
| 26 | import org.onosproject.net.driver.DriverHandler; |
| 27 | import org.onosproject.net.driver.DriverService; |
| 28 | import org.onosproject.net.driver.DriverServiceAdapter; |
| 29 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.protocol.rest.RestSBController; |
| 32 | import org.onosproject.protocol.rest.RestSBDevice; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.hamcrest.Matchers.notNullValue; |
| 40 | import static org.junit.Assert.assertThat; |
| 41 | |
| 42 | /** |
| 43 | * Unit tests for methods of ServerControllerConfig. |
| 44 | */ |
| 45 | public class ServerControllerConfigTest { |
| 46 | |
| 47 | // Device information used during the tests |
| 48 | private static final String REST_SCHEME = "rest"; |
| 49 | private static final String REST_DEV_TEST_IP_1 = "10.0.0.1"; |
| 50 | private static final int REST_TEST_PORT = 80; |
| 51 | |
| 52 | // Controller information used during the tests |
| 53 | private static final String REST_CTRL_TEST_IP_1 = "10.0.0.253"; |
| 54 | private static final String REST_CTRL_TEST_IP_2 = "10.0.0.254"; |
| 55 | private static final String REST_CTRL_TEST_TYPE = "tcp"; |
| 56 | private static List<ControllerInfo> controllers; |
| 57 | |
| 58 | // Device used during the tests |
| 59 | private DeviceId restDeviceId1; |
| 60 | private RestSBDevice restDevice1; |
| 61 | |
| 62 | // Test Driver service used during the tests |
| 63 | private DriverService driverService; |
| 64 | |
| 65 | // Test mastership service used during the tests |
| 66 | private MastershipService mastershipService; |
| 67 | |
| 68 | // Test Rest SB controller used during the tests |
| 69 | private RestSBController controller; |
| 70 | |
| 71 | @Before |
| 72 | public void setUp() throws Exception { |
| 73 | restDeviceId1 = TestConfig.REST_DEV_ID1; |
| 74 | assertThat(restDeviceId1, notNullValue()); |
| 75 | |
| 76 | restDevice1 = TestConfig.REST_DEV1; |
| 77 | assertThat(restDevice1, notNullValue()); |
| 78 | |
| 79 | controllers = TestConfig.CONTROLLERS; |
| 80 | assertThat(controllers, notNullValue()); |
| 81 | |
| 82 | driverService = new TestDriverService(); |
| 83 | mastershipService = new TestMastershipService(); |
| 84 | controller = new RestSBControllerMock(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Test of setControllers(). |
| 89 | */ |
| 90 | @Test |
| 91 | public void testSetControllers() { |
| 92 | // Get device handler |
| 93 | DriverHandler driverHandler = null; |
| 94 | try { |
| 95 | driverHandler = driverService.createHandler(restDeviceId1); |
| 96 | } catch (Exception e) { |
| 97 | throw e; |
| 98 | } |
| 99 | assertThat(driverHandler, notNullValue()); |
| 100 | |
| 101 | // TODO: Fix this test |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Test of getControllers(). |
| 106 | */ |
| 107 | @Test |
| 108 | public void testGetControllers() { |
| 109 | // Get device handler |
| 110 | DriverHandler driverHandler = null; |
| 111 | try { |
| 112 | driverHandler = driverService.createHandler(restDeviceId1); |
| 113 | } catch (Exception e) { |
| 114 | throw e; |
| 115 | } |
| 116 | assertThat(driverHandler, notNullValue()); |
| 117 | |
| 118 | // Ask for the controllers of this device |
| 119 | List<ControllerInfo> receivedControllers = null; |
| 120 | |
| 121 | // TODO: Fix this test |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test class for driver handler. |
| 126 | */ |
| 127 | private class TestDriverHandler implements DriverHandler { |
| 128 | |
| 129 | private final DeviceId deviceId; |
| 130 | |
| 131 | TestDriverHandler(DeviceId deviceId) { |
| 132 | this.deviceId = deviceId; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public Driver driver() { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public DriverData data() { |
| 142 | // Just create a fake driver data |
| 143 | return new DefaultDriverData(null, deviceId); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | @SuppressWarnings("unchecked") |
| 148 | public <T extends Behaviour> T behaviour(Class<T> behaviourClass) { |
| 149 | // Let's create the behavior |
| 150 | ControllerConfig controllerConfing = null; |
| 151 | try { |
| 152 | controllerConfing = new ServerControllerConfig(); |
| 153 | } catch (Exception e) { |
| 154 | // Do nothing |
| 155 | } |
| 156 | |
| 157 | if (controllerConfing == null) { |
| 158 | return null; |
| 159 | } |
| 160 | |
| 161 | // Set the handler |
| 162 | controllerConfing.setHandler(this); |
| 163 | |
| 164 | // Done, return the behavior |
| 165 | return (T) controllerConfing; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | @SuppressWarnings("unchecked") |
| 170 | public <T> T get(Class<T> serviceClass) { |
| 171 | if (serviceClass == MastershipService.class) { |
| 172 | return (T) mastershipService; |
| 173 | } else if (serviceClass == RestSBController.class) { |
| 174 | return (T) controller; |
| 175 | } |
| 176 | return null; |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) { |
| 181 | return true; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Test class for driver service. |
| 187 | */ |
| 188 | private class TestDriverService extends DriverServiceAdapter { |
| 189 | @Override |
| 190 | public DriverHandler createHandler(DeviceId deviceId, String... credentials) { |
| 191 | return new TestDriverHandler(deviceId); |
| 192 | } |
| 193 | |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Test class for mastership service. |
| 198 | */ |
| 199 | private final class TestMastershipService extends MastershipServiceAdapter { |
| 200 | @Override |
| 201 | public boolean isLocalMaster(DeviceId deviceId) { |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | } |