2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
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;
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
37 public class ListInsertionDataChangeListenerTest extends AbstractDataChangeListenerTest{
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);
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());
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);
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();
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);
74 * Listener for all list items
76 * Updated should be empty, since no list item was
77 * updated, items were only removed and added
79 assertContains(all.getOriginalData(), TOP_FOO);
80 assertContains(all.getCreatedData(), TOP_BAR);
81 assertEmpty(all.getUpdatedData());
82 assertContains(all.getRemovedPaths(), TOP_FOO);
86 * Listener for all Foo item
88 * This one should see only Foo item removed
90 assertContains(foo.getOriginalData(), TOP_FOO);
91 assertEmpty(foo.getCreatedData());
92 assertEmpty(foo.getUpdatedData());
93 assertContains(foo.getRemovedPaths(), TOP_FOO);
96 * Listener for bar list items
98 * Updated should be empty, since no list item was
99 * updated, items were only removed and added
101 assertEmpty(bar.getOriginalData());
102 assertContains(bar.getCreatedData(), TOP_BAR);
103 assertEmpty(bar.getUpdatedData());
104 assertEmpty(bar.getRemovedPaths());
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);
114 final ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
115 writeTx.merge(CONFIGURATION, TOP, top(topLevelList(TOP_BAR_KEY)));
116 assertCommit(writeTx.submit());
118 verifyBarOnlyAdded(topListener,allListener,fooListener,barListener);
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);
128 final ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
129 writeTx.put(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
130 assertCommit(writeTx.submit());
132 verifyBarOnlyAdded(topListener,allListener,fooListener,barListener);
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);
142 final ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
143 writeTx.merge(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
144 assertCommit(writeTx.submit());
146 verifyBarOnlyAdded(topListener,allListener,fooListener,barListener);
149 private void verifyBarOnlyAdded(final TestListener top, final TestListener all, final TestListener foo,
150 final TestListener bar) {
152 assertFalse(foo.hasEvent());
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());
162 * Listener for all list items
164 * Updated should be empty, since no list item was
165 * updated, items were only removed and added
167 assertEmpty(all.event().getOriginalData());
168 assertContains(all.event().getCreatedData(), TOP_BAR);
169 assertEmpty(all.event().getUpdatedData());
170 assertEmpty(all.event().getRemovedPaths());
173 * Listener for all Foo item
175 * Foo Listener should not have foo event
177 assertFalse(foo.hasEvent());
180 * Listener for bar list items
182 * Updated should be empty, since no list item was
183 * updated, items were only removed and added
185 assertEmpty(bar.event().getOriginalData());
186 assertContains(bar.event().getCreatedData(), TOP_BAR);
187 assertEmpty(bar.event().getUpdatedData());
188 assertEmpty(bar.event().getRemovedPaths());