Limit visibility of ShardDataTree methods
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerSupportTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.cluster.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.INNER_LIST_QNAME;
12 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_CONTAINER_PATH;
13 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_CONTAINER_QNAME;
14 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_LIST_PATH;
15 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_LIST_QNAME;
16 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_PATH;
17 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerEntryPath;
18 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
19 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerEntryKey;
20 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerEntryPath;
21 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
22 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
23 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.testNodeWithOuter;
24 import akka.actor.ActorRef;
25 import akka.dispatch.Dispatchers;
26 import akka.testkit.TestActorRef;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
29 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
30 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
31 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34
35 /**
36  * Unit tests for DataChangeListenerSupport.
37  *
38  * @author Thomas Pantelis
39  */
40 public class DataChangeListenerSupportTest extends AbstractShardTest {
41
42     @Test
43     public void testChangeListenerWithNoInitialData() throws Exception {
44
45         new ShardTestKit(getSystem()) {
46             {
47                 final TestActorRef<Shard> actor = actorFactory.createTestActor(
48                         newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
49                         "testChangeListenerWithNoInitialData");
50
51                 waitUntilLeader(actor);
52
53                 final Shard shard = actor.underlyingActor();
54                 final MockDataChangeListener listener = new MockDataChangeListener(0);
55                 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
56                         "testChangeListenerWithNoInitialData-DataChangeListener");
57                 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
58                 support.onMessage(new RegisterChangeListener(TEST_PATH, dclActor, DataChangeScope.ONE, false),true,true);
59
60                 listener.expectNoMoreChanges("Unexpected initial change event");
61             }
62         };
63     }
64
65     @Test
66     public void testInitialChangeListenerEventWithContainerPath() throws Exception {
67
68         new ShardTestKit(getSystem()) {
69             {
70                 final TestActorRef<Shard> actor = actorFactory.createTestActor(
71                         newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
72                         "testInitialChangeListenerEventWithContainerPath");
73
74                 waitUntilLeader(actor);
75
76                 final Shard shard = actor.underlyingActor();
77                 writeToStore(shard.getDataStore(), TestModel.TEST_PATH,
78                         ImmutableNodes.containerNode(TestModel.TEST_QNAME));
79                 final MockDataChangeListener listener = new MockDataChangeListener(1);
80                 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
81                         "testInitialChangeListenerEventWithContainerPath-DataChangeListener");
82                 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
83                 support.onMessage(new RegisterChangeListener(TEST_PATH, dclActor, DataChangeScope.ONE, false),true,true);
84
85                 listener.waitForChangeEvents(TEST_PATH);
86             }
87         };
88     }
89
90     @Test
91     public void testInitialChangeListenerEventWithListPath() throws Exception {
92         new ShardTestKit(getSystem()) {
93             {
94                 final TestActorRef<Shard> actor = actorFactory.createTestActor(
95                         newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
96                         "testInitialChangeListenerEventWithListPath");
97
98                 waitUntilLeader(actor);
99
100                 final Shard shard = actor.underlyingActor();
101                 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
102
103                 final MockDataChangeListener listener = new MockDataChangeListener(1);
104                 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
105                         "testInitialChangeListenerEventWithListPath-DataChangeListener");
106                 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
107                 support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH, dclActor, DataChangeScope.ONE, false),
108                         true, true);
109
110                 listener.waitForChangeEvents();
111                 assertEquals("Outer entry 1 present", true, NormalizedNodes
112                         .findNode(listener.getCreatedData(0, OUTER_LIST_PATH), outerEntryKey(1)).isPresent());
113                 assertEquals("Outer entry 2 present", true, NormalizedNodes
114                         .findNode(listener.getCreatedData(0, OUTER_LIST_PATH), outerEntryKey(2)).isPresent());
115             }
116         };
117     }
118
119     @Test
120     public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
121
122         new ShardTestKit(getSystem()) {
123             {
124                 final TestActorRef<Shard> actor = actorFactory.createTestActor(
125                         newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
126                         "testInitialChangeListenerEventWithWildcardedListPath");
127
128                 waitUntilLeader(actor);
129
130                 final Shard shard = actor.underlyingActor();
131
132                 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
133                 writeToStore(shard.getDataStore(), OUTER_CONTAINER_PATH,
134                         ImmutableNodes.containerNode(OUTER_CONTAINER_QNAME));
135
136                 final MockDataChangeListener listener = new MockDataChangeListener(1);
137                 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
138                         "testInitialChangeListenerEventWithWildcardedListPath-DataChangeListener");
139                 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
140                 support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), dclActor,
141                         DataChangeScope.ONE, false), true, true);
142
143                 listener.waitForChangeEvents();
144                 listener.verifyCreatedData(0, outerEntryPath(1));
145                 listener.verifyCreatedData(0, outerEntryPath(2));
146                 listener.verifyNoCreatedData(0, OUTER_CONTAINER_PATH);
147             }
148         };
149     }
150
151     @Test
152     public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
153
154         new ShardTestKit(getSystem()) {
155             {
156                 final TestActorRef<Shard> actor = actorFactory.createTestActor(
157                         newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
158                         "testInitialChangeListenerEventWithNestedWildcardedListsPath");
159
160                 waitUntilLeader(actor);
161
162                 final Shard shard = actor.underlyingActor();
163
164                 mergeToStore(shard.getDataStore(), TEST_PATH,
165                         testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")),
166                                 outerNodeEntry(2, innerNode("three", "four")))));
167
168                 final MockDataChangeListener listener = new MockDataChangeListener(1);
169                 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
170                         "testInitialChangeListenerEventWithNestedWildcardedListsPath-DataChangeListener");
171                 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
172                 support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), dclActor,
173                         DataChangeScope.ONE, false), true, true);
174
175
176                 listener.waitForChangeEvents();
177                 listener.verifyCreatedData(0, innerEntryPath(1, "one"));
178                 listener.verifyCreatedData(0, innerEntryPath(1, "two"));
179                 listener.verifyCreatedData(0, innerEntryPath(2, "three"));
180                 listener.verifyCreatedData(0, innerEntryPath(2, "four"));
181
182                 // Register for a specific outer list entry
183                 final MockDataChangeListener listener2 = new MockDataChangeListener(1);
184                 final ActorRef dclActor2 = actorFactory.createActor(DataChangeListener.props(listener2),
185                         "testInitialChangeListenerEventWithNestedWildcardedListsPath-DataChangeListener2");
186                 final DataChangeListenerSupport support2 = new DataChangeListenerSupport(shard);
187                 support2.onMessage(new RegisterChangeListener(
188                         OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), dclActor2,
189                         DataChangeScope.ONE, false), true, true);
190
191                 listener2.waitForChangeEvents();
192                 listener2.verifyCreatedData(0, innerEntryPath(1, "one"));
193                 listener2.verifyCreatedData(0, innerEntryPath(1, "two"));
194                 listener2.verifyNoCreatedData(0, innerEntryPath(2, "three"));
195                 listener2.verifyNoCreatedData(0, innerEntryPath(2, "four"));
196             }
197         };
198     }
199
200     @Test
201     public void testInitialChangeListenerEventWhenNotInitiallyLeader() throws Exception {
202
203         new ShardTestKit(getSystem()) {
204             {
205                 final TestActorRef<Shard> actor = actorFactory.createTestActor(
206                         newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
207                         "testInitialChangeListenerEventWhenNotInitiallyLeader");
208
209                 waitUntilLeader(actor);
210
211                 final Shard shard = actor.underlyingActor();
212
213                 mergeToStore(shard.getDataStore(), TEST_PATH,
214                         testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")),
215                                 outerNodeEntry(2, innerNode("three", "four")))));
216
217                 final MockDataChangeListener listener = new MockDataChangeListener(0);
218                 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
219                         "testInitialChangeListenerEventWhenNotInitiallyLeader-DataChangeListener");
220                 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
221                 support.onMessage(new RegisterChangeListener(
222                         OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), dclActor,
223                         DataChangeScope.ONE, false), false, true);
224
225                 listener.expectNoMoreChanges("Unexpected initial change event");
226                 listener.reset(1);
227
228                 support.onLeadershipChange(true, true);
229
230                 listener.waitForChangeEvents();
231                 listener.verifyCreatedData(0, innerEntryPath(1, "one"));
232                 listener.verifyCreatedData(0, innerEntryPath(1, "two"));
233                 listener.verifyCreatedData(0, innerEntryPath(2, "three"));
234                 listener.verifyCreatedData(0, innerEntryPath(2, "four"));
235             }
236         };
237
238     }
239 }