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.opendaylight.controller.md.cluster.datastore.model.TestModel.INNER_LIST_QNAME;
11 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_LIST_PATH;
12 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.OUTER_LIST_QNAME;
13 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_PATH;
14 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.TEST_QNAME;
15 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerEntryPath;
16 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.innerNode;
17 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerEntryKey;
18 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerEntryPath;
19 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNode;
20 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.outerNodeEntry;
21 import static org.opendaylight.controller.md.cluster.datastore.model.TestModel.testNodeWithOuter;
23 import akka.actor.ActorRef;
24 import akka.testkit.TestActorRef;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
29 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 * Unit tests for DataTreeChangeListenerSupport.
36 * @author Thomas Pantelis
38 public class DataTreeChangeListenerSupportTest extends AbstractShardTest {
40 private DataTreeChangeListenerSupport support;
44 shard = createShard();
45 support = new DataTreeChangeListenerSupport(shard);
50 public void tearDown() {
56 public void testChangeListenerWithNoInitialData() throws Exception {
57 MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 0, true);
59 listener.expectNoMoreChanges("Unexpected initial change event");
63 public void testInitialChangeListenerEventWithContainerPath() throws Exception {
64 writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
66 MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 1, true);
68 listener.waitForChangeEvents();
69 listener.verifyNotifiedData(TEST_PATH);
73 public void testInitialChangeListenerEventWithListPath() throws Exception {
74 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
76 MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH, 1, true);
78 listener.waitForChangeEvents();
79 listener.verifyNotifiedData(OUTER_LIST_PATH);
83 public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
84 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
86 MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), 2, true);
88 listener.waitForChangeEvents();
89 listener.verifyNotifiedData(outerEntryPath(1), outerEntryPath(2));
93 public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
94 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(
95 outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
97 MockDataTreeChangeListener listener = registerChangeListener(
98 OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 4, true);
100 listener.waitForChangeEvents();
101 listener.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"), innerEntryPath(2, "three"),
102 innerEntryPath(2, "four"));
104 // Register for a specific outer list entry
106 MockDataTreeChangeListener listener2 = registerChangeListener(
107 OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 2, true);
109 listener2.waitForChangeEvents();
110 listener2.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"));
111 listener2.verifyNoNotifiedData(innerEntryPath(2, "three"), innerEntryPath(2, "four"));
114 private MockDataTreeChangeListener registerChangeListener(final YangInstanceIdentifier path,
115 final int expectedEvents, final boolean isLeader) {
116 MockDataTreeChangeListener listener = new MockDataTreeChangeListener(expectedEvents);
117 ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener));
118 support.onMessage(new RegisterDataTreeChangeListener(path, dclActor, false), isLeader, true);
122 private Shard createShard() {
123 TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps());
124 ShardTestKit.waitUntilLeader(actor);
125 return actor.underlyingActor();