Do not use MoreExecutors.sameThreadExecutor()
[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 import akka.actor.ActorRef;
13 import akka.actor.Props;
14 import akka.testkit.JavaTestKit;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import org.junit.Test;
17 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistration;
18 import org.opendaylight.controller.cluster.datastore.messages.CloseDataChangeListenerRegistrationReply;
19 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
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", MoreExecutors.newDirectExecutorService());
29
30   static {
31     store.onGlobalContextUpdated(TestModel.createTestContext());
32   }
33
34
35   @Test
36   public void testOnReceiveCloseListenerRegistration() throws Exception {
37     new JavaTestKit(getSystem()) {{
38       final Props props = DataChangeListenerRegistrationActor.props(store
39           .registerChangeListener(TestModel.TEST_PATH, noOpDataChangeListener(),
40               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(new CloseDataChangeListenerRegistration().toSerializable(), getRef());
48
49           final String out = new ExpectMsg<String>(duration("1 seconds"), "match hint") {
50             // do not put code outside this method, will run afterwards
51             @Override
52             protected String match(final Object in) {
53               if (in.getClass().equals(CloseDataChangeListenerRegistrationReply.SERIALIZABLE_CLASS)) {
54                 return "match";
55               } else {
56                 throw noMatch();
57               }
58             }
59           }.get(); // this extracts the received message
60
61           assertEquals("match", out);
62
63           expectNoMsg();
64         }
65
66
67       };
68     }};
69   }
70
71   private static AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>> noOpDataChangeListener(){
72     return new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
73       @Override
74       public void onDataChanged(final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
75
76       }
77     };
78   }
79
80 }