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