BUG-2138: Create DistributedShardFrontend
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerRegistrationTest.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import static org.junit.Assert.assertEquals;
12
13 import akka.actor.ActorRef;
14 import akka.actor.Props;
15 import akka.testkit.JavaTestKit;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
19 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistrationReply;
20 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
22 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
23 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26
27 public class DataChangeListenerRegistrationTest extends AbstractActorTest {
28     private static final InMemoryDOMDataStore STORE = new InMemoryDOMDataStore("OPER",
29             MoreExecutors.newDirectExecutorService());
30
31     static {
32         STORE.onGlobalContextUpdated(TestModel.createTestContext());
33     }
34
35     @Test
36     public void testOnReceiveCloseListenerRegistration() throws Exception {
37         new JavaTestKit(getSystem()) {
38             {
39                 final Props props = DataChangeListenerRegistrationActor.props(STORE.registerChangeListener(
40                         TestModel.TEST_PATH, noOpDataChangeListener(), AsyncDataBroker.DataChangeScope.BASE));
41                 final ActorRef subject = getSystem().actorOf(props, "testCloseListenerRegistration");
42
43                 new Within(duration("1 seconds")) {
44                     @Override
45                     protected void run() {
46
47                         subject.tell(CloseDataChangeListenerRegistration.INSTANCE, getRef());
48
49                         final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
50                             // do not put code outside this method, will run
51                             // afterwards
52                             @Override
53                             protected String match(final Object in) {
54                                 if (in.getClass().equals(CloseDataChangeListenerRegistrationReply.class)) {
55                                     return "match";
56                                 } else {
57                                     throw noMatch();
58                                 }
59                             }
60                         }.get(); // this extracts the received message
61
62                         assertEquals("match", out);
63
64                         expectNoMsg();
65                     }
66
67                 };
68             }
69         };
70     }
71
72     private static AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener() {
73         return change -> {
74         };
75     }
76 }