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

©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.