1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6 import org.junit.After;
7 import org.junit.Before;
9 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
10 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
11 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
12 import org.opendaylight.controller.cluster.datastore.utils.MockActorContext;
13 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
14 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
18 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Matchers.any;
30 import static org.mockito.Matchers.eq;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.verify;
34 public class DistributedDataStoreTest extends AbstractActorTest{
36 private DistributedDataStore distributedDataStore;
37 private MockActorContext mockActorContext;
38 private ActorRef doNothingActorRef;
41 public void setUp() throws Exception {
42 ShardStrategyFactory.setConfiguration(new MockConfiguration());
43 final Props props = Props.create(DoNothingActor.class);
45 doNothingActorRef = getSystem().actorOf(props);
47 mockActorContext = new MockActorContext(getSystem(), doNothingActorRef);
48 distributedDataStore = new DistributedDataStore(mockActorContext);
49 distributedDataStore.onGlobalContextUpdated(
50 TestModel.createTestContext());
52 // Make CreateTransactionReply as the default response. Will need to be
53 // tuned if a specific test requires some other response
54 mockActorContext.setExecuteShardOperationResponse(
55 CreateTransactionReply.newBuilder()
56 .setTransactionActorPath(doNothingActorRef.path().toString())
57 .setTransactionId("txn-1 ")
62 public void tearDown() throws Exception {
67 public void testConstructor(){
68 ActorSystem actorSystem = mock(ActorSystem.class);
70 new DistributedDataStore(actorSystem, "config",
71 mock(ClusterWrapper.class), mock(Configuration.class), null);
73 verify(actorSystem).actorOf(any(Props.class), eq("shardmanager-config"));
77 public void testRegisterChangeListenerWhenShardIsNotLocal() throws Exception {
79 ListenerRegistration registration =
80 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
82 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
83 throw new UnsupportedOperationException("onDataChanged");
85 }, AsyncDataBroker.DataChangeScope.BASE);
87 // Since we do not expect the shard to be local registration will return a NoOpRegistration
88 assertTrue(registration instanceof NoOpDataChangeListenerRegistration);
90 assertNotNull(registration);
94 public void testRegisterChangeListenerWhenShardIsLocal() throws Exception {
96 mockActorContext.setExecuteLocalShardOperationResponse(new RegisterChangeListenerReply(doNothingActorRef.path()));
98 ListenerRegistration registration =
99 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
101 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
102 throw new UnsupportedOperationException("onDataChanged");
104 }, AsyncDataBroker.DataChangeScope.BASE);
106 assertTrue(registration instanceof DataChangeListenerRegistrationProxy);
108 assertNotNull(registration);
113 public void testCreateTransactionChain() throws Exception {
114 final DOMStoreTransactionChain transactionChain = distributedDataStore.createTransactionChain();
115 assertNotNull(transactionChain);
119 public void testNewReadOnlyTransaction() throws Exception {
120 final DOMStoreReadTransaction transaction = distributedDataStore.newReadOnlyTransaction();
121 assertNotNull(transaction);
125 public void testNewWriteOnlyTransaction() throws Exception {
126 final DOMStoreWriteTransaction transaction = distributedDataStore.newWriteOnlyTransaction();
127 assertNotNull(transaction);
131 public void testNewReadWriteTransaction() throws Exception {
132 final DOMStoreReadWriteTransaction transaction = distributedDataStore.newReadWriteTransaction();
133 assertNotNull(transaction);