Refactor netconf clustered topology tests 04/56004/1
authorAndrej Mak <andrej.mak@pantheon.tech>
Tue, 25 Apr 2017 06:19:53 +0000 (08:19 +0200)
committerJakub Morvay <jmorvay@cisco.com>
Tue, 25 Apr 2017 14:13:15 +0000 (14:13 +0000)
- split long tests into smaller
- remove duplicated code

Change-Id: I64913380fd0768394b430971ef5ff118d56b7b6b
Signed-off-by: Andrej Mak <andrej.mak@pantheon.tech>
netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/ReadOnlyTransactionTest.java
netconf/netconf-topology-singleton/src/test/java/org/opendaylight/netconf/topology/singleton/impl/tx/WriteOnlyTransactionTest.java

index 1dafc59438c8d18be3b564eef3319096bb2097c9..79e5001e3612c417be5b23f3bfd53a7d55ffe5b9 100644 (file)
@@ -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<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
+                Futures.immediateCheckedFuture(Optional.of(outputNode));
+        doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier);
 
-        /* Initialize data on master */
+        final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
+                slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
 
-        initializeDataTest();
+        final Optional<NormalizedNode<?, ?>> 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<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultEmpty =
                 Futures.immediateCheckedFuture(Optional.absent());
-
         doReturn(resultEmpty).when(readTx).read(storeType, instanceIdentifier);
 
         final CheckedFuture<Optional<NormalizedNode<?, ?>>, 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<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNormalizedNodeMessage =
-                Futures.immediateCheckedFuture(Optional.of(outputNode));
-
-        doReturn(resultNormalizedNodeMessage).when(readTx).read(storeType, instanceIdentifier);
-
-        final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> resultNodeMessageResponse =
-                slaveDataBroker.newReadOnlyTransaction().read(storeType, instanceIdentifier);
-
-        final Optional<NormalizedNode<?, ?>> 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<Optional<NormalizedNode<?, ?>>, 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<Boolean, ReadFailedException> resultTrue =
                 Futures.immediateCheckedFuture(true);
-
         doReturn(resultTrue).when(readTx).exists(storeType, instanceIdentifier);
 
         final CheckedFuture<Boolean, ReadFailedException> trueResponse =
@@ -193,25 +177,12 @@ public class ReadOnlyTransactionTest {
         final Boolean trueMessage = trueResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
 
         assertEquals(true, trueMessage);
+    }
 
-        // Message: False
-
-        final CheckedFuture<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
-
-        doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier);
-
-        final CheckedFuture<Boolean, ReadFailedException> 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<Boolean, ReadFailedException> resultNull = Futures.immediateCheckedFuture(null);
-
         doReturn(resultNull).when(readTx).exists(storeType, instanceIdentifier);
 
         final CheckedFuture<Boolean, ReadFailedException> 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<Boolean, ReadFailedException> resultFalse = Futures.immediateCheckedFuture(false);
+        doReturn(resultFalse).when(readTx).exists(storeType, instanceIdentifier);
 
+        final CheckedFuture<Boolean, ReadFailedException> 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<Boolean, ReadFailedException> resultThrowable =
                 Futures.immediateFailedCheckedFuture(readFailedException);
-
         doReturn(resultThrowable).when(readTx).exists(storeType, instanceIdentifier);
 
         final CheckedFuture<Boolean, ReadFailedException> resultThrowableResponse =
@@ -235,7 +222,6 @@ public class ReadOnlyTransactionTest {
 
         exception.expect(ReadFailedException.class);
         resultThrowableResponse.checkedGet(TIMEOUT_SEC, TimeUnit.SECONDS);
-
     }
 
     private void initializeDataTest() throws Exception {
index deb8235d7d0d8d0863769a35f85d278cfa10fa13..fb89b2cd5ee0c933ea9626d7d4f716156b20fe21 100644 (file)
@@ -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<SourceIdentifier> sourceIdentifiers;
     @Mock
     private DOMDataBroker deviceDataBroker;
     @Mock
     private DOMDataWriteTransaction writeTx;
     @Mock
     private DOMRpcService domRpcService;
+    private ActorRef masterRef;
+    private ProxyDOMDataBroker slaveDataBroker;
+    private List<SourceIdentifier> 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<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
+        doReturn(resultSubmit).when(writeTx).submit();
 
         // Without Tx
-
         final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
-        final CheckedFuture<Void, TransactionCommitFailedException> resultSubmit = Futures.immediateCheckedFuture(null);
-        doReturn(resultSubmit).when(writeTx).submit();
 
         final CheckedFuture<Void, TransactionCommitFailedException> resultSubmitResponse = wTx.submit();
 
@@ -208,19 +178,13 @@ public class WriteOnlyTransactionTest {
 
     @Test
     public void testSubmitWithOperation() throws Exception {
-
-        /* Initialize data on master */
-
-        initializeDataTest();
+        final CheckedFuture<Void, TransactionCommitFailedException> 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<Void, TransactionCommitFailedException> resultSubmitTx = Futures.immediateCheckedFuture(null);
-        doReturn(resultSubmitTx).when(writeTx).submit();
-
         final CheckedFuture<Void, TransactionCommitFailedException> 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<Void,TransactionCommitFailedException> resultThrowable =
+        final CheckedFuture<Void, TransactionCommitFailedException> resultThrowable =
                 Futures.immediateFailedCheckedFuture(throwable);
-
         doReturn(resultThrowable).when(writeTx).submit();
 
+        final DOMDataWriteTransaction wTx = slaveDataBroker.newWriteOnlyTransaction();
+        wTx.delete(LogicalDatastoreType.CONFIGURATION,
+                YangInstanceIdentifier.EMPTY);
         final CheckedFuture<Void, TransactionCommitFailedException> 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 {