X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=netconf%2Fnetconf-topology-singleton%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fnetconf%2Ftopology%2Fsingleton%2Fimpl%2Ftx%2FWriteOnlyTransactionTest.java;h=a13c9536254e6101c2b21f2bcf22fea53e1f2ae2;hb=0a95f4298a7f467b6c2eebf7904c8253cf3d5198;hp=f5fbe31e9d9a08707236e2cf64ba15dbd29131ad;hpb=8c9ae0a590ed7a7088990aa78d24fbd81b07375e;p=netconf.git diff --git a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/WriteOnlyTransactionTest.java b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/WriteOnlyTransactionTest.java index f5fbe31e9d..a13c953625 100644 --- a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/WriteOnlyTransactionTest.java +++ b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/WriteOnlyTransactionTest.java @@ -11,11 +11,10 @@ package org.opendaylight.netconf.topology.singleton.impl.tx; import static junit.framework.TestCase.assertNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -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; @@ -28,11 +27,10 @@ import akka.testkit.JavaTestKit; import akka.testkit.TestActorRef; import akka.util.Timeout; import com.google.common.collect.Lists; +import com.google.common.net.InetAddresses; import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.Futures; -import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; import java.util.List; import java.util.concurrent.TimeUnit; import org.junit.After; @@ -41,16 +39,15 @@ 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.DOMMountPointService; 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,193 +70,173 @@ public class WriteOnlyTransactionTest { @Rule public final ExpectedException exception = ExpectedException.none(); - private ActorRef masterRef; - private NetconfDOMDataBroker slaveDataBroker; - private DOMDataBroker masterDataBroker; - private List sourceIdentifiers; - + @Mock + private DOMDataBroker deviceDataBroker; @Mock private DOMDataWriteTransaction writeTx; - @Mock private DOMRpcService domRpcService; + @Mock + private DOMMountPointService mountPointService; + private ActorRef masterRef; + private ProxyDOMDataBroker slaveDataBroker; + private List sourceIdentifiers; + private NormalizedNode testNode; + private YangInstanceIdentifier instanceIdentifier; + private LogicalDatastoreType storeType; @Before - public void setup() throws UnknownHostException { + public void setup() throws Exception { initMocks(this); system = ActorSystem.create(); final RemoteDeviceId remoteDeviceId = new RemoteDeviceId("netconf-topology", - new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 9999)); + new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9999)); final NetconfTopologySetup setup = mock(NetconfTopologySetup.class); + doReturn(Duration.apply(0, TimeUnit.SECONDS)).when(setup).getIdleTimeout(); final Props props = NetconfNodeActor.props(setup, remoteDeviceId, DEFAULT_SCHEMA_REPOSITORY, - DEFAULT_SCHEMA_REPOSITORY); + DEFAULT_SCHEMA_REPOSITORY, TIMEOUT, mountPointService); 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(); + doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode); + doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode); + doNothing().when(writeTx).delete(storeType, instanceIdentifier); // 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)); + initializeDataTest(); + testNode = ImmutableContainerNodeBuilder.create() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("", "TestQname"))) + .withChild(ImmutableNodes.leafNode(QName.create("", "NodeQname"), "foo")).build(); + instanceIdentifier = YangInstanceIdentifier.EMPTY; + storeType = LogicalDatastoreType.CONFIGURATION; } @After public void teardown() { - JavaTestKit.shutdownActorSystem(system); + JavaTestKit.shutdownActorSystem(system, null, true); system = null; } @Test - public void testPutMergeDeleteCalls() 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(); - + public void testPut() throws Exception { // Test of invoking put on master through slave proxy - - doNothing().when(writeTx).put(storeType, instanceIdentifier, testNode); - - DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); wTx.put(storeType, instanceIdentifier, testNode); - verify(writeTx, times(1)).put(storeType, instanceIdentifier, testNode); + verify(writeTx, timeout(2000)).put(storeType, instanceIdentifier, testNode); wTx.cancel(); - // Test of invoking merge on master through slave proxy + } - doNothing().when(writeTx).merge(storeType, instanceIdentifier, testNode); - wTx = slaveDataBroker.newWriteOnlyTransaction(); + @Test + public void testMerge() throws Exception { + // Test of invoking merge on master through slave proxy + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); wTx.merge(storeType, instanceIdentifier, testNode); - verify(writeTx, times(1)).merge(storeType, instanceIdentifier, testNode); + verify(writeTx, timeout(2000)).merge(storeType, instanceIdentifier, testNode); wTx.cancel(); - // Test of invoking delete on master through slave proxy + } - doNothing().when(writeTx).delete(storeType, instanceIdentifier); - wTx = slaveDataBroker.newWriteOnlyTransaction(); - wTx.delete(storeType, instanceIdentifier); + @Test + public void testDelete() throws Exception { + // Test of invoking delete on master through slave proxy + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); + wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY); wTx.cancel(); - verify(writeTx, times(1)).delete(storeType, instanceIdentifier); - + verify(writeTx, timeout(2000)).delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY); } @Test public void testSubmit() throws Exception { - - /* Initialize data on master */ - - initializeDataTest(); + final CheckedFuture resultSubmit = Futures.immediateCheckedFuture(null); + doReturn(resultSubmit).when(writeTx).submit(); // Without Tx - - DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); - final CheckedFuture resultSubmit = Futures.immediateCheckedFuture(null); - doReturn(resultSubmit).when(writeTx).submit(); + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); final CheckedFuture resultSubmitResponse = wTx.submit(); final Object result = resultSubmitResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); assertNull(result); + } + @Test + public void testSubmitWithOperation() throws Exception { + final CheckedFuture resultSubmitTx = + Futures.immediateCheckedFuture(null); + doReturn(resultSubmitTx).when(writeTx).submit(); // With Tx - wTx = slaveDataBroker.newWriteOnlyTransaction(); - doNothing().when(writeTx).delete(any(), any()); + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY); - final CheckedFuture resultSubmitTx = Futures.immediateCheckedFuture(null); - doReturn(resultSubmitTx).when(writeTx).submit(); - final CheckedFuture resultSubmitTxResponse = wTx.submit(); final Object resultTx = resultSubmitTxResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); assertNull(resultTx); + } - wTx = slaveDataBroker.newWriteOnlyTransaction(); - wTx.delete(LogicalDatastoreType.CONFIGURATION, - YangInstanceIdentifier.EMPTY); - + @Test + public void testSubmitFail() throws Exception { final TransactionCommitFailedException throwable = new TransactionCommitFailedException("Fail", null); - final CheckedFuture resultThrowable = + final CheckedFuture resultThrowable = Futures.immediateFailedCheckedFuture(throwable); - doReturn(resultThrowable).when(writeTx).submit(); + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); + wTx.delete(LogicalDatastoreType.CONFIGURATION, + YangInstanceIdentifier.EMPTY); final CheckedFuture resultThrowableResponse = wTx.submit(); - exception.expect(TransactionCommitFailedException.class); resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); } @Test public void testCancel() throws Exception { - - /* Initialize data on master */ - - initializeDataTest(); + doReturn(true).when(writeTx).cancel(); // Without Tx - - DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); final Boolean resultFalseNoTx = wTx.cancel(); - assertEquals(false, resultFalseNoTx); + assertEquals(true, resultFalseNoTx); + } - // With Tx, readWriteTx test + @Test + public void testCancelWithOperation() throws Exception { + doReturn(true).when(writeTx).cancel(); - wTx = slaveDataBroker.newWriteOnlyTransaction(); - doNothing().when(writeTx).delete(any(), any()); + // With Tx, readWriteTx test + final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY); - doReturn(true).when(writeTx).cancel(); final Boolean resultTrue = wTx.cancel(); assertEquals(true, resultTrue); - doReturn(false).when(writeTx).cancel(); - final Boolean resultFalse = wTx.cancel(); assertEquals(false, resultFalse); - } private void initializeDataTest() throws Exception { final Future initialDataToActor = - Patterns.ask(masterRef, new CreateInitialMasterActorData(masterDataBroker, sourceIdentifiers, + Patterns.ask(masterRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers, domRpcService), TIMEOUT); final Object success = Await.result(initialDataToActor, TIMEOUT.duration());