Bug 1225: Added DataBroker and DataChangeListener test suite.
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / ListInsertionDataChangeListenerTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.md.sal.binding.impl.test;
9 import static org.junit.Assert.assertFalse;
10 import static org.opendaylight.controller.md.sal.binding.test.AssertCollections.assertContains;
11 import static org.opendaylight.controller.md.sal.binding.test.AssertCollections.assertEmpty;
12 import static org.opendaylight.controller.md.sal.binding.test.AssertCollections.assertNotContains;
13 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
14 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
15 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.top;
17 import static org.opendaylight.controller.md.sal.test.model.util.ListsBindingUtils.topLevelList;
18
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
22 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
23 import org.opendaylight.controller.md.sal.binding.test.AbstractDataChangeListenerTest;
24 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 /**
32  *
33  * This testsuite tests explanation for data change scope and data modifications
34  * which were described in
35  * https://lists.opendaylight.org/pipermail/controller-dev/2014-July/005541.html
36  *
37  *
38  */
39 public class ListInsertionDataChangeListenerTest extends AbstractDataChangeListenerTest{
40
41     private static final InstanceIdentifier<Top> TOP = InstanceIdentifier.create(Top.class);
42     private static final InstanceIdentifier<TopLevelList> WILDCARDED = TOP.child(TopLevelList.class);
43     private static final InstanceIdentifier<TopLevelList> TOP_FOO = TOP.child(TopLevelList.class, TOP_FOO_KEY);
44     private static final InstanceIdentifier<TopLevelList> TOP_BAR = TOP.child(TopLevelList.class, TOP_BAR_KEY);
45
46
47     @Override
48     protected void setupWithDataBroker(final DataBroker dataBroker) {
49         WriteTransaction initialTx = dataBroker.newWriteOnlyTransaction();
50         initialTx.put(CONFIGURATION, TOP, top(topLevelList(TOP_FOO_KEY)));
51         assertCommit(initialTx.commit());
52     }
53
54     @Test
55     public void replaceTopNodeSubtreeListeners() {
56         TestListener topListener = createListener(CONFIGURATION, TOP, DataChangeScope.SUBTREE);
57         TestListener allListener = createListener(CONFIGURATION, WILDCARDED, DataChangeScope.SUBTREE);
58         TestListener fooListener = createListener(CONFIGURATION, TOP_FOO, DataChangeScope.SUBTREE);
59         TestListener barListener = createListener(CONFIGURATION, TOP_BAR, DataChangeScope.SUBTREE);
60
61         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
62         writeTx.put(CONFIGURATION, TOP, top(topLevelList(TOP_BAR_KEY)));
63         assertCommit(writeTx.commit());
64         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> top = topListener.event();
65         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> all = allListener.event();
66         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> foo = fooListener.event();
67         AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> bar = barListener.event();
68
69         // Listener for TOP element
70         assertContains(top.getOriginalData(), TOP,TOP_FOO);
71         assertContains(top.getCreatedData(), TOP_BAR);
72         assertContains(top.getUpdatedData(), TOP);
73         assertContains(top.getRemovedPaths(), TOP_FOO);
74
75         /*
76          *  Listener for all list items
77          *
78          *  Updated should be empty, since no list item was
79          *  updated, items were only removed and added
80          */
81         assertContains(all.getOriginalData(), TOP_FOO);
82         assertContains(all.getCreatedData(), TOP_BAR);
83         assertEmpty(all.getUpdatedData());
84         assertContains(all.getRemovedPaths(), TOP_FOO);
85
86
87         /*
88          *  Listener for all Foo item
89          *
90          *  This one should see only Foo item removed
91          */
92         assertContains(foo.getOriginalData(), TOP_FOO);
93         assertEmpty(foo.getCreatedData());
94         assertEmpty(foo.getUpdatedData());
95         assertContains(foo.getRemovedPaths(), TOP_FOO);
96
97         /*
98          *  Listener for bar list items
99          *
100          *  Updated should be empty, since no list item was
101          *  updated, items were only removed and added
102          */
103         assertEmpty(bar.getOriginalData());
104         assertContains(bar.getCreatedData(), TOP_BAR);
105         assertEmpty(bar.getUpdatedData());
106         assertEmpty(bar.getRemovedPaths());
107     }
108
109     @Test
110     public void mergeTopNodeSubtreeListeners() {
111         TestListener topListener = createListener(CONFIGURATION, TOP, DataChangeScope.SUBTREE);
112         TestListener allListener = createListener(CONFIGURATION, WILDCARDED, DataChangeScope.SUBTREE);
113         TestListener fooListener = createListener(CONFIGURATION, TOP_FOO, DataChangeScope.SUBTREE);
114         TestListener barListener = createListener(CONFIGURATION, TOP_BAR, DataChangeScope.SUBTREE);
115
116         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
117         writeTx.merge(CONFIGURATION, TOP, top(topLevelList(TOP_BAR_KEY)));
118         assertCommit(writeTx.commit());
119
120         verifyBarOnlyAdded(topListener,allListener,fooListener,barListener);
121     }
122
123     @Test
124     public void putTopBarNodeSubtreeListeners() {
125         TestListener topListener = createListener(CONFIGURATION, TOP, DataChangeScope.SUBTREE);
126         TestListener allListener = createListener(CONFIGURATION, WILDCARDED, DataChangeScope.SUBTREE);
127         TestListener fooListener = createListener(CONFIGURATION, TOP_FOO, DataChangeScope.SUBTREE);
128         TestListener barListener = createListener(CONFIGURATION, TOP_BAR, DataChangeScope.SUBTREE);
129
130         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
131         writeTx.put(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
132         assertCommit(writeTx.commit());
133
134         verifyBarOnlyAdded(topListener,allListener,fooListener,barListener);
135     }
136
137     @Test
138     public void mergeTopBarNodeSubtreeListeners() {
139         TestListener topListener = createListener(CONFIGURATION, TOP, DataChangeScope.SUBTREE);
140         TestListener allListener = createListener(CONFIGURATION, WILDCARDED, DataChangeScope.SUBTREE);
141         TestListener fooListener = createListener(CONFIGURATION, TOP_FOO, DataChangeScope.SUBTREE);
142         TestListener barListener = createListener(CONFIGURATION, TOP_BAR, DataChangeScope.SUBTREE);
143
144         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
145         writeTx.merge(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
146         assertCommit(writeTx.commit());
147
148         verifyBarOnlyAdded(topListener,allListener,fooListener,barListener);
149     }
150
151     private void verifyBarOnlyAdded(final TestListener top, final TestListener all, final TestListener foo,
152             final TestListener bar) {
153
154         assertFalse(foo.hasEvent());
155
156         // Listener for TOP element
157         assertContains(top.event().getOriginalData(), TOP);
158         assertNotContains(top.event().getOriginalData(),TOP_FOO);
159         assertContains(top.event().getCreatedData(), TOP_BAR);
160         assertContains(top.event().getUpdatedData(), TOP);
161         assertEmpty(top.event().getRemovedPaths());
162
163         /*
164          *  Listener for all list items
165          *
166          *  Updated should be empty, since no list item was
167          *  updated, items were only removed and added
168          */
169         assertEmpty(all.event().getOriginalData());
170         assertContains(all.event().getCreatedData(), TOP_BAR);
171         assertEmpty(all.event().getUpdatedData());
172         assertEmpty(all.event().getRemovedPaths());
173
174         /*
175          *  Listener for all Foo item
176          *
177          *  Foo Listener should not have foo event
178          */
179         assertFalse(foo.hasEvent());
180
181         /*
182          *  Listener for bar list items
183          *
184          *  Updated should be empty, since no list item was
185          *  updated, items were only removed and added
186          */
187         assertEmpty(bar.event().getOriginalData());
188         assertContains(bar.event().getCreatedData(), TOP_BAR);
189         assertEmpty(bar.event().getUpdatedData());
190         assertEmpty(bar.event().getRemovedPaths());
191     }
192
193 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.