From da2126ecf60a994578c2ef654f4ff73ae727f6b5 Mon Sep 17 00:00:00 2001 From: Andrej Mak Date: Tue, 25 Apr 2017 08:19:53 +0200 Subject: [PATCH] Refactor netconf clustered topology tests - split long tests into smaller - remove duplicated code Change-Id: I64913380fd0768394b430971ef5ff118d56b7b6b Signed-off-by: Andrej Mak --- .../impl/tx/ReadOnlyTransactionTest.java | 106 ++++++++--------- .../impl/tx/WriteOnlyTransactionTest.java | 108 +++++------------- 2 files changed, 73 insertions(+), 141 deletions(-) diff --git a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/ReadOnlyTransactionTest.java b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/ReadOnlyTransactionTest.java index 1dafc59438..79e5001e36 100644 --- a/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/ReadOnlyTransactionTest.java +++ b/netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/ReadOnlyTransactionTest.java @@ -28,7 +28,6 @@ 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; @@ -75,9 +74,11 @@ public class ReadOnlyTransactionTest { private DOMDataReadOnlyTransaction readTx; @Mock private DOMRpcService domRpcService; + private YangInstanceIdentifier instanceIdentifier; + private LogicalDatastoreType storeType; @Before - public void setup() throws UnknownHostException { + public void setup() throws Exception { initMocks(this); system = ActorSystem.create(); @@ -94,12 +95,14 @@ public class ReadOnlyTransactionTest { sourceIdentifiers = Lists.newArrayList(); //device read tx - readTx = mock(DOMDataReadOnlyTransaction.class); doReturn(readTx).when(deviceDataBroker).newReadOnlyTransaction(); // Create slave data broker for testing proxy slaveDataBroker = new ProxyDOMDataBroker(system, remoteDeviceId, masterRef, Timeout.apply(5, TimeUnit.SECONDS)); + initializeDataTest(); + instanceIdentifier = YangInstanceIdentifier.EMPTY; + storeType = LogicalDatastoreType.CONFIGURATION; } @After @@ -110,19 +113,29 @@ public class ReadOnlyTransactionTest { @Test public void testRead() throws Exception { + // Message: NormalizedNodeMessage + final NormalizedNode outputNode = ImmutableContainerNodeBuilder.create() + .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname"))) + .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build(); + final CheckedFuture>, ReadFailedException> resultNormalizedNodeMessage = + Futures.immediateCheckedFuture(Optional.of(outputNode)); + doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier); - /* Initialize data on master */ + final CheckedFuture>, ReadFailedException> resultNodeMessageResponse = + slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier); - initializeDataTest(); + final Optional> resultNodeMessage = + resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); - final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY; - final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION; + assertTrue(resultNodeMessage.isPresent()); + assertEquals(resultNodeMessage.get(), outputNode); + } + @Test + public void testReadEmpty() throws Exception { // Message: EmptyReadResponse - final CheckedFuture>, ReadFailedException> resultEmpty = Futures.immediateCheckedFuture(Optional.absent()); - doReturn(resultEmpty).when(readTx).read(storeType, instanceIdentifier); final CheckedFuture>, ReadFailedException> resultEmptyResponse = @@ -133,29 +146,11 @@ public class ReadOnlyTransactionTest { resultEmptyResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); assertEquals(resultEmptyMessage, Optional.absent()); + } - // Message: NormalizedNodeMessage - - final NormalizedNode outputNode = ImmutableContainerNodeBuilder.create() - .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("TestQname"))) - .withChild(ImmutableNodes.leafNode(QName.create("NodeQname"), "foo")).build(); - - final CheckedFuture>, ReadFailedException> resultNormalizedNodeMessage = - Futures.immediateCheckedFuture(Optional.of(outputNode)); - - doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier); - - final CheckedFuture>, ReadFailedException> resultNodeMessageResponse = - slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier); - - final Optional> resultNodeMessage = - resultNodeMessageResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); - - assertTrue(resultNodeMessage.isPresent()); - assertEquals(resultNodeMessage.get(), outputNode); - + @Test + public void testReadFail() throws Exception { // Message: Throwable - final ReadFailedException readFailedException = new ReadFailedException("Fail", null); final CheckedFuture>, ReadFailedException> resultThrowable = Futures.immediateFailedCheckedFuture(readFailedException); @@ -167,24 +162,13 @@ public class ReadOnlyTransactionTest { exception.expect(ReadFailedException.class); resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); - } @Test public void testExist() throws Exception { - - /* Initialize data on master */ - - initializeDataTest(); - - final YangInstanceIdentifier instanceIdentifier = YangInstanceIdentifier.EMPTY; - final LogicalDatastoreType storeType = LogicalDatastoreType.CONFIGURATION; - // Message: True - final CheckedFuture resultTrue = Futures.immediateCheckedFuture(true); - doReturn(resultTrue).when(readTx).exists(storeType, instanceIdentifier); final CheckedFuture trueResponse = @@ -193,25 +177,12 @@ public class ReadOnlyTransactionTest { final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); assertEquals(true, trueMessage); + } - // Message: False - - final CheckedFuture resultFalse = Futures.immediateCheckedFuture(false); - - doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier); - - final CheckedFuture falseResponse = - slaveDataBroker.newReadOnlyTransaction().exists(storeType, - instanceIdentifier); - - final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); - - assertEquals(false, falseMessage); - + @Test + public void testExistsNull() throws Exception { // Message: False, result null - final CheckedFuture resultNull = Futures.immediateCheckedFuture(null); - doReturn(resultNull).when(readTx).exists(storeType, instanceIdentifier); final CheckedFuture nullResponse = @@ -221,13 +192,29 @@ public class ReadOnlyTransactionTest { final Boolean nullFalseMessage = nullResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); assertEquals(false, nullFalseMessage); + } - // Message: Throwable + @Test + public void testExistsFalse() throws Exception { + // Message: False + final CheckedFuture resultFalse = Futures.immediateCheckedFuture(false); + doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier); + final CheckedFuture falseResponse = + slaveDataBroker.newReadOnlyTransaction().exists(storeType, + instanceIdentifier); + + final Boolean falseMessage = falseResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); + + assertEquals(false, falseMessage); + } + + @Test + public void testExistsFail() throws Exception { + // Message: Throwable final ReadFailedException readFailedException = new ReadFailedException("Fail", null); final CheckedFuture resultThrowable = Futures.immediateFailedCheckedFuture(readFailedException); - doReturn(resultThrowable).when(readTx).exists(storeType, instanceIdentifier); final CheckedFuture resultThrowableResponse = @@ -235,7 +222,6 @@ public class ReadOnlyTransactionTest { exception.expect(ReadFailedException.class); resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS); - } private void initializeDataTest() throws Exception { 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 deb8235d7d..fb89b2cd5e 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,7 +11,6 @@ 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; @@ -32,7 +31,6 @@ 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; @@ -72,18 +70,21 @@ public class WriteOnlyTransactionTest { @Rule public final ExpectedException exception = ExpectedException.none(); - private ActorRef masterRef; - private ProxyDOMDataBroker slaveDataBroker; - private List sourceIdentifiers; @Mock private DOMDataBroker deviceDataBroker; @Mock private DOMDataWriteTransaction writeTx; @Mock private DOMRpcService domRpcService; + 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(); @@ -100,15 +101,23 @@ public class WriteOnlyTransactionTest { sourceIdentifiers = Lists.newArrayList(); - writeTx = mock(DOMDataWriteTransaction.class); final DOMDataReadOnlyTransaction readTx = mock(DOMDataReadOnlyTransaction.class); 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 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 @@ -119,85 +128,46 @@ public class WriteOnlyTransactionTest { @Test public void testPut() 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 put on master through slave proxy - - doNothing().when(writeTx).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); final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); wTx.merge(storeType, instanceIdentifier, testNode); verify(writeTx, timeout(2000)).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); + // Test of invoking delete on master through slave proxy final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); wTx.delete(storeType, instanceIdentifier); wTx.cancel(); verify(writeTx, timeout(2000)).delete(storeType, instanceIdentifier); - } @Test public void testSubmit() throws Exception { - - /* Initialize data on master */ - - initializeDataTest(); + final CheckedFuture resultSubmit = Futures.immediateCheckedFuture(null); + doReturn(resultSubmit).when(writeTx).submit(); // Without Tx - final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); - final CheckedFuture resultSubmit = Futures.immediateCheckedFuture(null); - doReturn(resultSubmit).when(writeTx).submit(); final CheckedFuture resultSubmitResponse = wTx.submit(); @@ -208,19 +178,13 @@ public class WriteOnlyTransactionTest { @Test public void testSubmitWithOperation() throws Exception { - - /* Initialize data on master */ - - initializeDataTest(); + final CheckedFuture resultSubmitTx = Futures.immediateCheckedFuture(null); + doReturn(resultSubmitTx).when(writeTx).submit(); // With Tx final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); - doNothing().when(writeTx).delete(any(), any()); 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); @@ -230,36 +194,25 @@ public class WriteOnlyTransactionTest { @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); - 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 - doReturn(true).when(writeTx).cancel(); final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); final Boolean resultFalseNoTx = wTx.cancel(); assertEquals(true, resultFalseNoTx); @@ -267,25 +220,18 @@ public class WriteOnlyTransactionTest { @Test public void testCancelWithOperation() throws Exception { - - /* Initialize data on master */ - - initializeDataTest(); + doReturn(true).when(writeTx).cancel(); // With Tx, readWriteTx test - final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction(); - doNothing().when(writeTx).delete(any(), any()); wTx.delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY); - doReturn(true).when(writeTx).cancel(); final Boolean resultTrue = wTx.cancel(); assertEquals(true, resultTrue); final Boolean resultFalse = wTx.cancel(); assertEquals(false, resultFalse); - } private void initializeDataTest() throws Exception { -- 2.36.6