2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
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;
25 import akka.actor.ActorRef;
26 import akka.dispatch.Dispatchers;
27 import akka.testkit.TestActorRef;
28 import org.junit.Test;
29 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListener;
30 import org.opendaylight.controller.cluster.datastore.utils.MockDataChangeListener;
31 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
32 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
37 * Unit tests for DataChangeListenerSupport.
39 * @author Thomas Pantelis
41 public class DataChangeListenerSupportTest extends AbstractShardTest {
44 public void testChangeListenerWithNoInitialData() throws Exception {
46 new ShardTestKit(getSystem()) {
48 final TestActorRef<Shard> actor = actorFactory.createTestActor(
49 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
50 "testChangeListenerWithNoInitialData");
52 waitUntilLeader(actor);
54 final Shard shard = actor.underlyingActor();
55 final MockDataChangeListener listener = new MockDataChangeListener(0);
56 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
57 "testChangeListenerWithNoInitialData-DataChangeListener");
58 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
59 support.onMessage(new RegisterChangeListener(TEST_PATH, dclActor, DataChangeScope.ONE, false),
62 listener.expectNoMoreChanges("Unexpected initial change event");
68 public void testInitialChangeListenerEventWithContainerPath() throws Exception {
70 new ShardTestKit(getSystem()) {
72 final TestActorRef<Shard> actor = actorFactory.createTestActor(
73 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
74 "testInitialChangeListenerEventWithContainerPath");
76 waitUntilLeader(actor);
78 final Shard shard = actor.underlyingActor();
79 writeToStore(shard.getDataStore(), TestModel.TEST_PATH,
80 ImmutableNodes.containerNode(TestModel.TEST_QNAME));
81 final MockDataChangeListener listener = new MockDataChangeListener(1);
82 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
83 "testInitialChangeListenerEventWithContainerPath-DataChangeListener");
84 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
85 support.onMessage(new RegisterChangeListener(TEST_PATH, dclActor, DataChangeScope.ONE, false),
88 listener.waitForChangeEvents(TEST_PATH);
94 public void testInitialChangeListenerEventWithListPath() throws Exception {
95 new ShardTestKit(getSystem()) {
97 final TestActorRef<Shard> actor = actorFactory.createTestActor(
98 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
99 "testInitialChangeListenerEventWithListPath");
101 waitUntilLeader(actor);
103 final Shard shard = actor.underlyingActor();
104 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
106 final MockDataChangeListener listener = new MockDataChangeListener(1);
107 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
108 "testInitialChangeListenerEventWithListPath-DataChangeListener");
109 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
110 support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH, dclActor, DataChangeScope.ONE, false),
113 listener.waitForChangeEvents();
114 assertEquals("Outer entry 1 present", true, NormalizedNodes
115 .findNode(listener.getCreatedData(0, OUTER_LIST_PATH), outerEntryKey(1)).isPresent());
116 assertEquals("Outer entry 2 present", true, NormalizedNodes
117 .findNode(listener.getCreatedData(0, OUTER_LIST_PATH), outerEntryKey(2)).isPresent());
123 public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
125 new ShardTestKit(getSystem()) {
127 final TestActorRef<Shard> actor = actorFactory.createTestActor(
128 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
129 "testInitialChangeListenerEventWithWildcardedListPath");
131 waitUntilLeader(actor);
133 final Shard shard = actor.underlyingActor();
135 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
136 writeToStore(shard.getDataStore(), OUTER_CONTAINER_PATH,
137 ImmutableNodes.containerNode(OUTER_CONTAINER_QNAME));
139 final MockDataChangeListener listener = new MockDataChangeListener(1);
140 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
141 "testInitialChangeListenerEventWithWildcardedListPath-DataChangeListener");
142 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
143 support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), dclActor,
144 DataChangeScope.ONE, false), true, true);
146 listener.waitForChangeEvents();
147 listener.verifyCreatedData(0, outerEntryPath(1));
148 listener.verifyCreatedData(0, outerEntryPath(2));
149 listener.verifyNoCreatedData(0, OUTER_CONTAINER_PATH);
155 public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
157 new ShardTestKit(getSystem()) {
159 final TestActorRef<Shard> actor = actorFactory.createTestActor(
160 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
161 "testInitialChangeListenerEventWithNestedWildcardedListsPath");
163 waitUntilLeader(actor);
165 final Shard shard = actor.underlyingActor();
167 mergeToStore(shard.getDataStore(), TEST_PATH,
168 testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")),
169 outerNodeEntry(2, innerNode("three", "four")))));
171 final MockDataChangeListener listener = new MockDataChangeListener(1);
172 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
173 "testInitialChangeListenerEventWithNestedWildcardedListsPath-DataChangeListener");
174 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
175 support.onMessage(new RegisterChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME)
176 .node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), dclActor, DataChangeScope.ONE, false),
180 listener.waitForChangeEvents();
181 listener.verifyCreatedData(0, innerEntryPath(1, "one"));
182 listener.verifyCreatedData(0, innerEntryPath(1, "two"));
183 listener.verifyCreatedData(0, innerEntryPath(2, "three"));
184 listener.verifyCreatedData(0, innerEntryPath(2, "four"));
186 // Register for a specific outer list entry
187 final MockDataChangeListener listener2 = new MockDataChangeListener(1);
188 final ActorRef dclActor2 = actorFactory.createActor(DataChangeListener.props(listener2),
189 "testInitialChangeListenerEventWithNestedWildcardedListsPath-DataChangeListener2");
190 final DataChangeListenerSupport support2 = new DataChangeListenerSupport(shard);
191 support2.onMessage(new RegisterChangeListener(
192 OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), dclActor2,
193 DataChangeScope.ONE, false), true, true);
195 listener2.waitForChangeEvents();
196 listener2.verifyCreatedData(0, innerEntryPath(1, "one"));
197 listener2.verifyCreatedData(0, innerEntryPath(1, "two"));
198 listener2.verifyNoCreatedData(0, innerEntryPath(2, "three"));
199 listener2.verifyNoCreatedData(0, innerEntryPath(2, "four"));
205 public void testInitialChangeListenerEventWhenNotInitiallyLeader() throws Exception {
207 new ShardTestKit(getSystem()) {
209 final TestActorRef<Shard> actor = actorFactory.createTestActor(
210 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
211 "testInitialChangeListenerEventWhenNotInitiallyLeader");
213 waitUntilLeader(actor);
215 final Shard shard = actor.underlyingActor();
217 mergeToStore(shard.getDataStore(), TEST_PATH,
218 testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")),
219 outerNodeEntry(2, innerNode("three", "four")))));
221 final MockDataChangeListener listener = new MockDataChangeListener(0);
222 final ActorRef dclActor = actorFactory.createActor(DataChangeListener.props(listener),
223 "testInitialChangeListenerEventWhenNotInitiallyLeader-DataChangeListener");
224 final DataChangeListenerSupport support = new DataChangeListenerSupport(shard);
225 support.onMessage(new RegisterChangeListener(
226 OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), dclActor,
227 DataChangeScope.ONE, false), false, true);
229 listener.expectNoMoreChanges("Unexpected initial change event");
232 support.onLeadershipChange(true, true);
234 listener.waitForChangeEvents();
235 listener.verifyCreatedData(0, innerEntryPath(1, "one"));
236 listener.verifyCreatedData(0, innerEntryPath(1, "two"));
237 listener.verifyCreatedData(0, innerEntryPath(2, "three"));
238 listener.verifyCreatedData(0, innerEntryPath(2, "four"));