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;
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;
36 * Unit tests for DataChangeListenerSupport.
38 * @author Thomas Pantelis
40 public class DataChangeListenerSupportTest extends AbstractShardTest {
43 public void testChangeListenerWithNoInitialData() throws Exception {
45 new ShardTestKit(getSystem()) {
47 final TestActorRef<Shard> actor = actorFactory.createTestActor(
48 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
49 "testChangeListenerWithNoInitialData");
51 waitUntilLeader(actor);
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);
60 listener.expectNoMoreChanges("Unexpected initial change event");
66 public void testInitialChangeListenerEventWithContainerPath() throws Exception {
68 new ShardTestKit(getSystem()) {
70 final TestActorRef<Shard> actor = actorFactory.createTestActor(
71 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
72 "testInitialChangeListenerEventWithContainerPath");
74 waitUntilLeader(actor);
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);
85 listener.waitForChangeEvents(TEST_PATH);
91 public void testInitialChangeListenerEventWithListPath() throws Exception {
92 new ShardTestKit(getSystem()) {
94 final TestActorRef<Shard> actor = actorFactory.createTestActor(
95 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
96 "testInitialChangeListenerEventWithListPath");
98 waitUntilLeader(actor);
100 final Shard shard = actor.underlyingActor();
101 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
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),
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());
120 public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
122 new ShardTestKit(getSystem()) {
124 final TestActorRef<Shard> actor = actorFactory.createTestActor(
125 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
126 "testInitialChangeListenerEventWithWildcardedListPath");
128 waitUntilLeader(actor);
130 final Shard shard = actor.underlyingActor();
132 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
133 writeToStore(shard.getDataStore(), OUTER_CONTAINER_PATH,
134 ImmutableNodes.containerNode(OUTER_CONTAINER_QNAME));
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);
143 listener.waitForChangeEvents();
144 listener.verifyCreatedData(0, outerEntryPath(1));
145 listener.verifyCreatedData(0, outerEntryPath(2));
146 listener.verifyNoCreatedData(0, OUTER_CONTAINER_PATH);
152 public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
154 new ShardTestKit(getSystem()) {
156 final TestActorRef<Shard> actor = actorFactory.createTestActor(
157 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
158 "testInitialChangeListenerEventWithNestedWildcardedListsPath");
160 waitUntilLeader(actor);
162 final Shard shard = actor.underlyingActor();
164 mergeToStore(shard.getDataStore(), TEST_PATH,
165 testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")),
166 outerNodeEntry(2, innerNode("three", "four")))));
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);
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"));
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);
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"));
201 public void testInitialChangeListenerEventWhenNotInitiallyLeader() throws Exception {
203 new ShardTestKit(getSystem()) {
205 final TestActorRef<Shard> actor = actorFactory.createTestActor(
206 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
207 "testInitialChangeListenerEventWhenNotInitiallyLeader");
209 waitUntilLeader(actor);
211 final Shard shard = actor.underlyingActor();
213 mergeToStore(shard.getDataStore(), TEST_PATH,
214 testNodeWithOuter(outerNode(outerNodeEntry(1, innerNode("one", "two")),
215 outerNodeEntry(2, innerNode("three", "four")))));
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);
225 listener.expectNoMoreChanges("Unexpected initial change event");
228 support.onLeadershipChange(true, true);
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"));