Clean 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.doReturn;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.Mock;
19 import org.mockito.junit.MockitoJUnitRunner;
20 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
21 import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier;
22 import org.opendaylight.controller.cluster.access.concepts.FrontendType;
23 import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
24 import org.opendaylight.controller.cluster.access.concepts.MemberName;
25 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
26 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory;
27 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
28 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
29 import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
30 import org.opendaylight.controller.cluster.datastore.DatastoreContext;
31 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
32
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class ClientBackedDataStoreTest {
35
36     private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
37             FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
38
39     private static final FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
40             MemberName.forName("member"), FrontendType.forName("frontend"));
41     private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
42
43     private static final TransactionIdentifier TRANSACTION_IDENTIFIER =
44         new TransactionIdentifier(new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0), 0);
45
46     @Mock
47     private DataStoreClient clientActor;
48     @Mock
49     private ActorUtils actorUtils;
50     @Mock
51     private ClientLocalHistory clientLocalHistory;
52     @Mock
53     private ClientTransaction clientTransaction;
54     @Mock
55     private ClientSnapshot clientSnapshot;
56
57     @Before
58     public void setUp() {
59         doReturn(DatastoreContext.newBuilder().build()).when(actorUtils).getDatastoreContext();
60         doReturn(TRANSACTION_IDENTIFIER).when(clientTransaction).getIdentifier();
61         doReturn(TRANSACTION_IDENTIFIER).when(clientSnapshot).getIdentifier();
62
63         doReturn(clientTransaction).when(clientActor).createTransaction();
64         doReturn(clientLocalHistory).when(clientActor).createLocalHistory();
65         doReturn(clientSnapshot).when(clientActor).createSnapshot();
66     }
67
68     @Test
69     public void testCreateTransactionChain() {
70         try (var clientBackedDataStore = new ClientBackedDataStore(actorUtils, UNKNOWN_ID, clientActor)) {
71             assertNotNull(clientBackedDataStore.createTransactionChain());
72             verify(clientActor, times(1)).createLocalHistory();
73         }
74     }
75
76     @Test
77     public void testNewReadOnlyTransaction() {
78         try (var clientBackedDataStore = new ClientBackedDataStore(actorUtils, UNKNOWN_ID, clientActor)) {
79             assertNotNull(clientBackedDataStore.newReadOnlyTransaction());
80             verify(clientActor, times(1)).createSnapshot();
81         }
82     }
83
84     @Test
85     public void testNewWriteOnlyTransaction() {
86         try (var clientBackedDataStore = new ClientBackedDataStore(actorUtils, UNKNOWN_ID, clientActor)) {
87             assertNotNull(clientBackedDataStore.newWriteOnlyTransaction());
88             verify(clientActor, times(1)).createTransaction();
89         }
90     }
91
92     @Test
93     public void testNewReadWriteTransaction() {
94         try (var clientBackedDataStore = new ClientBackedDataStore(actorUtils, UNKNOWN_ID, clientActor)) {
95             assertNotNull(clientBackedDataStore.newReadWriteTransaction());
96             verify(clientActor, times(1)).createTransaction();
97         }
98     }
99 }