1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import junit.framework.Assert;
6 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
7 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
8 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
9 import org.opendaylight.controller.cluster.datastore.utils.MockActorContext;
10 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
11 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
15 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24 public class DistributedDataStoreTest extends AbstractActorTest{
26 private DistributedDataStore distributedDataStore;
27 private MockActorContext mockActorContext;
28 private ActorRef doNothingActorRef;
31 public void setUp() throws Exception {
32 ShardStrategyFactory.setConfiguration(new MockConfiguration());
33 final Props props = Props.create(DoNothingActor.class);
35 doNothingActorRef = getSystem().actorOf(props);
37 mockActorContext = new MockActorContext(getSystem(), doNothingActorRef);
38 distributedDataStore = new DistributedDataStore(mockActorContext, "config");
39 distributedDataStore.onGlobalContextUpdated(
40 TestModel.createTestContext());
42 // Make CreateTransactionReply as the default response. Will need to be
43 // tuned if a specific test requires some other response
44 mockActorContext.setExecuteShardOperationResponse(
45 CreateTransactionReply.newBuilder()
46 .setTransactionActorPath(doNothingActorRef.path().toString())
47 .setTransactionId("txn-1 ")
52 public void tearDown() throws Exception {
57 public void testRegisterChangeListenerWhenShardIsNotLocal() throws Exception {
59 ListenerRegistration registration =
60 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
62 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
63 throw new UnsupportedOperationException("onDataChanged");
65 }, AsyncDataBroker.DataChangeScope.BASE);
67 // Since we do not expect the shard to be local registration will return a NoOpRegistration
68 Assert.assertTrue(registration instanceof NoOpDataChangeListenerRegistration);
70 Assert.assertNotNull(registration);
74 public void testRegisterChangeListenerWhenShardIsLocal() throws Exception {
76 mockActorContext.setExecuteLocalShardOperationResponse(new RegisterChangeListenerReply(doNothingActorRef.path()));
78 ListenerRegistration registration =
79 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
81 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
82 throw new UnsupportedOperationException("onDataChanged");
84 }, AsyncDataBroker.DataChangeScope.BASE);
86 Assert.assertTrue(registration instanceof DataChangeListenerRegistrationProxy);
88 Assert.assertNotNull(registration);
93 public void testCreateTransactionChain() throws Exception {
94 final DOMStoreTransactionChain transactionChain = distributedDataStore.createTransactionChain();
95 Assert.assertNotNull(transactionChain);
99 public void testNewReadOnlyTransaction() throws Exception {
100 final DOMStoreReadTransaction transaction = distributedDataStore.newReadOnlyTransaction();
101 Assert.assertNotNull(transaction);
105 public void testNewWriteOnlyTransaction() throws Exception {
106 final DOMStoreWriteTransaction transaction = distributedDataStore.newWriteOnlyTransaction();
107 Assert.assertNotNull(transaction);
111 public void testNewReadWriteTransaction() throws Exception {
112 final DOMStoreReadWriteTransaction transaction = distributedDataStore.newReadWriteTransaction();
113 Assert.assertNotNull(transaction);