Eliminate use of deprecated mockito methods
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / databroker / AbstractDOMBrokerWriteTransactionTest.java
1 /*
2  *
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.controller.cluster.databroker;
8
9 import static org.junit.Assert.assertTrue;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.doThrow;
12
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.concurrent.ExecutionException;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
26
27 public class AbstractDOMBrokerWriteTransactionTest {
28
29     @Mock
30     private AbstractDOMTransactionFactory abstractDOMTransactionFactory;
31
32     @Mock
33     private DOMStoreWriteTransaction domStoreWriteTransaction;
34
35     private class AbstractDOMBrokerWriteTransactionTestImpl
36             extends AbstractDOMBrokerWriteTransaction<DOMStoreWriteTransaction> {
37
38         AbstractDOMBrokerWriteTransactionTestImpl() {
39             super(new Object(), Collections.emptyMap(), abstractDOMTransactionFactory);
40         }
41
42         @Override
43         protected DOMStoreWriteTransaction createTransaction(LogicalDatastoreType key) {
44             return null;
45         }
46
47         @Override
48         protected Collection<DOMStoreWriteTransaction> getSubtransactions() {
49             return Collections.singletonList(domStoreWriteTransaction);
50         }
51     }
52
53     @Before
54     public void setup() {
55         MockitoAnnotations.initMocks(this);
56     }
57
58     @Test
59     public void readyRuntimeExceptionAndCancel() throws InterruptedException {
60         RuntimeException thrown = new RuntimeException();
61         doThrow(thrown).when(domStoreWriteTransaction).ready();
62         AbstractDOMBrokerWriteTransactionTestImpl abstractDOMBrokerWriteTransactionTestImpl =
63                 new AbstractDOMBrokerWriteTransactionTestImpl();
64
65         FluentFuture<? extends CommitInfo> submitFuture = abstractDOMBrokerWriteTransactionTestImpl.commit();
66         try {
67             submitFuture.get();
68             Assert.fail("TransactionCommitFailedException expected");
69         } catch (ExecutionException e) {
70             assertTrue(e.getCause() instanceof TransactionCommitFailedException);
71             assertTrue(e.getCause().getCause() == thrown);
72             abstractDOMBrokerWriteTransactionTestImpl.cancel();
73         }
74     }
75
76     @Test
77     public void submitRuntimeExceptionAndCancel() throws InterruptedException {
78         RuntimeException thrown = new RuntimeException();
79         doThrow(thrown).when(abstractDOMTransactionFactory).commit(any(), any());
80         AbstractDOMBrokerWriteTransactionTestImpl abstractDOMBrokerWriteTransactionTestImpl
81                 = new AbstractDOMBrokerWriteTransactionTestImpl();
82
83         FluentFuture<? extends CommitInfo> submitFuture = abstractDOMBrokerWriteTransactionTestImpl.commit();
84         try {
85             submitFuture.get();
86             Assert.fail("TransactionCommitFailedException expected");
87         } catch (ExecutionException e) {
88             assertTrue(e.getCause() instanceof TransactionCommitFailedException);
89             assertTrue(e.getCause().getCause() == thrown);
90             abstractDOMBrokerWriteTransactionTestImpl.cancel();
91         }
92     }
93 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.