Merge "Implementation of ModuleShardStrategy"
[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.Props;
5 import junit.framework.Assert;
6
7 import org.opendaylight.controller.cluster.datastore.messages.RegisterChangeListenerReply;
8 import org.opendaylight.controller.cluster.datastore.shardstrategy.ShardStrategyFactory;
9 import org.opendaylight.controller.cluster.datastore.utils.DoNothingActor;
10 import org.opendaylight.controller.cluster.datastore.utils.MockActorContext;
11 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
12 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
13 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeListener;
16 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
17 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
18 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
19 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
24
25 public class DistributedDataStoreTest extends AbstractActorTest{
26
27     private DistributedDataStore distributedDataStore;
28     private MockActorContext mockActorContext;
29     private ActorRef doNothingActorRef;
30
31     @org.junit.Before
32     public void setUp() throws Exception {
33         ShardStrategyFactory.setConfiguration(new MockConfiguration());
34         final Props props = Props.create(DoNothingActor.class);
35
36         doNothingActorRef = getSystem().actorOf(props);
37
38         mockActorContext = new MockActorContext(getSystem(), doNothingActorRef);
39         distributedDataStore = new DistributedDataStore(mockActorContext, "config");
40         distributedDataStore.onGlobalContextUpdated(
41             TestModel.createTestContext());
42
43         // Make CreateTransactionReply as the default response. Will need to be
44         // tuned if a specific test requires some other response
45         mockActorContext.setExecuteShardOperationResponse(
46             CreateTransactionReply.newBuilder()
47                 .setTransactionActorPath(doNothingActorRef.path().toString())
48                 .setTransactionId("txn-1 ")
49                 .build());
50     }
51
52     @org.junit.After
53     public void tearDown() throws Exception {
54
55     }
56
57     @org.junit.Test
58     public void testRegisterChangeListener() throws Exception {
59         mockActorContext.setExecuteShardOperationResponse(new RegisterChangeListenerReply(doNothingActorRef.path()).toSerializable());
60         ListenerRegistration registration =
61                 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<InstanceIdentifier, NormalizedNode<?, ?>>() {
62             @Override
63             public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> change) {
64                 throw new UnsupportedOperationException("onDataChanged");
65             }
66         }, AsyncDataBroker.DataChangeScope.BASE);
67
68         Assert.assertNotNull(registration);
69     }
70
71     @org.junit.Test
72     public void testCreateTransactionChain() throws Exception {
73         final DOMStoreTransactionChain transactionChain = distributedDataStore.createTransactionChain();
74         Assert.assertNotNull(transactionChain);
75     }
76
77     @org.junit.Test
78     public void testNewReadOnlyTransaction() throws Exception {
79         final DOMStoreReadTransaction transaction = distributedDataStore.newReadOnlyTransaction();
80         Assert.assertNotNull(transaction);
81     }
82
83     @org.junit.Test
84     public void testNewWriteOnlyTransaction() throws Exception {
85         final DOMStoreWriteTransaction transaction = distributedDataStore.newWriteOnlyTransaction();
86         Assert.assertNotNull(transaction);
87     }
88
89     @org.junit.Test
90     public void testNewReadWriteTransaction() throws Exception {
91         final DOMStoreReadWriteTransaction transaction = distributedDataStore.newReadWriteTransaction();
92         Assert.assertNotNull(transaction);
93     }
94 }