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;
22 import akka.actor.ActorRef;
23 import akka.testkit.TestActorRef;
24 import org.junit.After;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
28 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
29 import org.opendaylight.controller.cluster.raft.TestActorFactory;
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 {
39 private final TestActorFactory actorFactory = new TestActorFactory(getSystem());
42 private DataTreeChangeListenerSupport support;
46 shard = createShard();
47 support = new DataTreeChangeListenerSupport(shard);
52 public void tearDown() {
58 public void testChangeListenerWithNoInitialData() throws Exception {
59 MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 0, true);
61 listener.expectNoMoreChanges("Unexpected initial change event");
65 public void testInitialChangeListenerEventWithContainerPath() throws Exception {
66 writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
68 MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 1, true);
70 listener.waitForChangeEvents();
71 listener.verifyNotifiedData(TEST_PATH);
75 public void testInitialChangeListenerEventWithListPath() throws Exception {
76 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
78 MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH, 1, true);
80 listener.waitForChangeEvents();
81 listener.verifyNotifiedData(OUTER_LIST_PATH);
85 public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
86 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
88 MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), 2, true);
90 listener.waitForChangeEvents();
91 listener.verifyNotifiedData(outerEntryPath(1), outerEntryPath(2));
95 public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
96 mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(
97 outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
99 MockDataTreeChangeListener listener = registerChangeListener(
100 OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 4, true);
102 listener.waitForChangeEvents();
103 listener.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"), innerEntryPath(2, "three"),
104 innerEntryPath(2, "four"));
106 // Register for a specific outer list entry
108 MockDataTreeChangeListener listener2 = registerChangeListener(
109 OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 2, true);
111 listener2.waitForChangeEvents();
112 listener2.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"));
113 listener2.verifyNoNotifiedData(innerEntryPath(2, "three"), innerEntryPath(2, "four"));
116 private MockDataTreeChangeListener registerChangeListener(YangInstanceIdentifier path,
117 int expectedEvents, boolean isLeader) {
118 MockDataTreeChangeListener listener = new MockDataTreeChangeListener(expectedEvents);
119 ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener));
120 support.onMessage(new RegisterDataTreeChangeListener(path, dclActor, false), isLeader, true);
124 private Shard createShard() {
125 TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps());
126 return actor.underlyingActor();