1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
7 import org.junit.After;
8 import org.junit.Before;
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;
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;
35 public class DistributedDataStoreTest extends AbstractActorTest{
37 private DistributedDataStore distributedDataStore;
38 private MockActorContext mockActorContext;
39 private ActorRef doNothingActorRef;
42 public void setUp() throws Exception {
43 ShardStrategyFactory.setConfiguration(new MockConfiguration());
44 final Props props = Props.create(DoNothingActor.class);
46 doNothingActorRef = getSystem().actorOf(props);
48 mockActorContext = new MockActorContext(getSystem(), doNothingActorRef);
49 distributedDataStore = new DistributedDataStore(mockActorContext);
50 distributedDataStore.onGlobalContextUpdated(
51 TestModel.createTestContext());
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 ")
63 public void tearDown() throws Exception {
67 @SuppressWarnings("resource")
69 public void testConstructor(){
70 ActorSystem actorSystem = mock(ActorSystem.class);
72 new DistributedDataStore(actorSystem, "config",
73 mock(ClusterWrapper.class), mock(Configuration.class),
74 new DatastoreContext());
76 verify(actorSystem).actorOf(any(Props.class), eq("shardmanager-config"));
80 public void testRegisterChangeListenerWhenShardIsNotLocal() throws Exception {
82 ListenerRegistration registration =
83 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
85 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
86 throw new UnsupportedOperationException("onDataChanged");
88 }, AsyncDataBroker.DataChangeScope.BASE);
90 // Since we do not expect the shard to be local registration will return a NoOpRegistration
91 assertTrue(registration instanceof NoOpDataChangeListenerRegistration);
93 assertNotNull(registration);
97 public void testRegisterChangeListenerWhenShardIsLocal() throws Exception {
99 mockActorContext.setExecuteLocalShardOperationResponse(new RegisterChangeListenerReply(doNothingActorRef.path()));
101 ListenerRegistration registration =
102 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
104 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
105 throw new UnsupportedOperationException("onDataChanged");
107 }, AsyncDataBroker.DataChangeScope.BASE);
109 assertTrue(registration instanceof DataChangeListenerRegistrationProxy);
111 assertNotNull(registration);
116 public void testCreateTransactionChain() throws Exception {
117 final DOMStoreTransactionChain transactionChain = distributedDataStore.createTransactionChain();
118 assertNotNull(transactionChain);
122 public void testNewReadOnlyTransaction() throws Exception {
123 final DOMStoreReadTransaction transaction = distributedDataStore.newReadOnlyTransaction();
124 assertNotNull(transaction);
128 public void testNewWriteOnlyTransaction() throws Exception {
129 final DOMStoreWriteTransaction transaction = distributedDataStore.newWriteOnlyTransaction();
130 assertNotNull(transaction);
134 public void testNewReadWriteTransaction() throws Exception {
135 final DOMStoreReadWriteTransaction transaction = distributedDataStore.newReadWriteTransaction();
136 assertNotNull(transaction);