1426e4a243f4a35cdc4de136581ee1f1a26a7d45
[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.actor.ActorSelection;
25 import akka.pattern.Patterns;
26 import akka.testkit.JavaTestKit;
27 import akka.testkit.TestActorRef;
28 import akka.util.Timeout;
29 import com.google.common.base.Throwables;
30 import java.util.AbstractMap.SimpleEntry;
31 import java.util.Map.Entry;
32 import java.util.concurrent.TimeUnit;
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistration;
37 import org.opendaylight.controller.cluster.datastore.messages.CloseDataTreeNotificationListenerRegistrationReply;
38 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListener;
39 import org.opendaylight.controller.cluster.datastore.messages.RegisterDataTreeChangeListenerReply;
40 import org.opendaylight.controller.cluster.datastore.utils.MockDataTreeChangeListener;
41 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
44 import scala.concurrent.Await;
45 import scala.concurrent.duration.Duration;
46
47 /**
48  * Unit tests for DataTreeChangeListenerSupport.
49  *
50  * @author Thomas Pantelis
51  */
52 public class DataTreeChangeListenerSupportTest extends AbstractShardTest {
53     private Shard shard;
54     private TestActorRef<Shard> shardActor;
55
56     @Override
57     @Before
58     public void setUp() {
59         super.setUp();
60         createShard();
61     }
62
63     @Override
64     @After
65     public void tearDown() {
66         super.tearDown();
67         actorFactory.close();
68     }
69
70     @Test
71     public void testChangeListenerWithNoInitialData() throws Exception {
72         MockDataTreeChangeListener listener = registerChangeListener(TEST_PATH, 0).getKey();
73
74         listener.expectNoMoreChanges("Unexpected initial change event");
75     }
76
77     @Test
78     public void testInitialChangeListenerEventWithContainerPath() throws Exception {
79         writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
80
81         Entry<MockDataTreeChangeListener, ActorSelection> entry = registerChangeListener(TEST_PATH, 1);
82         MockDataTreeChangeListener listener = entry.getKey();
83
84         listener.waitForChangeEvents();
85         listener.verifyNotifiedData(TEST_PATH);
86
87         listener.reset(1);
88
89         writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
90         listener.waitForChangeEvents();
91         listener.verifyNotifiedData(TEST_PATH);
92
93         listener.reset(1);
94         JavaTestKit kit = new JavaTestKit(getSystem());
95         entry.getValue().tell(CloseDataTreeNotificationListenerRegistration.getInstance(), kit.getRef());
96         kit.expectMsgClass(JavaTestKit.duration("5 seconds"), CloseDataTreeNotificationListenerRegistrationReply.class);
97
98         writeToStore(shard.getDataStore(), TEST_PATH, ImmutableNodes.containerNode(TEST_QNAME));
99         listener.verifyNoNotifiedData(TEST_PATH);
100     }
101
102     @Test
103     public void testInitialChangeListenerEventWithListPath() throws Exception {
104         mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
105
106         MockDataTreeChangeListener listener = registerChangeListener(OUTER_LIST_PATH, 1).getKey();
107
108         listener.waitForChangeEvents();
109         listener.verifyNotifiedData(OUTER_LIST_PATH);
110     }
111
112     @Test
113     public void testInitialChangeListenerEventWithWildcardedListPath() throws Exception {
114         mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(1, 2));
115
116         MockDataTreeChangeListener listener =
117                 registerChangeListener(OUTER_LIST_PATH.node(OUTER_LIST_QNAME), 1).getKey();
118
119         listener.waitForChangeEvents();
120         listener.verifyNotifiedData(outerEntryPath(1), outerEntryPath(2));
121     }
122
123     @Test
124     public void testInitialChangeListenerEventWithNestedWildcardedListsPath() throws Exception {
125         mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(
126                 outerNodeEntry(1, innerNode("one", "two")), outerNodeEntry(2, innerNode("three", "four")))));
127
128         MockDataTreeChangeListener listener = registerChangeListener(
129                 OUTER_LIST_PATH.node(OUTER_LIST_QNAME).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 1).getKey();
130
131         listener.waitForChangeEvents();
132         listener.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"), innerEntryPath(2, "three"),
133                 innerEntryPath(2, "four"));
134
135         // Register for a specific outer list entry
136
137         MockDataTreeChangeListener listener2 = registerChangeListener(
138                 OUTER_LIST_PATH.node(outerEntryKey(1)).node(INNER_LIST_QNAME).node(INNER_LIST_QNAME), 1).getKey();
139
140         listener2.waitForChangeEvents();
141         listener2.verifyNotifiedData(innerEntryPath(1, "one"), innerEntryPath(1, "two"));
142         listener2.verifyNoNotifiedData(innerEntryPath(2, "three"), innerEntryPath(2, "four"));
143
144         listener.reset(1);
145         listener2.reset(1);
146
147         mergeToStore(shard.getDataStore(), TEST_PATH, testNodeWithOuter(outerNode(
148                 outerNodeEntry(1, innerNode("three")))));
149
150         listener.waitForChangeEvents();
151         listener.verifyNotifiedData(innerEntryPath(1, "three"));
152
153         listener2.waitForChangeEvents();
154         listener2.verifyNotifiedData(innerEntryPath(1, "three"));
155     }
156
157     @SuppressWarnings("checkstyle:IllegalCatch")
158     private Entry<MockDataTreeChangeListener, ActorSelection> registerChangeListener(final YangInstanceIdentifier path,
159             final int expectedEvents) {
160         MockDataTreeChangeListener listener = new MockDataTreeChangeListener(expectedEvents);
161         ActorRef dclActor = actorFactory.createActor(DataTreeChangeListenerActor.props(listener, TestModel.TEST_PATH));
162
163         try {
164             RegisterDataTreeChangeListenerReply reply = (RegisterDataTreeChangeListenerReply)
165                 Await.result(Patterns.ask(shardActor, new RegisterDataTreeChangeListener(path, dclActor, false),
166                     new Timeout(5, TimeUnit.SECONDS)), Duration.create(5, TimeUnit.SECONDS));
167             return new SimpleEntry<>(listener, getSystem().actorSelection(reply.getListenerRegistrationPath()));
168
169         } catch (Exception e) {
170             Throwables.propagate(e);
171             return null;
172         }
173     }
174
175     private void createShard() {
176         shardActor = actorFactory.createTestActor(newShardProps());
177         ShardTestKit.waitUntilLeader(shardActor);
178         shard = shardActor.underlyingActor();
179     }
180 }