Thomas Vachuska | 7c64be8 | 2017-08-23 14:09:14 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.net.driver.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableMap; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 26 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 27 | import org.onosproject.component.ComponentService; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 30 | import org.onosproject.net.driver.DefaultDriver; |
| 31 | import org.onosproject.net.driver.DefaultDriverProvider; |
| 32 | import org.onosproject.net.driver.DriverEvent; |
| 33 | import org.onosproject.net.driver.DriverListener; |
| 34 | import org.onosproject.net.driver.TestBehaviour; |
| 35 | import org.onosproject.net.driver.TestBehaviourImpl; |
| 36 | |
| 37 | import static org.junit.Assert.*; |
| 38 | import static org.onosproject.net.driver.DefaultDriverTest.*; |
| 39 | import static org.onosproject.net.driver.DriverEvent.Type.DRIVER_ENHANCED; |
| 40 | import static org.onosproject.net.driver.DriverEvent.Type.DRIVER_REDUCED; |
| 41 | |
| 42 | /** |
| 43 | * Suite of tests for the driver registry mechanism. |
| 44 | */ |
| 45 | public class DriverRegistryManagerTest { |
| 46 | |
| 47 | private DriverRegistryManager mgr; |
| 48 | private TestEventListener testListener = new TestEventListener(); |
| 49 | private TestComponentService componentService = new TestComponentService(); |
| 50 | |
| 51 | @Before |
| 52 | public void setUp() { |
| 53 | mgr = new DriverRegistryManager(); |
| 54 | mgr.deviceService = new DeviceServiceAdapter(); |
| 55 | mgr.componentConfigService = new ComponentConfigAdapter(); |
| 56 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 57 | mgr.componenService = componentService; |
| 58 | mgr.activate(null); |
| 59 | } |
| 60 | |
| 61 | @After |
| 62 | public void tearDown() { |
| 63 | mgr.deactivate(); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void basicEvents() { |
| 68 | mgr.addListener(testListener); |
| 69 | DefaultDriverProvider mockProvider = new DefaultDriverProvider(); |
| 70 | DefaultDriver driver = new DefaultDriver("foo", Lists.newArrayList(), |
| 71 | MFR, HW, SW, |
| 72 | ImmutableMap.of(TestBehaviour.class, |
| 73 | TestBehaviourImpl.class), |
| 74 | ImmutableMap.of("foo", "bar")); |
| 75 | mockProvider.addDriver(driver); |
| 76 | mgr.registerProvider(mockProvider); |
| 77 | assertEquals("wrong driver event type", DRIVER_ENHANCED, testListener.event.type()); |
| 78 | assertSame("wrong driver event subject", driver, testListener.event.subject()); |
| 79 | |
| 80 | mgr.unregisterProvider(mockProvider); |
| 81 | assertEquals("wrong driver event type", DRIVER_REDUCED, testListener.event.type()); |
| 82 | assertSame("wrong driver event subject", driver, testListener.event.subject()); |
| 83 | |
| 84 | mgr.removeListener(testListener); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void managerStart() { |
| 89 | DefaultDriverProvider mockProvider = new DefaultDriverProvider(); |
| 90 | DefaultDriver driver = new DefaultDriver("default", Lists.newArrayList(), |
| 91 | MFR, HW, SW, |
| 92 | ImmutableMap.of(TestBehaviour.class, |
| 93 | TestBehaviourImpl.class), |
| 94 | ImmutableMap.of("foo", "bar")); |
| 95 | mockProvider.addDriver(driver); |
| 96 | mgr.registerProvider(mockProvider); |
| 97 | assertTrue("should be activated", componentService.activated); |
| 98 | |
| 99 | mgr.unregisterProvider(mockProvider); |
| 100 | assertFalse("should not be dactivated", componentService.activated); |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void basicQueries() { |
| 105 | DefaultDriverProvider mockProvider = new DefaultDriverProvider(); |
| 106 | DefaultDriver driver = new DefaultDriver("default", Lists.newArrayList(), |
| 107 | MFR, HW, SW, |
| 108 | ImmutableMap.of(TestBehaviour.class, |
| 109 | TestBehaviourImpl.class), |
| 110 | ImmutableMap.of("foo", "bar")); |
| 111 | mockProvider.addDriver(driver); |
| 112 | mgr.registerProvider(mockProvider); |
| 113 | assertSame("driver is missing", driver, mgr.getDriver("default")); |
| 114 | assertSame("driver is missing", driver, mgr.getDriver(MFR, HW, SW)); |
| 115 | assertArrayEquals("driver list is wrong", |
| 116 | ImmutableList.of(driver).toArray(), |
| 117 | mgr.getDrivers().toArray()); |
| 118 | assertArrayEquals("provider list is wrong", |
| 119 | ImmutableList.of(mockProvider).toArray(), |
| 120 | mgr.getProviders().toArray()); |
| 121 | assertEquals("wrong behaviour class", TestBehaviourImpl.class, |
| 122 | mgr.getBehaviourClass("org.onosproject.net.driver.TestBehaviourImpl")); |
| 123 | } |
| 124 | |
| 125 | // TODO: add tests for REGEX matching and for driver inheritance |
| 126 | |
| 127 | private class TestEventListener implements DriverListener { |
| 128 | private DriverEvent event; |
| 129 | |
| 130 | @Override |
| 131 | public void event(DriverEvent event) { |
| 132 | this.event = event; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private class TestComponentService implements ComponentService { |
| 137 | private boolean activated; |
| 138 | |
| 139 | @Override |
| 140 | public void activate(ApplicationId appId, String name) { |
| 141 | activated = true; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void deactivate(ApplicationId appId, String name) { |
| 146 | activated = false; |
| 147 | } |
| 148 | } |
| 149 | } |