Bug 5386: Delete QoS and Queues entries from ovsdb on UNI deletion
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / impl / UnimgrDataChangeListenerTest.java
1 /*
2  * Copyright (c) 2016 CableLabs 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 package org.opendaylight.unimgr.impl;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.atLeast;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
32 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
33 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.unimgr.command.Command;
36 import org.opendaylight.unimgr.command.EvcCreateCommand;
37 import org.opendaylight.unimgr.command.EvcDeleteCommand;
38 import org.opendaylight.unimgr.command.EvcUpdateCommand;
39 import org.opendaylight.unimgr.command.TransactionInvoker;
40 import org.opendaylight.unimgr.command.UniCreateCommand;
41 import org.opendaylight.unimgr.command.UniDeleteCommand;
42 import org.opendaylight.unimgr.command.UniUpdateCommand;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
44 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.powermock.api.mockito.PowerMockito;
48 import org.powermock.api.support.membermodification.MemberModifier;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest({UnimgrMapper.class,
54     UnimgrDataChangeListener.class})
55 public class UnimgrDataChangeListenerTest {
56
57     @Mock private UnimgrDataChangeListener unimgrDataChangeListener;
58     @Mock private HashSet<ListenerRegistration<DataChangeListener>> listeners;
59     @Mock private DataBroker dataBroker;
60     @Mock private TransactionInvoker invoker;
61
62     @Before
63     public void setUp() throws Exception {
64         unimgrDataChangeListener = mock(UnimgrDataChangeListener.class, Mockito.CALLS_REAL_METHODS);
65         MemberModifier.field(UnimgrDataChangeListener.class, "invoker").set(unimgrDataChangeListener, invoker);
66     }
67
68     @SuppressWarnings({ "unchecked", "resource" })
69     @Test
70     public void testUnimgrDataChangeListener() throws Exception {
71         final InstanceIdentifier<Topology> path = mock(InstanceIdentifier.class);
72         final ListenerRegistration<DataChangeListener> listenerRegistration = mock(ListenerRegistration.class);
73         PowerMockito.mockStatic(UnimgrMapper.class);
74         PowerMockito.when(UnimgrMapper.getUniTopologyIid()).thenReturn(path);
75         PowerMockito.when(UnimgrMapper.getEvcTopologyIid()).thenReturn(path);
76         PowerMockito.when(UnimgrMapper.getOvsdbTopologyIid()).thenReturn(path);
77         PowerMockito.whenNew(HashSet.class).withNoArguments().thenReturn(listeners);
78         when(listeners.add(any(ListenerRegistration.class))).thenReturn(true);
79         when(dataBroker.registerDataChangeListener(any(LogicalDatastoreType.class),
80                 any(InstanceIdentifier.class),
81                 any(UnimgrDataChangeListener.class),
82                 eq(DataChangeScope.SUBTREE))).thenReturn(listenerRegistration);
83         new UnimgrDataChangeListener(dataBroker, invoker);
84         assertFalse(listeners.isEmpty());
85         verify(listeners, atLeast(3)).add(any(ListenerRegistration.class));
86         verify(dataBroker, atLeast(3)).registerDataChangeListener(any(LogicalDatastoreType.class),
87                 any(InstanceIdentifier.class),
88                 any(UnimgrDataChangeListener.class),
89                 eq(DataChangeScope.SUBTREE));
90     }
91
92     @SuppressWarnings({ "rawtypes", "unchecked" })
93     @Test
94     public void testOnDataChanged() {
95         final AsyncDataChangeEvent change = mock(AsyncDataChangeEvent.class);
96         final Map map = mock(Map.class);
97         when(change.getCreatedData()).thenReturn(map);
98         doNothing().when(unimgrDataChangeListener).create(any(Map.class));
99         doNothing().when(unimgrDataChangeListener).update(any(Map.class));
100         doNothing().when(unimgrDataChangeListener).delete(any(AsyncDataChangeEvent.class));
101         unimgrDataChangeListener.onDataChanged(change);
102         verify(unimgrDataChangeListener).create(any(Map.class));
103         verify(unimgrDataChangeListener).update(any(Map.class));
104         verify(unimgrDataChangeListener).delete(any(AsyncDataChangeEvent.class));
105     }
106
107     @SuppressWarnings("unchecked")
108     @Test
109     public void testCreate() throws Exception {
110         // False case
111         unimgrDataChangeListener.create(null);
112
113         // True case
114         final Map<InstanceIdentifier<?>, DataObject> changes = mock(Map.class);
115         final UniCreateCommand uniCreate = mock(UniCreateCommand.class);
116         final EvcCreateCommand evcCreate = mock(EvcCreateCommand.class);
117         final List<Command> commands = mock(ArrayList.class);
118         PowerMockito.whenNew(ArrayList.class).withNoArguments()
119                 .thenReturn((ArrayList<Command>) commands);
120         PowerMockito.whenNew(UniCreateCommand.class).withArguments(any(DataBroker.class),
121                 any(Map.class)).thenReturn(uniCreate);
122         PowerMockito.whenNew(EvcCreateCommand.class).withArguments(any(DataBroker.class),
123                 any(Map.class)).thenReturn(evcCreate);
124         when(commands.add(any(Command.class))).thenReturn(true);
125         doNothing().when(invoker).setCommands(any(ArrayList.class));
126         doNothing().when(invoker).invoke();
127         unimgrDataChangeListener.create(changes);
128         verify(invoker, times(1)).setCommands(any(List.class));
129         verify(invoker, times(1)).invoke();
130     }
131
132     @SuppressWarnings("unchecked")
133     @Test
134     public void testUpdate() throws Exception {
135         // False case
136         unimgrDataChangeListener.update(null);
137
138         // True case
139         final Map<InstanceIdentifier<?>, DataObject> changes = mock(Map.class);
140         final UniUpdateCommand uniUpdate = mock(UniUpdateCommand.class);
141         final EvcUpdateCommand evcUpdate = mock(EvcUpdateCommand.class);
142         final List<Command> commands = mock(ArrayList.class);
143         PowerMockito.whenNew(ArrayList.class).withNoArguments()
144                 .thenReturn((ArrayList<Command>) commands);
145         PowerMockito.whenNew(UniUpdateCommand.class).withArguments(any(DataBroker.class),
146                 any(Map.class)).thenReturn(uniUpdate);
147         PowerMockito.whenNew(EvcUpdateCommand.class).withArguments(any(DataBroker.class),
148                 any(Map.class)).thenReturn(evcUpdate);
149         when(commands.add(any(Command.class))).thenReturn(true);
150         doNothing().when(invoker).setCommands(any(ArrayList.class));
151         doNothing().when(invoker).invoke();
152         unimgrDataChangeListener.update(changes);
153         verify(invoker, times(1)).setCommands(any(List.class));
154         verify(invoker, times(1)).invoke();
155     }
156
157     @SuppressWarnings("unchecked")
158     @Test
159     public void testDelete() throws Exception {
160         // False case
161         unimgrDataChangeListener.delete(null);
162
163         // True case
164         final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes = mock(AsyncDataChangeEvent.class);
165         final UniDeleteCommand uniDelete = mock(UniDeleteCommand.class);
166         final EvcDeleteCommand evcDelete = mock(EvcDeleteCommand.class);
167         final List<Command> commands = mock(ArrayList.class);
168         PowerMockito.whenNew(ArrayList.class).withNoArguments()
169                 .thenReturn((ArrayList<Command>) commands);
170         PowerMockito.whenNew(UniDeleteCommand.class).withArguments(any(DataBroker.class),
171                 any(AsyncDataChangeEvent.class)).thenReturn(uniDelete);
172         PowerMockito.whenNew(EvcDeleteCommand.class).withArguments(any(DataBroker.class),
173                 any(AsyncDataChangeEvent.class)).thenReturn(evcDelete);
174         when(commands.add(any(Command.class))).thenReturn(true);
175         doNothing().when(invoker).setCommands(any(ArrayList.class));
176         doNothing().when(invoker).invoke();
177         unimgrDataChangeListener.delete(changes);
178         verify(invoker, times(1)).setCommands(any(List.class));
179         verify(invoker, times(1)).invoke();
180     }
181 }