a69179eef6f3f5c485f7f79f4cd1c3eb7187f764
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataTreeChangeListenerSupportTest.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.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
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;
32
33 /**
34  * Unit tests for DataTreeChangeListenerSupport.
35  *
36  * @author Thomas Pantelis
37  */
38 public class DataTreeChangeListenerSupportTest extends AbstractShardTest {
39     private Shard shard;
40     private DataTreeChangeListenerSupport support;
41
42     @Before
43     public void setup() {
44         shard = createShard();
45         support = new DataTreeChangeListenerSupport(shard);
46     }
47
48     @Override
49     @After
50     public void tearDown() {
51         super.tearDown();
52         actorFactory.close();
53     }
54
55     @Test
56     public void testChangeListenerWithNoInitialData() throws Exception {
57         MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 0, true);
58
59         listener.expectNoMoreChanges("Unexpected initial change event");
60     }
61
62     @Test
63     public void testInitialChangeListenerEventWithContainerPath() throws Exception {
64         writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
65
66         MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 1, true);
67
68         listener.waitForChangeEvents();
69         listener.verifyNotifiedData(TEST_PATH);
70     }
71
72     @Test
73     public void testInitialChangeListenerEventWithListPath() throws Exception {
74         mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
75
76         MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH, 1, true);
77
78         listener.waitForChangeEvents();
79         listener.verifyNotifiedData(OUTER_LIST_PATH);
80     }
81
82     @Test
83     public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
84         mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
85
86         MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), 2, true);
87
88         listener.waitForChangeEvents();
89         listener.verifyNotifiedData(outerEntryPath(1), outerEntryPath(2));
90     }
91
92     @Test
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")))));
96
97         MockDataTreeChangeListener listener = registerChangeListener(
98                 OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 4, true);
99
100         listener.waitForChangeEvents();
101         listener.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"), innerEntryPath(2, "three"),
102                 innerEntryPath(2, "four"));
103
104         // Register for a specific outer list entry
105
106         MockDataTreeChangeListener listener2 = registerChangeListener(
107                 OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 2, true);
108
109         listener2.waitForChangeEvents();
110         listener2.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"));
111         listener2.verifyNoNotifiedData(innerEntryPath(2, "three"), innerEntryPath(2, "four"));
112     }
113
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);
119         return listener;
120     }
121
122     private Shard createShard() {
123         TestActorRef<Shard> actor = actorFactory.createTestActor(newShardProps());
124         ShardTestKit.waitUntilLeader(actor);
125         return actor.underlyingActor();
126     }
127 }