1ad1b345efc233ab1beed7f7b02e6be543be369d
[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.Matchers.any;
11 import static org.mockito.Mockito.doThrow;
12
13 import com.google.common.util.concurrent.CheckedFuture;
14 import java.util.Collection;
15 import java.util.Collections;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
23 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
24
25 public class AbstractDOMBrokerWriteTransactionTest {
26
27     @Mock
28     private AbstractDOMTransactionFactory abstractDOMTransactionFactory;
29
30     @Mock
31     private DOMStoreWriteTransaction domStoreWriteTransaction;
32
33     private class AbstractDOMBrokerWriteTransactionTestImpl
34             extends AbstractDOMBrokerWriteTransaction<DOMStoreWriteTransaction> {
35
36         AbstractDOMBrokerWriteTransactionTestImpl() {
37             super(new Object(), Collections.emptyMap(), abstractDOMTransactionFactory);
38         }
39
40         @Override
41         protected DOMStoreWriteTransaction createTransaction(LogicalDatastoreType key) {
42             return null;
43         }
44
45         @Override
46         protected Collection<DOMStoreWriteTransaction> getSubtransactions() {
47             return Collections.singletonList(domStoreWriteTransaction);
48         }
49     }
50
51     @Before
52     public void setup() {
53         MockitoAnnotations.initMocks(this);
54     }
55
56     @Test
57     public void readyRuntimeExceptionAndCancel() {
58         RuntimeException thrown = new RuntimeException();
59         doThrow(thrown).when(domStoreWriteTransaction).ready();
60         AbstractDOMBrokerWriteTransactionTestImpl abstractDOMBrokerWriteTransactionTestImpl =
61                 new AbstractDOMBrokerWriteTransactionTestImpl();
62
63         CheckedFuture<Void, TransactionCommitFailedException> submitFuture =
64                 abstractDOMBrokerWriteTransactionTestImpl.submit();
65         try {
66             submitFuture.checkedGet();
67             Assert.fail("TransactionCommitFailedException expected");
68         } catch (TransactionCommitFailedException e) {
69             assertTrue(e.getCause() == thrown);
70             abstractDOMBrokerWriteTransactionTestImpl.cancel();
71         }
72     }
73
74     @Test
75     public void submitRuntimeExceptionAndCancel() {
76         RuntimeException thrown = new RuntimeException();
77         doThrow(thrown).when(abstractDOMTransactionFactory).commit(any(), any());
78         AbstractDOMBrokerWriteTransactionTestImpl abstractDOMBrokerWriteTransactionTestImpl
79                 = new AbstractDOMBrokerWriteTransactionTestImpl();
80
81         CheckedFuture<Void, TransactionCommitFailedException> submitFuture =
82                 abstractDOMBrokerWriteTransactionTestImpl.submit();
83         try {
84             submitFuture.checkedGet();
85             Assert.fail("TransactionCommitFailedException expected");
86         } catch (TransactionCommitFailedException e) {
87             assertTrue(e.getCause() == thrown);
88             abstractDOMBrokerWriteTransactionTestImpl.cancel();
89         }
90     }
91 }

©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.