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