Update junit Assert imports
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / sal / tx / NetconfDeviceWriteOnlyTxTest.java
1 package org.opendaylight.controller.sal.connect.netconf.sal.tx;
2
3 import static org.junit.Assert.fail;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Matchers.eq;
6 import static org.mockito.Mockito.doReturn;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.verifyNoMoreInteractions;
9 import static org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil.DISCARD_CHANGES_RPC_CONTENT;
10
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mock;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
20 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
21 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.controller.sal.core.api.RpcImplementation;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28
29 public class NetconfDeviceWriteOnlyTxTest {
30
31     private final RemoteDeviceId id = new RemoteDeviceId("test-mount");
32
33     @Mock
34     private RpcImplementation rpc;
35     @Mock
36     private DataNormalizer normalizer;
37     private YangInstanceIdentifier yangIId;
38
39     @Before
40     public void setUp() throws Exception {
41         MockitoAnnotations.initMocks(this);
42
43         doReturn(Futures.<RpcResult<CompositeNode>>immediateFailedFuture(new IllegalStateException("Failed tx")))
44         .doReturn(Futures.immediateFuture(RpcResultBuilder.<CompositeNode>success().build()))
45                 .when(rpc).invokeRpc(any(QName.class), any(CompositeNode.class));
46
47         yangIId = YangInstanceIdentifier.builder().node(QName.create("namespace", "2012-12-12", "name")).build();
48         doReturn(yangIId).when(normalizer).toLegacy(yangIId);
49     }
50
51     @Test
52     public void testDiscardCahnges() {
53         final NetconfDeviceWriteOnlyTx tx = new NetconfDeviceWriteOnlyTx(id, rpc, normalizer, true, true);
54         final CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
55         try {
56             submitFuture.checkedGet();
57         } catch (final TransactionCommitFailedException e) {
58             // verify discard changes was sent
59             verify(rpc).invokeRpc(NetconfMessageTransformUtil.NETCONF_DISCARD_CHANGES_QNAME, DISCARD_CHANGES_RPC_CONTENT);
60             return;
61         }
62
63         fail("Submit should fail");
64     }
65
66
67     @Test
68     public void testDiscardCahngesNotSentWithoutCandidate() {
69         doReturn(Futures.immediateFuture(RpcResultBuilder.<CompositeNode>success().build()))
70         .doReturn(Futures.<RpcResult<CompositeNode>>immediateFailedFuture(new IllegalStateException("Failed tx")))
71                 .when(rpc).invokeRpc(any(QName.class), any(CompositeNode.class));
72
73         final NetconfDeviceWriteOnlyTx tx = new NetconfDeviceWriteOnlyTx(id, rpc, normalizer, false, true);
74         tx.delete(LogicalDatastoreType.CONFIGURATION, yangIId);
75         verify(rpc).invokeRpc(eq(NetconfMessageTransformUtil.NETCONF_EDIT_CONFIG_QNAME), any(CompositeNode.class));
76         verifyNoMoreInteractions(rpc);
77     }
78
79 }

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