20481a6e2a283ac32a0534e4ece66fba396981e9
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / node / OfOverlayNodeListenerTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node;
10
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;
15
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
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;
36
37 public class OfOverlayNodeListenerTest {
38
39     private OfOverlayNodeListener listener;
40
41     private DataBroker dataProvider;
42     private SwitchManager switchManager;
43     private ListenerRegistration<DataChangeListener> listenerRegistration;
44
45     private AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change;
46     private OfOverlayNodeConfig entryValue;
47     private NodeId childNodeId;
48     private InstanceIdentifier<OfOverlayNodeConfig> entryKey;
49
50     @SuppressWarnings("unchecked")
51     @Before
52     public void initialisation() {
53         dataProvider = mock(DataBroker.class);
54         switchManager = mock(SwitchManager.class);
55         listenerRegistration = mock(ListenerRegistration.class);
56         when(
57                 dataProvider.registerDataChangeListener(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
58                         any(DataChangeListener.class), any(DataChangeScope.class))).thenReturn(listenerRegistration);
59
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)
66             .build();
67
68         listener = new OfOverlayNodeListener(dataProvider, switchManager);
69     }
70
71     @Test
72     public void constructorTest() throws Exception {
73         listener.close();
74         verify(listenerRegistration).close();
75     }
76
77     @Test
78     public void onDataChangedTestCreatedData() {
79         Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
80         entrySet.put(entryKey, entryValue);
81         when(change.getCreatedData()).thenReturn(entrySet);
82
83         listener.onDataChanged(change);
84         verify(switchManager).updateSwitchConfig(childNodeId, entryValue);
85     }
86
87     @Test
88     public void onDataChangedTestUpdatedData() {
89         Map<InstanceIdentifier<?>, DataObject> entrySet = new HashMap<InstanceIdentifier<?>, DataObject>();
90         entrySet.put(entryKey, entryValue);
91         when(change.getUpdatedData()).thenReturn(entrySet);
92
93         listener.onDataChanged(change);
94         verify(switchManager).updateSwitchConfig(childNodeId, entryValue);
95     }
96
97     @Test
98     public void onDataChangedTestRemovedPaths() {
99         Set<InstanceIdentifier<?>> removedPaths = new HashSet<InstanceIdentifier<?>>();
100         removedPaths.add(entryKey);
101         when(change.getRemovedPaths()).thenReturn(removedPaths);
102
103         listener.onDataChanged(change);
104         verify(switchManager).updateSwitchConfig(childNodeId, null);
105     }
106 }