2 * Copyright (c) 2015 Cisco Systems, Inc. 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.groupbasedpolicy.renderer.ofoverlay.node;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import java.util.HashMap;
17 import java.util.HashSet;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfig;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 public class OfOverlayNodeListenerTest {
39 private OfOverlayNodeListener listener;
41 private DataBroker dataProvider;
42 private SwitchManager switchManager;
43 private ListenerRegistration<DataChangeListener> listenerRegistration;
45 private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
46 private OfOverlayNodeConfig entryValue;
47 private NodeId childNodeId;
48 private InstanceIdentifier<OfOverlayNodeConfig> entryKey;
50 @SuppressWarnings("unchecked")
52 public void initialisation() {
53 dataProvider = mock(DataBroker.class);
54 switchManager = mock(SwitchManager.class);
55 listenerRegistration = mock(ListenerRegistration.class);
57 dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
58 any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(listenerRegistration);
60 change = mock(AsyncDataChangeEvent.class);
61 entryValue = mock(OfOverlayNodeConfig.class);
62 childNodeId = mock(NodeId.class);
63 entryKey = InstanceIdentifier.builder(Nodes.class)
64 .child(Node.class, new NodeKey(childNodeId))
65 .augmentation(OfOverlayNodeConfig.class)
68 listener = new OfOverlayNodeListener(dataProvider, switchManager);
72 public void constructorTest() throws Exception {
74 verify(listenerRegistration).close();
78 public void onDataChangedTestCreatedData() {
79 Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
80 entrySet.put(entryKey, entryValue);
81 when(change.getCreatedData()).thenReturn(entrySet);
83 listener.onDataChanged(change);
84 verify(switchManager).updateSwitchConfig(childNodeId, entryValue);
88 public void onDataChangedTestUpdatedData() {
89 Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
90 entrySet.put(entryKey, entryValue);
91 when(change.getUpdatedData()).thenReturn(entrySet);
93 listener.onDataChanged(change);
94 verify(switchManager).updateSwitchConfig(childNodeId, entryValue);
98 public void onDataChangedTestRemovedPaths() {
99 Set<InstanceIdentifier<?>> removedPaths = new HashSet<InstanceIdentifier<?>>();
100 removedPaths.add(entryKey);
101 when(change.getRemovedPaths()).thenReturn(removedPaths);
103 listener.onDataChanged(change);
104 verify(switchManager).updateSwitchConfig(childNodeId, null);