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

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