Unit tests for ClientBackedDataStore class
[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 java.lang.reflect.Field;
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.Mock;
15 import org.mockito.Mockito;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
18 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
19 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
20 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.MemberName;
22 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
23 import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle;
24 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
25 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
26 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
27 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
28 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
29 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
30 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
31 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
32 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
33 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
34 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36
37 public class ClientBackedDataStoreTest {
38
39     private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
40             FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
41
42     private static FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
43             MemberName.forName("member"), FrontendType.forName("frontend"));
44     private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
45
46     private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
47     private static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(HISTORY_ID, 0);
48
49     @Mock
50     private DataStoreClient clientActor;
51
52     @Mock
53     private ActorContext actorContext;
54
55     @Mock
56     private ClientLocalHistory clientLocalHistory;
57
58     @Mock
59     private ClientTransaction clientTransaction;
60
61     @Mock
62     private ClientSnapshot clientSnapshot;
63
64     @Before
65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67
68         final SchemaContext schemaContext = TestModel.createTestContext();
69
70         Mockito.when(actorContext.getSchemaContext()).thenReturn(schemaContext);
71         Mockito.when(actorContext.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build());
72
73         final Field transactionIdField = AbstractClientHandle.class.getDeclaredField("transactionId");
74         transactionIdField.setAccessible(true);
75
76         // set transaction ids to mocked objects
77         transactionIdField.set(clientTransaction, TRANSACTION_IDENTIFIER);
78         transactionIdField.set(clientSnapshot, TRANSACTION_IDENTIFIER);
79
80         Mockito.when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
81         Mockito.when(clientActor.createTransaction()).thenReturn(clientTransaction);
82         Mockito.when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
83         Mockito.when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
84     }
85
86     @Test
87     public void testCreateTransactionChain() throws Exception {
88         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
89                 actorContext, UNKNOWN_ID, clientActor)) {
90             final DOMStoreTransactionChain txChain = clientBackedDataStore.createTransactionChain();
91             Assert.assertNotNull(txChain);
92             Mockito.verify(clientActor, Mockito.times(1)).createLocalHistory();
93         }
94     }
95
96     @Test
97     public void testNewReadOnlyTransaction() throws Exception {
98         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
99                 actorContext, UNKNOWN_ID, clientActor)) {
100             final DOMStoreReadTransaction tx = clientBackedDataStore.newReadOnlyTransaction();
101             Assert.assertNotNull(tx);
102             Mockito.verify(clientActor, Mockito.times(1)).createSnapshot();
103         }
104     }
105
106     @Test
107     public void testNewWriteOnlyTransaction() throws Exception {
108         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
109                 actorContext, UNKNOWN_ID, clientActor)) {
110             final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
111             Assert.assertNotNull(tx);
112             Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
113         }
114     }
115
116     @Test
117     public void testNewReadWriteTransaction() throws Exception {
118         try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
119                 actorContext, UNKNOWN_ID, clientActor)) {
120             final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
121             Assert.assertNotNull(tx);
122             Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
123         }
124     }
125 }