Fix infinite loop on cancel transaction
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / broker / impl / DOMForwardedWriteTransactionTest.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.md.sal.dom.broker.impl;
8
9 import static org.junit.Assert.assertTrue;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.doThrow;
13
14 import com.google.common.util.concurrent.CheckedFuture;
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.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
24
25 public class DOMForwardedWriteTransactionTest {
26
27     @Mock
28     private AbstractDOMForwardedTransactionFactory abstractDOMForwardedTransactionFactory;
29
30     @Mock
31     private DOMStoreWriteTransaction domStoreWriteTransaction;
32
33     @Before
34     public void setup() {
35         MockitoAnnotations.initMocks(this);
36     }
37
38     @Test
39     public void readyRuntimeExceptionAndCancel() {
40         RuntimeException thrown = new RuntimeException();
41         doThrow(thrown).when(domStoreWriteTransaction).ready();
42         DOMForwardedWriteTransaction<DOMStoreWriteTransaction> domForwardedWriteTransaction =
43                 new DOMForwardedWriteTransaction<>(
44                         new Object(),
45                         Collections.singletonMap(LogicalDatastoreType.OPERATIONAL, domStoreWriteTransaction),
46                         abstractDOMForwardedTransactionFactory);
47         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = domForwardedWriteTransaction.submit();
48         try {
49             submitFuture.checkedGet();
50             Assert.fail("TransactionCommitFailedException expected");
51         } catch (TransactionCommitFailedException e) {
52             assertTrue(e.getCause() == thrown);
53             domForwardedWriteTransaction.cancel();
54         }
55     }
56
57     @Test
58     public void submitRuntimeExceptionAndCancel() {
59         RuntimeException thrown = new RuntimeException();
60         doReturn(null).when(domStoreWriteTransaction).ready();
61         doThrow(thrown).when(abstractDOMForwardedTransactionFactory).submit(any(), any());
62         DOMForwardedWriteTransaction<DOMStoreWriteTransaction> domForwardedWriteTransaction =
63                 new DOMForwardedWriteTransaction<>(
64                     new Object(),
65                     Collections.singletonMap(LogicalDatastoreType.OPERATIONAL, domStoreWriteTransaction),
66                     abstractDOMForwardedTransactionFactory);
67         CheckedFuture<Void, TransactionCommitFailedException> submitFuture = domForwardedWriteTransaction.submit();
68         try {
69             submitFuture.checkedGet();
70             Assert.fail("TransactionCommitFailedException expected");
71         } catch (TransactionCommitFailedException e) {
72             assertTrue(e.getCause() == thrown);
73             domForwardedWriteTransaction.cancel();
74         }
75     }
76 }

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