Merge "BUG 907 distinguish augmented nodes"
[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 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;
23
24 public class DistributedDataStoreTest extends AbstractActorTest{
25
26     private DistributedDataStore distributedDataStore;
27     private MockActorContext mockActorContext;
28     private ActorRef doNothingActorRef;
29
30     @org.junit.Before
31     public void setUp() throws Exception {
32         ShardStrategyFactory.setConfiguration(new MockConfiguration());
33         final Props props = Props.create(DoNothingActor.class);
34
35         doNothingActorRef = getSystem().actorOf(props);
36
37         mockActorContext = new MockActorContext(getSystem(), doNothingActorRef);
38         distributedDataStore = new DistributedDataStore(mockActorContext, "config");
39         distributedDataStore.onGlobalContextUpdated(
40             TestModel.createTestContext());
41
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 ")
48                 .build());
49     }
50
51     @org.junit.After
52     public void tearDown() throws Exception {
53
54     }
55
56     @org.junit.Test
57     public void testRegisterChangeListenerWhenShardIsNotLocal() throws Exception {
58
59         ListenerRegistration registration =
60                 distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
61             @Override
62             public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
63                 throw new UnsupportedOperationException("onDataChanged");
64             }
65         }, AsyncDataBroker.DataChangeScope.BASE);
66
67         // Since we do not expect the shard to be local registration will return a NoOpRegistration
68         Assert.assertTrue(registration instanceof NoOpDataChangeListenerRegistration);
69
70         Assert.assertNotNull(registration);
71     }
72
73     @org.junit.Test
74     public void testRegisterChangeListenerWhenShardIsLocal() throws Exception {
75
76         mockActorContext.setExecuteLocalShardOperationResponse(new RegisterChangeListenerReply(doNothingActorRef.path()));
77
78         ListenerRegistration registration =
79             distributedDataStore.registerChangeListener(TestModel.TEST_PATH, new AsyncDataChangeListener<YangInstanceIdentifier, NormalizedNode<?, ?>>() {
80                 @Override
81                 public void onDataChanged(AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change) {
82                     throw new UnsupportedOperationException("onDataChanged");
83                 }
84             }, AsyncDataBroker.DataChangeScope.BASE);
85
86         Assert.assertTrue(registration instanceof DataChangeListenerRegistrationProxy);
87
88         Assert.assertNotNull(registration);
89     }
90
91
92     @org.junit.Test
93     public void testCreateTransactionChain() throws Exception {
94         final DOMStoreTransactionChain transactionChain = distributedDataStore.createTransactionChain();
95         Assert.assertNotNull(transactionChain);
96     }
97
98     @org.junit.Test
99     public void testNewReadOnlyTransaction() throws Exception {
100         final DOMStoreReadTransaction transaction = distributedDataStore.newReadOnlyTransaction();
101         Assert.assertNotNull(transaction);
102     }
103
104     @org.junit.Test
105     public void testNewWriteOnlyTransaction() throws Exception {
106         final DOMStoreWriteTransaction transaction = distributedDataStore.newWriteOnlyTransaction();
107         Assert.assertNotNull(transaction);
108     }
109
110     @org.junit.Test
111     public void testNewReadWriteTransaction() throws Exception {
112         final DOMStoreReadWriteTransaction transaction = distributedDataStore.newReadWriteTransaction();
113         Assert.assertNotNull(transaction);
114     }
115 }