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