DataBrokerTestModule: use AbstractDataBrokerTest without inheritance
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / test / DataTreeChangeListenerTest.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.mdsal.binding.dom.adapter.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_BAR_KEY;
15 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.TOP_FOO_KEY;
16 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.USES_ONE_KEY;
17 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.complexUsesAugment;
18 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.path;
19 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.top;
20 import static org.opendaylight.mdsal.binding.test.model.util.ListsBindingUtils.topLevelList;
21
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Iterables;
24 import com.google.common.util.concurrent.SettableFuture;
25 import java.util.Collection;
26 import java.util.Set;
27 import java.util.concurrent.TimeUnit;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.binding.api.DataObjectModification;
31 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
32 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
33 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
34 import org.opendaylight.mdsal.binding.api.DataTreeModification;
35 import org.opendaylight.mdsal.binding.api.WriteTransaction;
36 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMDataBrokerAdapter;
37 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.TwoLevelList;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
46 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
47
48 public class DataTreeChangeListenerTest extends AbstractDataBrokerTest {
49
50     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.create(Top.class);
51     private static final PathArgument TOP_ARGUMENT = TOP_PATH.getPathArguments().iterator().next();
52     private static final InstanceIdentifier<TopLevelList> FOO_PATH = path(TOP_FOO_KEY);
53     private static final PathArgument FOO_ARGUMENT = Iterables.getLast(FOO_PATH.getPathArguments());
54     private static final TopLevelList FOO_DATA = topLevelList(TOP_FOO_KEY, complexUsesAugment(USES_ONE_KEY));
55     private static final InstanceIdentifier<TopLevelList> BAR_PATH = path(TOP_BAR_KEY);
56     private static final PathArgument BAR_ARGUMENT = Iterables.getLast(BAR_PATH.getPathArguments());
57     private static final TopLevelList BAR_DATA = topLevelList(TOP_BAR_KEY);
58     private static final DataTreeIdentifier<Top> TOP_IDENTIFIER
59             = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, TOP_PATH);
60
61     private static final Top TOP_INITIAL_DATA = top(FOO_DATA);
62
63     private BindingDOMDataBrokerAdapter dataBrokerImpl;
64
65     private static final class EventCapturingListener<T extends DataObject> implements DataTreeChangeListener<T> {
66
67         private SettableFuture<Collection<DataTreeModification<T>>> futureChanges = SettableFuture.create();
68
69         @Override
70         public void onDataTreeChanged(final Collection<DataTreeModification<T>> changes) {
71             this.futureChanges.set(changes);
72
73         }
74
75         Collection<DataTreeModification<T>> nextEvent() throws Exception {
76             final Collection<DataTreeModification<T>> result = futureChanges.get(200,TimeUnit.MILLISECONDS);
77             futureChanges = SettableFuture.create();
78             return result;
79         }
80     }
81
82     @Override
83     protected Set<YangModuleInfo> getModuleInfos() throws Exception {
84         return ImmutableSet.of(
85                 BindingReflections.getModuleInfo(TwoLevelList.class),
86                 BindingReflections.getModuleInfo(TreeComplexUsesAugment.class)
87                 );
88     }
89
90     @Override
91     protected void setupWithDataBroker(final DataBroker dataBroker) {
92         dataBrokerImpl = (BindingDOMDataBrokerAdapter) dataBroker;
93     }
94
95     @Test
96     public void testTopLevelListener() throws Exception {
97         final EventCapturingListener<Top> listener = new EventCapturingListener<>();
98         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener);
99
100         createAndVerifyTop(listener);
101
102         putTx(BAR_PATH, BAR_DATA).commit().get();
103         final DataObjectModification<Top> afterBarPutEvent
104                 = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
105         verifyModification(afterBarPutEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
106         final DataObjectModification<TopLevelList> barPutMod = afterBarPutEvent.getModifiedChildListItem(
107                 TopLevelList.class, TOP_BAR_KEY);
108         assertNotNull(barPutMod);
109         verifyModification(barPutMod, BAR_ARGUMENT, ModificationType.WRITE);
110
111         deleteTx(BAR_PATH).commit().get();
112         final DataObjectModification<Top> afterBarDeleteEvent
113                 = Iterables.getOnlyElement(listener.nextEvent()).getRootNode();
114         verifyModification(afterBarDeleteEvent, TOP_ARGUMENT, ModificationType.SUBTREE_MODIFIED);
115         final DataObjectModification<TopLevelList> barDeleteMod = afterBarDeleteEvent.getModifiedChildListItem(
116                 TopLevelList.class, TOP_BAR_KEY);
117         verifyModification(barDeleteMod, BAR_ARGUMENT, ModificationType.DELETE);
118
119         dataBrokerImpl.registerDataTreeChangeListener(TOP_IDENTIFIER, listener).close();
120     }
121
122     @Test
123     public void testWildcardedListListener() throws Exception {
124         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
125         final DataTreeIdentifier<TopLevelList> wildcard = DataTreeIdentifier.create(
126                 LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
127         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
128
129         putTx(TOP_PATH, TOP_INITIAL_DATA).commit().get();
130
131         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
132         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
133         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
134
135         putTx(BAR_PATH, BAR_DATA).commit().get();
136         final DataTreeModification<TopLevelList> barWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
137         assertEquals(BAR_PATH, barWriteEvent.getRootPath().getRootIdentifier());
138         verifyModification(barWriteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.WRITE);
139
140         deleteTx(BAR_PATH).commit().get();
141         final DataTreeModification<TopLevelList> barDeleteEvent = Iterables.getOnlyElement(listener.nextEvent());
142         assertEquals(BAR_PATH, barDeleteEvent.getRootPath().getRootIdentifier());
143         verifyModification(barDeleteEvent.getRootNode(), BAR_ARGUMENT, ModificationType.DELETE);
144     }
145
146     @Test
147     public void testWildcardedListListenerWithPreexistingData() throws Exception {
148         putTx(TOP_PATH, TOP_INITIAL_DATA).commit().get();
149
150         final EventCapturingListener<TopLevelList> listener = new EventCapturingListener<>();
151         final DataTreeIdentifier<TopLevelList> wildcard = DataTreeIdentifier.create(
152                 LogicalDatastoreType.OPERATIONAL, TOP_PATH.child(TopLevelList.class));
153         dataBrokerImpl.registerDataTreeChangeListener(wildcard, listener);
154
155         final DataTreeModification<TopLevelList> fooWriteEvent = Iterables.getOnlyElement(listener.nextEvent());
156         assertEquals(FOO_PATH, fooWriteEvent.getRootPath().getRootIdentifier());
157         verifyModification(fooWriteEvent.getRootNode(), FOO_ARGUMENT, ModificationType.WRITE);
158     }
159
160     private void createAndVerifyTop(final EventCapturingListener<Top> listener) throws Exception {
161         putTx(TOP_PATH,TOP_INITIAL_DATA).commit().get();
162         final Collection<DataTreeModification<Top>> events = listener.nextEvent();
163
164         assertFalse("Non empty collection should be received.",events.isEmpty());
165         final DataTreeModification<Top> initialWrite = Iterables.getOnlyElement(events);
166         final DataObjectModification<? extends DataObject> initialNode = initialWrite.getRootNode();
167         verifyModification(initialNode,TOP_PATH.getPathArguments().iterator().next(),ModificationType.WRITE);
168         assertEquals(TOP_INITIAL_DATA, initialNode.getDataAfter());
169     }
170
171     private static void verifyModification(final DataObjectModification<? extends DataObject> barWrite,
172             final PathArgument pathArg, final ModificationType eventType) {
173         assertEquals(pathArg.getType(), barWrite.getDataType());
174         assertEquals(eventType,barWrite.getModificationType());
175         assertEquals(pathArg, barWrite.getIdentifier());
176     }
177
178     private <T extends DataObject> WriteTransaction putTx(final InstanceIdentifier<T> path, final T data) {
179         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
180         tx.put(LogicalDatastoreType.OPERATIONAL, path, data);
181         return tx;
182     }
183
184     private WriteTransaction deleteTx(final InstanceIdentifier<?> path) {
185         final WriteTransaction tx = dataBrokerImpl.newWriteOnlyTransaction();
186         tx.delete(LogicalDatastoreType.OPERATIONAL, path);
187         return tx;
188     }
189 }