Refactor AbstractDataTreeChangeListenerTest
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / 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;
9
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;
15
16 import com.google.common.collect.ImmutableSet;
17 import java.util.HashSet;
18 import java.util.Objects;
19 import java.util.Set;
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;
32
33 /**
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.
36  */
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);
42
43     @Override
44     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
45         return ImmutableSet.of(BindingReflections.getModuleInfo(Top.class));
46     }
47
48     @Before
49     public void setupWithDataBroker() {
50         WriteTransaction initialTx = getDataBroker().newWriteOnlyTransaction();
51         initialTx.put(CONFIGURATION, TOP, top(topLevelList(TOP_FOO_KEY)));
52         assertCommit(initialTx.commit());
53     }
54
55     @Test
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);
60
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());
72
73                         barListener.assertModifications(added(TOP_BAR, topBar));
74                     }
75                     fooListener.assertModifications(added(TOP_FOO, topFoo), deleted(TOP_FOO, topFoo));
76                 }
77                 allListener.assertModifications(
78                     added(TOP_FOO, topFoo),
79                     added(TOP_BAR, topBar),
80                     deleted(TOP_FOO, topFoo));
81             }
82             topListener.assertModifications(
83                 added(TOP, top(topLevelList(TOP_FOO_KEY))),
84                 replaced(TOP, top(topFoo), top));
85         }
86     }
87
88     @Test
89     public void mergeTopNodeSubtreeListeners() {
90         final var topBar = topLevelList(TOP_BAR_KEY);
91         final var topFoo = topLevelList(TOP_FOO_KEY);
92
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());
100
101                         barListener.assertModifications(added(TOP_BAR, topBar));
102                     }
103                     fooListener.assertModifications(added(TOP_FOO, topFoo));
104                 }
105                 allListener.assertModifications(added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
106             }
107             topListener.assertModifications(
108                 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
109         }
110     }
111
112     @Test
113     public void putTopBarNodeSubtreeListeners() {
114         final var topBar = topLevelList(TOP_BAR_KEY);
115         final var topFoo = topLevelList(TOP_FOO_KEY);
116
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());
124
125                         barListener.assertModifications(added(TOP_BAR, topBar));
126                     }
127                     fooListener.assertModifications(added(TOP_FOO, topFoo));
128                 }
129                 allListener.assertModifications(added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
130             }
131             topListener.assertModifications(
132                 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
133         }
134     }
135
136     @Test
137     public void mergeTopBarNodeSubtreeListeners() {
138         final var topBar = topLevelList(TOP_BAR_KEY);
139         final var topFoo = topLevelList(TOP_FOO_KEY);
140
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());
148
149                         barListener.assertModifications(added(TOP_BAR, topBar));
150                     }
151                     fooListener.assertModifications(added(TOP_FOO, topFoo));
152                 }
153                 allListener.assertModifications(added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
154             }
155             topListener.assertModifications(
156                 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
157         }
158     }
159
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),
163             dataAfter -> {
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);
168             });
169     }
170 }