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