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;
10 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
11 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
12 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.top;
13 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
14 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.HashSet;
18 import java.util.Objects;
20 import java.util.stream.Collectors;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
24 import org.opendaylight.mdsal.binding.api.WriteTransaction;
25 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataTreeChangeListenerTest;
26 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
34 * This testsuite tests explanation for data change scope and data modifications which were described in
35 * https://lists.opendaylight.org/pipermail/controller-dev/2014-July/005541.html.
37 public class ListInsertionDataChangeListenerTest extends AbstractDataTreeChangeListenerTest {
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);
44 protected Set<YangModuleInfo> getModuleInfos() throws Exception {
45 return ImmutableSet.of(BindingReflections.getModuleInfo(Top.class));
49 public void setupWithDataBroker() {
50 WriteTransaction initialTx = getDataBroker().newWriteOnlyTransaction();
51 initialTx.put(CONFIGURATION, TOP, top(topLevelList(TOP_FOO_KEY)));
52 assertCommit(initialTx.commit());
56 public void replaceTopNodeSubtreeListeners() {
57 final TopLevelList topBar = topLevelList(TOP_BAR_KEY);
58 final Top top = top(topBar);
59 final TopLevelList topFoo = topLevelList(TOP_FOO_KEY);
61 // Listener for TOP element
62 try (var topListener = createCollector(CONFIGURATION, TOP)) {
63 // Listener for all list items. This one should see Foo item deleted and Bar item added.
64 try (var allListener = createCollector(CONFIGURATION, WILDCARDED)) {
65 // Listener for all Foo item. This one should see only Foo item deleted.
66 try (var fooListener = createCollector(CONFIGURATION, TOP_FOO)) {
67 // Listener for bar list items.
68 try (var barListener = createCollector(CONFIGURATION, TOP_BAR)) {
69 final var writeTx = getDataBroker().newWriteOnlyTransaction();
70 writeTx.put(CONFIGURATION, TOP, top);
71 assertCommit(writeTx.commit());
73 barListener.assertModifications(added(TOP_BAR, topBar));
75 fooListener.assertModifications(added(TOP_FOO, topFoo), deleted(TOP_FOO, topFoo));
77 allListener.assertModifications(
78 added(TOP_FOO, topFoo),
79 added(TOP_BAR, topBar),
80 deleted(TOP_FOO, topFoo));
82 topListener.assertModifications(
83 added(TOP, top(topLevelList(TOP_FOO_KEY))),
84 replaced(TOP, top(topFoo), top));
89 public void mergeTopNodeSubtreeListeners() {
90 final var topBar = topLevelList(TOP_BAR_KEY);
91 final var topFoo = topLevelList(TOP_FOO_KEY);
93 try (var topListener = createCollector(CONFIGURATION, TOP)) {
94 try (var allListener = createCollector(CONFIGURATION, WILDCARDED)) {
95 try (var fooListener = createCollector(CONFIGURATION, TOP_FOO)) {
96 try (var barListener = createCollector(CONFIGURATION, TOP_BAR)) {
97 final var writeTx = getDataBroker().newWriteOnlyTransaction();
98 writeTx.merge(CONFIGURATION, TOP, top(topLevelList(TOP_BAR_KEY)));
99 assertCommit(writeTx.commit());
101 barListener.assertModifications(added(TOP_BAR, topBar));
103 fooListener.assertModifications(added(TOP_FOO, topFoo));
105 allListener.assertModifications(added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
107 topListener.assertModifications(
108 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
113 public void putTopBarNodeSubtreeListeners() {
114 final var topBar = topLevelList(TOP_BAR_KEY);
115 final var topFoo = topLevelList(TOP_FOO_KEY);
117 try (var topListener = createCollector(CONFIGURATION, TOP)) {
118 try (var allListener = createCollector(CONFIGURATION, WILDCARDED)) {
119 try (var fooListener = createCollector(CONFIGURATION, TOP_FOO)) {
120 try (var barListener = createCollector(CONFIGURATION, TOP_BAR)) {
121 var writeTx = getDataBroker().newWriteOnlyTransaction();
122 writeTx.put(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
123 assertCommit(writeTx.commit());
125 barListener.assertModifications(added(TOP_BAR, topBar));
127 fooListener.assertModifications(added(TOP_FOO, topFoo));
129 allListener.assertModifications(added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
131 topListener.assertModifications(
132 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
137 public void mergeTopBarNodeSubtreeListeners() {
138 final var topBar = topLevelList(TOP_BAR_KEY);
139 final var topFoo = topLevelList(TOP_FOO_KEY);
141 try (var topListener = createCollector(CONFIGURATION, TOP)) {
142 try (var allListener = createCollector(CONFIGURATION, WILDCARDED)) {
143 try (var fooListener = createCollector(CONFIGURATION, TOP_FOO)) {
144 try (var barListener = createCollector(CONFIGURATION, TOP_BAR)) {
145 final var writeTx = getDataBroker().newWriteOnlyTransaction();
146 writeTx.merge(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
147 assertCommit(writeTx.commit());
149 barListener.assertModifications(added(TOP_BAR, topBar));
151 fooListener.assertModifications(added(TOP_FOO, topFoo));
153 allListener.assertModifications(added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
155 topListener.assertModifications(
156 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
160 private static Matcher<Top> topSubtreeModified(final TopLevelList topFoo, final TopLevelList topBar) {
161 return match(ModificationType.SUBTREE_MODIFIED, TOP,
162 (DataMatcher<Top>) dataBefore -> Objects.equals(top(topFoo), dataBefore),
164 var expList = new HashSet<>(top(topBar, topFoo).nonnullTopLevelList().values());
165 var actualList = dataAfter.nonnullTopLevelList().values().stream()
166 .map(list -> new TopLevelListBuilder(list).build()).collect(Collectors.toSet());
167 return expList.equals(actualList);