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