Merge "Bug 1598: Cleanup stale ShardReadTransactions"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DistributedDataStoreTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6
7 import org.junit.After;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
11 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
12 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
13 import org.opendaylight.controller.cluster.datastore.utils.MockActorContext;
14 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
15 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
19 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.verify;
34
35 public class DistributedDataStoreTest extends AbstractActorTest{
36
37     private DistributedDataStore distributedDataStore;
38     private MockActorContext mockActorContext;
39     private ActorRef doNothingActorRef;
40
41     @Before
42     public void setUp() throws Exception {
43         ShardStrategyFactory.setConfiguration(new MockConfiguration());
44         final Props props = Props.create(DoNothingActor.class);
45
46         doNothingActorRef = getSystem().actorOf(props);
47
48         mockActorContext = new MockActorContext(getSystem(), doNothingActorRef);
49         distributedDataStore = new DistributedDataStore(mockActorContext);
50         distributedDataStore.onGlobalContextUpdated(
51             TestModel.createTestContext());
52
53         // Make CreateTransactionReply as the default response. Will need to be
54         // tuned if a specific test requires some other response
55         mockActorContext.setExecuteShardOperationResponse(
56             CreateTransactionReply.newBuilder()
57                 .setTransactionActorPath(doNothingActorRef.path().toString())
58                 .setTransactionId("txn-1 ")
59                 .build());
60     }
61
62     @After
63     public void tearDown() throws Exception {
64
65     }
66
67     @SuppressWarnings("resource")
68     @Test
69     public void testConstructor(){
70         ActorSystem actorSystem = mock(ActorSystem.class);
71
72         new DistributedDataStore(actorSystem, "config",
73             mock(ClusterWrapper.class), mock(Configuration.class),
74             new DistributedDataStoreProperties());
75
76         verify(actorSystem).actorOf(any(Props.class), eq("shardmanager-config"));
77     }
78
79     @Test
80     public void testRegisterChangeListenerWhenShardIsNotLocal() throws Exception {
81
82         ListenerRegistration registration =
83                 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
84             @Override
85             public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
86                 throw new UnsupportedOperationException("onDataChanged");
87             }
88         }, AsyncDataBroker.DataChangeScope.BASE);
89
90         // Since we do not expect the shard to be local registration will return a NoOpRegistration
91         assertTrue(registration instanceof NoOpDataChangeListenerRegistration);
92
93         assertNotNull(registration);
94     }
95
96     @Test
97     public void testRegisterChangeListenerWhenShardIsLocal() throws Exception {
98
99         mockActorContext.setExecuteLocalShardOperationResponse(new RegisterChangeListenerReply(doNothingActorRef.path()));
100
101         ListenerRegistration registration =
102             distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
103                 @Override
104                 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
105                     throw new UnsupportedOperationException("onDataChanged");
106                 }
107             }, AsyncDataBroker.DataChangeScope.BASE);
108
109         assertTrue(registration instanceof DataChangeListenerRegistrationProxy);
110
111         assertNotNull(registration);
112     }
113
114
115     @Test
116     public void testCreateTransactionChain() throws Exception {
117         final DOMStoreTransactionChain transactionChain = distributedDataStore.createTransactionChain();
118         assertNotNull(transactionChain);
119     }
120
121     @Test
122     public void testNewReadOnlyTransaction() throws Exception {
123         final DOMStoreReadTransaction transaction = distributedDataStore.newReadOnlyTransaction();
124         assertNotNull(transaction);
125     }
126
127     @Test
128     public void testNewWriteOnlyTransaction() throws Exception {
129         final DOMStoreWriteTransaction transaction = distributedDataStore.newWriteOnlyTransaction();
130         assertNotNull(transaction);
131     }
132
133     @Test
134     public void testNewReadWriteTransaction() throws Exception {
135         final DOMStoreReadWriteTransaction transaction = distributedDataStore.newReadWriteTransaction();
136         assertNotNull(transaction);
137     }
138 }