Change handling of netconf cluster transactions
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / tx / WriteOnlyTransactionTest.java
index c9fa38f4bd6ba06410079732fbcabb0a879fa637..3e86bd2572da3208bb965464d1dea8813a36637d 100644 (file)
@@ -15,7 +15,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 import static org.mockito.MockitoAnnotations.initMocks;
 import static org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils.DEFAULT_SCHEMA_REPOSITORY;
@@ -41,15 +41,14 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.mockito.Mock;
-import org.mockito.Mockito;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
-import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
-import org.opendaylight.netconf.topology.singleton.impl.NetconfDOMDataBroker;
+import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMDataBroker;
 import org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor;
 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
@@ -73,12 +72,14 @@ public class WriteOnlyTransactionTest {
     public final ExpectedException exception = ExpectedException.none();
 
     private ActorRef masterRef;
-    private NetconfDOMDataBroker slaveDataBroker;
-    private DOMDataBroker masterDataBroker;
+    private ProxyDOMDataBroker slaveDataBroker;
     private List<SourceIdentifier> sourceIdentifiers;
-
+    @Mock
+    private DOMDataBroker deviceDataBroker;
     @Mock
     private DOMDataWriteTransaction writeTx;
+    @Mock
+    private DOMRpcService domRpcService;
 
     @Before
     public void setup() throws UnknownHostException {
@@ -91,46 +92,31 @@ public class WriteOnlyTransactionTest {
 
         final NetconfTopologySetup setup = mock(NetconfTopologySetup.class);
         final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY,
-                DEFAULT_SCHEMA_REPOSITORY);
+                DEFAULT_SCHEMA_REPOSITORY, TIMEOUT);
 
         masterRef = TestActorRef.create(system, props, "master_read");
 
         sourceIdentifiers = Lists.newArrayList();
 
-        // Create master data broker
-
-        final DOMDataBroker delegateDataBroker = mock(DOMDataBroker.class);
         writeTx = mock(DOMDataWriteTransaction.class);
         final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class);
 
-        doReturn(writeTx).when(delegateDataBroker).newWriteOnlyTransaction();
-        doReturn(readTx).when(delegateDataBroker).newReadOnlyTransaction();
-
-        final NetconfDOMTransaction masterDOMTransactions =
-                new NetconfMasterDOMTransaction(remoteDeviceId, delegateDataBroker);
-
-        masterDataBroker =
-                new NetconfDOMDataBroker(system, remoteDeviceId, masterDOMTransactions);
+        doReturn(writeTx).when(deviceDataBroker).newWriteOnlyTransaction();
+        doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction();
 
         // Create slave data broker for testing proxy
-
-        final NetconfDOMTransaction proxyDOMTransactions =
-                new NetconfProxyDOMTransaction(remoteDeviceId, system, masterRef);
-
-        slaveDataBroker = new NetconfDOMDataBroker(system, remoteDeviceId, proxyDOMTransactions);
-
-
+        slaveDataBroker =
+                new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS));
     }
 
     @After
     public void teardown() {
-        JavaTestKit.shutdownActorSystem(system);
+        JavaTestKit.shutdownActorSystem(system, null, true);
         system = null;
     }
 
     @Test
-    public void testPutMergeDeleteCalls() throws Exception {
-
+    public void testPut() throws Exception {
         /* Initialize data on master */
 
         initializeDataTest();
@@ -144,23 +130,57 @@ public class WriteOnlyTransactionTest {
         // Test of invoking put on master through slave proxy
 
         doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode);
-        slaveDataBroker.newWriteOnlyTransaction().put(storeType, instanceIdentifier, testNode);
 
-        verify(writeTx, times(1)).put(storeType, instanceIdentifier, testNode);
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        wTx.put(storeType, instanceIdentifier, testNode);
+
+        verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode);
+
+        wTx.cancel();
+
+    }
+
+    @Test
+    public void testMerge() throws Exception {
+
+        /* Initialize data on master */
+
+        initializeDataTest();
 
+        final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
+        final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
+        final NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname")))
+                .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build();
         // Test of invoking merge on master through slave proxy
 
         doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode);
-        slaveDataBroker.newWriteOnlyTransaction().merge(storeType, instanceIdentifier, testNode);
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        wTx.merge(storeType, instanceIdentifier, testNode);
+
+        verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode);
 
-        verify(writeTx, times(1)).merge(storeType, instanceIdentifier, testNode);
+        wTx.cancel();
 
