Correct ActionProviderService method definition
[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.function.Function;
21 import java.util.stream.Collectors;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
25 import org.opendaylight.mdsal.binding.api.DataTreeModification;
26 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.mdsal.binding.api.WriteTransaction;
28 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataTreeChangeListenerTest;
29 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
35
36 /**
37  * This testsuite tests explanation for data change scope and data modifications which were described in
38  * https://lists.opendaylight.org/pipermail/controller-dev/2014-July/005541.html.
39  */
40 public class ListInsertionDataChangeListenerTest extends AbstractDataTreeChangeListenerTest {
41     private static final InstanceIdentifier<Top> TOP = InstanceIdentifier.create(Top.class);
42     private static final InstanceIdentifier<TopLevelList> WILDCARDED = TOP.child(TopLevelList.class);
43     private static final InstanceIdentifier<TopLevelList> TOP_FOO = TOP.child(TopLevelList.class, TOP_FOO_KEY);
44     private static final InstanceIdentifier<TopLevelList> TOP_BAR = TOP.child(TopLevelList.class, TOP_BAR_KEY);
45
46     @Override
47     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
48         return ImmutableSet.of(BindingReflections.getModuleInfo(Top.class));
49     }
50
51     @Before
52     public void setupWithDataBroker() {
53         WriteTransaction initialTx = getDataBroker().newWriteOnlyTransaction();
54         initialTx.put(CONFIGURATION, TOP, top(topLevelList(TOP_FOO_KEY)));
55         assertCommit(initialTx.commit());
56     }
57
58     @Test
59     public void replaceTopNodeSubtreeListeners() {
60         final TopLevelList topBar = topLevelList(TOP_BAR_KEY);
61         final Top top = top(topBar);
62         final TopLevelList topFoo = topLevelList(TOP_FOO_KEY);
63
64         // Listener for TOP element
65         final TestListener<Top> topListener = createListener(CONFIGURATION, TOP,
66                 added(TOP, top(topLevelList(TOP_FOO_KEY))), replaced(TOP, top(topFoo), top));
67
68         // Listener for all list items. This one should see Foo item deleted and Bar item added.
69         final TestListener<TopLevelList> allListener = createListener(CONFIGURATION, WILDCARDED,
70                 added(TOP_FOO, topFoo), added(TOP_BAR, topBar), deleted(TOP_FOO, topFoo));
71
72         // Listener for all Foo item. This one should see only Foo item deleted.
73         final TestListener<TopLevelList> fooListener = createListener(CONFIGURATION, TOP_FOO,
74                 added(TOP_FOO, topFoo), deleted(TOP_FOO, topFoo));
75
76         // Listener for bar list items.
77         final TestListener<TopLevelList> barListener = createListener(CONFIGURATION, TOP_BAR,
78                 added(TOP_BAR, topBar));
79
80         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
81         writeTx.put(CONFIGURATION, TOP, top);
82         assertCommit(writeTx.commit());
83
84         topListener.verify();
85         allListener.verify();
86         fooListener.verify();
87         barListener.verify();
88     }
89
90     @Test
91     public void mergeTopNodeSubtreeListeners() {
92         final TopLevelList topBar = topLevelList(TOP_BAR_KEY);
93         final TopLevelList topFoo = topLevelList(TOP_FOO_KEY);
94
95         final TestListener<Top> topListener = createListener(CONFIGURATION, TOP,
96                 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
97         final TestListener<TopLevelList> allListener = createListener(CONFIGURATION, WILDCARDED,
98                 added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
99         final TestListener<TopLevelList> fooListener = createListener(CONFIGURATION, TOP_FOO,
100                 added(TOP_FOO, topFoo));
101         final TestListener<TopLevelList> barListener = createListener(CONFIGURATION, TOP_BAR,
102                 added(TOP_BAR, topBar));
103
104         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
105         writeTx.merge(CONFIGURATION, TOP, top(topLevelList(TOP_BAR_KEY)));
106         assertCommit(writeTx.commit());
107
108         topListener.verify();
109         allListener.verify();
110         fooListener.verify();
111         barListener.verify();
112     }
113
114     @Test
115     public void putTopBarNodeSubtreeListeners() {
116         final TopLevelList topBar = topLevelList(TOP_BAR_KEY);
117         final TopLevelList topFoo = topLevelList(TOP_FOO_KEY);
118
119         final TestListener<Top> topListener = createListener(CONFIGURATION, TOP,
120                 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
121         final TestListener<TopLevelList> allListener = createListener(CONFIGURATION, WILDCARDED,
122                 added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
123         final TestListener<TopLevelList> fooListener = createListener(CONFIGURATION, TOP_FOO,
124                 added(TOP_FOO, topFoo));
125         final TestListener<TopLevelList> barListener = createListener(CONFIGURATION, TOP_BAR,
126                 added(TOP_BAR, topBar));
127
128         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
129         writeTx.put(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
130         assertCommit(writeTx.commit());
131
132         topListener.verify();
133         allListener.verify();
134         fooListener.verify();
135         barListener.verify();
136     }
137
138     @Test
139     public void mergeTopBarNodeSubtreeListeners() {
140         final TopLevelList topBar = topLevelList(TOP_BAR_KEY);
141         final TopLevelList topFoo = topLevelList(TOP_FOO_KEY);
142
143         final TestListener<Top> topListener = createListener(CONFIGURATION, TOP,
144                 added(TOP, top(topLevelList(TOP_FOO_KEY))), topSubtreeModified(topFoo, topBar));
145         final TestListener<TopLevelList> allListener = createListener(CONFIGURATION, WILDCARDED,
146                 added(TOP_FOO, topFoo), added(TOP_BAR, topBar));
147         final TestListener<TopLevelList> fooListener = createListener(CONFIGURATION, TOP_FOO,
148                 added(TOP_FOO, topFoo));
149         final TestListener<TopLevelList> barListener = createListener(CONFIGURATION, TOP_BAR,
150                 added(TOP_BAR, topBar));
151
152         ReadWriteTransaction writeTx = getDataBroker().newReadWriteTransaction();
153         writeTx.merge(CONFIGURATION, TOP_BAR, topLevelList(TOP_BAR_KEY));
154         assertCommit(writeTx.commit());
155
156         topListener.verify();
157         allListener.verify();
158         fooListener.verify();
159         barListener.verify();
160     }
161
162     private static Function<DataTreeModification<Top>, Boolean> topSubtreeModified(final TopLevelList topFoo,
163             final TopLevelList topBar) {
164         return match(ModificationType.SUBTREE_MODIFIED, TOP,
165             (Function<Top, Boolean>) dataBefore -> Objects.equals(top(topFoo), dataBefore),
166             dataAfter -> {
167                 Set<TopLevelList> expList = new HashSet<>(top(topBar, topFoo).getTopLevelList().values());
168                 Set<TopLevelList> actualList = dataAfter.getTopLevelList().values().stream()
169                         .map(list -> new TopLevelListBuilder(list).build()).collect(Collectors.toSet());
170                 return expList.equals(actualList);
171             });
172     }
173 }