Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / ClientBackedTransactionChainTest.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.never;
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
30 @RunWith(MockitoJUnitRunner.StrictStubs.class)
31 public class ClientBackedTransactionChainTest {
32     private ClientBackedTransactionChain chain;
33
34     @Mock
35     private ClientLocalHistory history;
36     @Mock
37     private ClientSnapshot snapshot;
38     @Mock
39     private ClientTransaction transaction;
40
41     @Before
42     public void setUp() {
43         final FrontendIdentifier frontendId = FrontendIdentifier.create(
44                 MemberName.forName("member"), FrontendType.forName("frontend"));
45         final ClientIdentifier clientId = ClientIdentifier.create(frontendId, 0);
46         final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(clientId, 0);
47         final TransactionIdentifier transactionId = new TransactionIdentifier(historyId, 0);
48
49         doReturn(transactionId).when(transaction).getIdentifier();
50         doReturn(transactionId).when(snapshot).getIdentifier();
51         doReturn(snapshot).when(history).takeSnapshot();
52         doReturn(transaction).when(history).createTransaction();
53
54         chain = new ClientBackedTransactionChain(history, false);
55     }
56
57     @Test
58     public void testNewReadOnlyTransaction() {
59         assertNotNull(chain.newReadOnlyTransaction());
60         verify(history).takeSnapshot();
61     }
62
63     @Test
64     public void testNewReadWriteTransaction() {
65         assertNotNull(chain.newReadWriteTransaction());
66         verify(history).createTransaction();
67     }
68
69     @Test
70     public void testNewWriteOnlyTransaction() {
71         assertNotNull(chain.newWriteOnlyTransaction());
72         verify(history).createTransaction();
73     }
74
75     @Test
76     public void testClose() {
77         chain.newReadOnlyTransaction();
78         chain.close();
79         verify(snapshot).abort();
80         verify(history).close();
81     }
82
83     @Test
84     public void testSnapshotClosed() {
85         chain.snapshotClosed(snapshot);
86         // snap is removed, so cannot be aborted
87         chain.close();
88         verify(snapshot, never()).abort();
89         verify(history).close();
90     }
91 }