+    }
+
+    @Test
+    public void testDelete() throws Exception {
+
+        /* Initialize data on master */
+
+        initializeDataTest();
+
+        final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY;
+        final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION;
         // Test of invoking delete on master through slave proxy
 
         doNothing().when(writeTx).delete(storeType, instanceIdentifier);
-        slaveDataBroker.newWriteOnlyTransaction().delete(storeType, instanceIdentifier);
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        wTx.delete(storeType, instanceIdentifier);
+        wTx.cancel();
 
-        verify(writeTx, times(1)).delete(storeType, instanceIdentifier);
+        verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier);
 
     }
 
@@ -173,33 +193,47 @@ public class WriteOnlyTransactionTest {
 
         // Without Tx
 
-        final CheckedFuture<Void,TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
         doReturn(resultSubmit).when(writeTx).submit();
 
-        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse =
-                slaveDataBroker.newWriteOnlyTransaction().submit();
+        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
 
-        final Object result= resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
+        final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
 
         assertNull(result);
+    }
 
-        // With Tx
+    @Test
+    public void testSubmitWithOperation() throws Exception {
+
+        /* Initialize data on master */
 
+        initializeDataTest();
+        // With Tx
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
         doNothing().when(writeTx).delete(any(), any());
-        slaveDataBroker.newWriteOnlyTransaction().delete(LogicalDatastoreType.CONFIGURATION,
+        wTx.delete(LogicalDatastoreType.CONFIGURATION,
                 YangInstanceIdentifier.EMPTY);
 
-        final CheckedFuture<Void,TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
+        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
         doReturn(resultSubmitTx).when(writeTx).submit();
 
-        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse =
-                slaveDataBroker.newWriteOnlyTransaction().submit();
+        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitTxResponse = wTx.submit();
 
         final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
 
         assertNull(resultTx);
+    }
 
-        slaveDataBroker.newWriteOnlyTransaction().delete(LogicalDatastoreType.CONFIGURATION,
+    @Test
+    public void testSubmitFail() throws Exception {
+
+        /* Initialize data on master */
+
+        initializeDataTest();
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        wTx.delete(LogicalDatastoreType.CONFIGURATION,
                 YangInstanceIdentifier.EMPTY);
 
         final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null);
@@ -209,7 +243,7 @@ public class WriteOnlyTransactionTest {
         doReturn(resultThrowable).when(writeTx).submit();
 
         final CheckedFuture<Void, TransactionCommitFailedException> resultThrowableResponse =
-                slaveDataBroker.newWriteOnlyTransaction().submit();
+                wTx.submit();
 
         exception.expect(TransactionCommitFailedException.class);
         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
@@ -223,32 +257,39 @@ public class WriteOnlyTransactionTest {
         initializeDataTest();
 
         // Without Tx
+        doReturn(true).when(writeTx).cancel();
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        final Boolean resultFalseNoTx = wTx.cancel();
+        assertEquals(true, resultFalseNoTx);
+    }
 
-        final Boolean resultFalseNoTx = slaveDataBroker.newWriteOnlyTransaction().cancel();
-        assertEquals(false, resultFalseNoTx);
+    @Test
+    public void testCancelWithOperation() throws Exception {
+
+        /* Initialize data on master */
+
+        initializeDataTest();
 
         // With Tx, readWriteTx test
 
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
         doNothing().when(writeTx).delete(any(), any());
-        slaveDataBroker.newReadWriteTransaction().delete(LogicalDatastoreType.CONFIGURATION,
+        wTx.delete(LogicalDatastoreType.CONFIGURATION,
                 YangInstanceIdentifier.EMPTY);
 
         doReturn(true).when(writeTx).cancel();
-
-        final Boolean resultTrue = slaveDataBroker.newWriteOnlyTransaction().cancel();
+        final Boolean resultTrue = wTx.cancel();
         assertEquals(true, resultTrue);
 
-        doReturn(false).when(writeTx).cancel();
-
-        final Boolean resultFalse = slaveDataBroker.newWriteOnlyTransaction().cancel();
+        final Boolean resultFalse = wTx.cancel();
         assertEquals(false, resultFalse);
 
     }
 
     private void initializeDataTest() throws Exception {
         final Future<Object> initialDataToActor =
-                Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers),
-                        TIMEOUT);
+                Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
+                                domRpcService), TIMEOUT);
 
         final Object success = Await.result(initialDataToActor, TIMEOUT.duration());