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