bf107514809355dbcb2cff65384b083178fe3781
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / ClientBackedDataStoreTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.databroker;
9
10 import org.junit.Assert;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.Mock;
14 import org.mockito.Mockito;
15 import org.mockito.MockitoAnnotations;
16 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
17 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
19 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
20 import org.opendaylight.controller.cluster.access.concepts.MemberName;
21 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
22 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
23 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
24 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
25 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
26 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
27 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
28 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
29 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
30 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 public class ClientBackedDataStoreTest {
36
37     private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
38             FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
39
40     private static FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
41             MemberName.forName("member"), FrontendType.forName("frontend"));
42     private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
43
44     private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
45     private static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(HISTORY_ID, 0);
46
47     @Mock
48     private DataStoreClient clientActor;
49
50     @Mock
51     private ActorContext actorContext;
52
53     @Mock
54     private ClientLocalHistory clientLocalHistory;
55
56     @Mock
57     private ClientTransaction clientTransaction;
58
59     @Mock
60     private ClientSnapshot clientSnapshot;
61
62     @Before
63     public void setUp() throws Exception {
64         MockitoAnnotations.initMocks(this);
65
66         final SchemaContext schemaContext = TestModel.createTestContext();
67
68         Mockito.when(actorContext.getSchemaContext()).thenReturn(schemaContext);
69         Mockito.when(actorContext.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build());
70         Mockito.when(clientTransaction.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
71         Mockito.when(clientSnapshot.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER);
72
73         Mockito.when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
74         Mockito.when(clientActor.createTransaction()).thenReturn(clientTransaction);
75         Mockito.when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
76         Mockito.when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
77     }
78
79     @Test
80     public void testCreateTransactionChain() throws Exception {
81         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
82                 actorContext, UNKNOWN_ID, clientActor)) {
83             final DOMStoreTransactionChain txChain = clientBackedDataStore.createTransactionChain();
84             Assert.assertNotNull(txChain);
85             Mockito.verify(clientActor, Mockito.times(1)).createLocalHistory();
86         }
87     }
88
89     @Test
90     public void testNewReadOnlyTransaction() throws Exception {
91         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
92                 actorContext, UNKNOWN_ID, clientActor)) {
93             final DOMStoreReadTransaction tx = clientBackedDataStore.newReadOnlyTransaction();
94             Assert.assertNotNull(tx);
95             Mockito.verify(clientActor, Mockito.times(1)).createSnapshot();
96         }
97     }
98
99     @Test
100     public void testNewWriteOnlyTransaction() throws Exception {
101         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
102                 actorContext, UNKNOWN_ID, clientActor)) {
103             final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
104             Assert.assertNotNull(tx);
105             Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
106         }
107     }
108
109     @Test
110     public void testNewReadWriteTransaction() throws Exception {
111         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
112                 actorContext, UNKNOWN_ID, clientActor)) {
113             final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
114             Assert.assertNotNull(tx);
115             Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
116         }
117     }
118 }