2 * Copyright (c) 2014 Pacnet and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.applications.lldpspeaker;
11 import static org.mockito.Mockito.*;
13 import java.util.HashMap;
14 import java.util.HashSet;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
23 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 * Tests for @{NodeConnectorInventoryEventTranslator} class.
36 @RunWith(MockitoJUnitRunner.class)
37 public class NodeConnectorInventoryEventTranslatorTest {
38 static InstanceIdentifier<NodeConnector> id = TestUtils.createNodeConnectorId("openflow:1", "openflow:1:1");
39 static FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector().build();
41 @Mock DataBroker dataBroker;
42 @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
43 @Mock NodeConnectorEventsObserver eventsObserver;
44 @Mock NodeConnectorEventsObserver eventsObserver2;
46 MockDataChangedEvent dataChangedEvent = new MockDataChangedEvent();
47 NodeConnectorInventoryEventTranslator translator;
51 when(dataBroker.registerDataChangeListener(
52 any(LogicalDatastoreType.class),
53 any(InstanceIdentifier.class),
54 any(DataChangeListener.class),
55 any(AsyncDataBroker.DataChangeScope.class)))
56 .thenReturn(dataChangeListenerRegistration);
57 translator = new NodeConnectorInventoryEventTranslator(dataBroker, eventsObserver, eventsObserver2);
61 * Test that checks if @{NodeConnectorEventsObserver#nodeConnectorAdded} is called
62 * for each FlowCapableNodeConnector item that @{AsyncDataChangeEvent#getCreatedData} return.
65 public void testNodeConnectorCreation() {
66 // Setup dataChangedEvent to mock new port creation in inventory
67 dataChangedEvent.created.put(id, fcnc);
69 // Invoke NodeConnectorInventoryEventTranslator and check result
70 translator.onDataChanged(dataChangedEvent);
71 verify(eventsObserver).nodeConnectorAdded(id, fcnc);
75 * Test that checks that nothing is called when port appeared in inventory in link down state.
78 public void testNodeConnectorCreationLinkDown() {
79 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(true, false).build();
81 // Setup dataChangedEvent to mock new port creation in inventory
82 dataChangedEvent.created.put(id, fcnc);
84 // Invoke NodeConnectorInventoryEventTranslator and check result
85 translator.onDataChanged(dataChangedEvent);
86 verifyZeroInteractions(eventsObserver);
90 * Test that checks that nothing is called when port appeared in inventory in admin down state.
93 public void testNodeConnectorCreationAdminDown() {
94 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(false, true).build();
96 // Setup dataChangedEvent to mock new port creation in inventory
97 dataChangedEvent.created.put(id, fcnc);
99 // Invoke NodeConnectorInventoryEventTranslator and check result
100 translator.onDataChanged(dataChangedEvent);
101 verifyZeroInteractions(eventsObserver);
105 * Test that checks if @{NodeConnectorEventsObserver#nodeConnectorRemoved} is called
106 * for each FlowCapableNodeConnector item inside @{AsyncDataChangeEvent#getUpdatedData}
107 * that have link down state.
110 public void testNodeConnectorUpdateToLinkDown() {
111 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(true, false).build();
113 // Setup dataChangedEvent to mock link down
114 dataChangedEvent.updated.put(id, fcnc);
116 // Invoke NodeConnectorInventoryEventTranslator and check result
117 translator.onDataChanged(dataChangedEvent);
118 verify(eventsObserver).nodeConnectorRemoved(id);
122 * Test that checks if @{NodeConnectorEventsObserver#nodeConnectorRemoved} is called
123 * for each FlowCapableNodeConnector item inside @{AsyncDataChangeEvent#getUpdatedData}
124 * that have administrative down state.
127 public void testNodeConnectorUpdateToAdminDown() {
128 FlowCapableNodeConnector fcnc = TestUtils.createFlowCapableNodeConnector(false, true).build();
130 // Setup dataChangedEvent to mock link down and administrative port down
131 dataChangedEvent.updated.put(id, fcnc);
133 // Invoke NodeConnectorInventoryEventTranslator and check result
134 translator.onDataChanged(dataChangedEvent);
135 verify(eventsObserver).nodeConnectorRemoved(id);
139 * Test that checks if @{NodeConnectorEventsObserver#nodeConnectorAdded} is called
140 * for each FlowCapableNodeConnector item inside @{AsyncDataChangeEvent#getUpdatedData}
141 * that have administrative up and link up state.
144 public void testNodeConnectorUpdateToUp() {
145 // Setup dataChangedEvent to mock link up and administrative port up
146 dataChangedEvent.updated.put(id, fcnc);
148 // Invoke NodeConnectorInventoryEventTranslator and check result
149 translator.onDataChanged(dataChangedEvent);
150 verify(eventsObserver).nodeConnectorAdded(id, fcnc);
154 * Test that checks if @{NodeConnectorEventsObserver#nodeConnectorRemoved} is called
155 * for each FlowCapableNodeConnector path that @{AsyncDataChangeEvent#getRemovedPaths} return.
158 public void testNodeConnectorRemoval() {
159 // Setup dataChangedEvent to mock node connector removal
160 dataChangedEvent.removed.add(id);
162 // Invoke NodeConnectorInventoryEventTranslator and check result
163 translator.onDataChanged(dataChangedEvent);
164 verify(eventsObserver).nodeConnectorRemoved(id);
168 * Test that checks if @{NodeConnectorEventsObserver#nodeConnectorAdded} and
169 * @{NodeConnectorEventsObserver#nodeConnectorRemoved} are called for each
170 * observer when multiple observers are registered for notifications.
173 public void testMultipleObserversNotified() throws Exception {
174 // Create prerequisites
175 InstanceIdentifier<NodeConnector> id2 = TestUtils.createNodeConnectorId("openflow:1", "openflow:1:2");
177 // Setup dataChangedEvent to mock port creation and removal
178 dataChangedEvent.created.put(id, fcnc);
179 dataChangedEvent.removed.add(id2);
181 // Invoke onDataChanged and check that both observers notified
182 translator.onDataChanged(dataChangedEvent);
183 verify(eventsObserver).nodeConnectorAdded(id, fcnc);
184 verify(eventsObserver).nodeConnectorRemoved(id2);
185 verify(eventsObserver2).nodeConnectorAdded(id, fcnc);
186 verify(eventsObserver2).nodeConnectorRemoved(id2);
190 * Test that @{ListenerRegistration} is closed when ${NodeConnectorInventoryEventTranslator#close}
195 public void testCleanup() throws Exception {
199 // Verify that ListenerRegistration to DOM events
200 verify(dataChangeListenerRegistration).close();
203 static class MockDataChangedEvent implements AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> {
204 Map<InstanceIdentifier<?>,DataObject> created = new HashMap<>();
205 Map<InstanceIdentifier<?>,DataObject> updated = new HashMap<>();
206 Set<InstanceIdentifier<?>> removed = new HashSet<>();
209 public Map<InstanceIdentifier<?>, DataObject> getCreatedData() {
214 public Map<InstanceIdentifier<?>, DataObject> getUpdatedData() {
219 public Set<InstanceIdentifier<?>> getRemovedPaths() {
224 public Map<InstanceIdentifier<?>, DataObject> getOriginalData() {
225 throw new UnsupportedOperationException("Not implemented by mock");
229 public DataObject getOriginalSubtree() {
230 throw new UnsupportedOperationException("Not implemented by mock");
234 public DataObject getUpdatedSubtree() {
235 throw new UnsupportedOperationException("Not implemented by mock");