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