56968390321e4996909b34fca2e485e9340e39b4
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / ClientBackedReadWriteTransactionTest.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.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.doReturn;
13
14 import com.google.common.util.concurrent.FluentFuture;
15 import com.google.common.util.concurrent.Futures;
16 import java.util.Optional;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction;
22 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 public class ClientBackedReadWriteTransactionTest
27         extends ClientBackedTransactionTest<ClientBackedReadWriteTransaction> {
28     private ClientBackedReadWriteTransaction object;
29
30     @Mock
31     private ClientTransaction delegate;
32     @Mock
33     private NormalizedNode<?, ?> data;
34     @Mock
35     private DOMStoreThreePhaseCommitCohort readyCohort;
36
37     @Override
38     ClientBackedReadWriteTransaction object() {
39         return object;
40     }
41
42     @Before
43     public void setUp() {
44         MockitoAnnotations.initMocks(this);
45
46         doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
47         doReturn(readyCohort).when(delegate).ready();
48
49         doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(delegate).exists(YangInstanceIdentifier.EMPTY);
50         doReturn(Futures.immediateCheckedFuture(Optional.of(data))).when(delegate).read(YangInstanceIdentifier.EMPTY);
51
52         object = new ClientBackedReadWriteTransaction(delegate, null);
53     }
54
55     @Test
56     public void testRead() throws Exception {
57         final FluentFuture<Optional<NormalizedNode<?, ?>>> result = object().read(YangInstanceIdentifier.EMPTY);
58         final Optional<NormalizedNode<?, ?>> resultData = result.get();
59         assertTrue(resultData.isPresent());
60         assertEquals(data, resultData.get());
61     }
62
63     @Test
64     public void testExists() throws Exception {
65         assertEquals(Boolean.TRUE, object().exists(YangInstanceIdentifier.EMPTY).get());
66     }
67 }