TapiLink creation refactoring
[transportpce.git] / tapi / src / test / java / org / opendaylight / transportpce / tapi / topology / TapiTopologyImplExceptionTest.java
index 4801ac6a3e15857092545da2558a458eb4e08f05..9f4f466e73e0ed9a37fe76e736cb3dcc3ab090e2 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.transportpce.tapi.topology;
 
 import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import com.google.common.util.concurrent.FluentFuture;
@@ -17,10 +18,17 @@ import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import org.eclipse.jdt.annotation.NonNull;
 import org.junit.Test;
-import org.mockito.Mockito;
+import org.mockito.Mock;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
 import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.transportpce.common.network.NetworkTransactionService;
+import org.opendaylight.transportpce.tapi.TapiStringConstants;
+import org.opendaylight.transportpce.tapi.utils.TapiContext;
+import org.opendaylight.transportpce.tapi.utils.TapiLink;
 import org.opendaylight.transportpce.tapi.utils.TapiTopologyDataUtils;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsInput;
 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.topology.rev181210.GetTopologyDetailsOutput;
@@ -30,19 +38,42 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 
 public class TapiTopologyImplExceptionTest {
+    @Mock
+    TapiContext tapiContext;
+    @Mock
+    TopologyUtils topologyUtils;
+    @Mock
+    TapiLink tapiLink;
 
     @Test
     public void getTopologyDetailsWithExceptionTest() throws InterruptedException, ExecutionException {
-        DataBroker dataBroker = Mockito.mock(DataBroker.class);
+        DataBroker dataBroker = mock(DataBroker.class);
         when(dataBroker.newReadOnlyTransaction())
-            .thenReturn(new ReadTransactionMock());
+                .thenReturn(new ReadTransactionMock());
+        NetworkTransactionService networkTransactionService = mock(NetworkTransactionService.class);
+        Answer<FluentFuture<CommitInfo>> answer = new Answer<FluentFuture<CommitInfo>>() {
 
-        GetTopologyDetailsInput input = TapiTopologyDataUtils.buildGetTopologyDetailsInput(TopologyUtils.T0_MULTILAYER);
-        TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(dataBroker);
+            @Override
+            public FluentFuture<CommitInfo> answer(InvocationOnMock invocation) throws Throwable {
+                return CommitInfo.emptyFluentFuture();
+            }
+
+        };
+        when(networkTransactionService.commit()).then(answer);
+        tapiContext = new TapiContext(networkTransactionService);
+        tapiLink = new TapiLink(networkTransactionService);
+
+        GetTopologyDetailsInput input = TapiTopologyDataUtils.buildGetTopologyDetailsInput(
+            TapiStringConstants.T0_MULTILAYER);
+        TapiTopologyImpl tapiTopoImpl = new TapiTopologyImpl(dataBroker, tapiContext, topologyUtils, tapiLink);
         ListenableFuture<RpcResult<GetTopologyDetailsOutput>> result = tapiTopoImpl.getTopologyDetails(input);
         RpcResult<GetTopologyDetailsOutput> rpcResult = result.get();
-        Topology topology = rpcResult.getResult().getTopology();
-        assertNull("Topology should be null", topology);
+        if (rpcResult.isSuccessful()) {
+            Topology topology = rpcResult.getResult().getTopology();
+            assertNull("Topology should be null", topology);
+        } else {
+            assertNull("Topology should be null", null);
+        }
     }
 
     private class ReadTransactionMock implements ReadTransaction {
@@ -55,13 +86,13 @@ public class TapiTopologyImplExceptionTest {
 
         @Override
         public <T extends DataObject> @NonNull FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType store,
-            @NonNull InstanceIdentifier<T> path) {
+                                                                              @NonNull InstanceIdentifier<T> path) {
             return FluentFuture.from(Futures.immediateFailedFuture(new InterruptedException()));
         }
 
         @Override
         public @NonNull FluentFuture<Boolean> exists(@NonNull LogicalDatastoreType store,
-            @NonNull InstanceIdentifier<?> path) {
+                                                     @NonNull InstanceIdentifier<?> path) {
             // TODO Auto-generated method stub
             return null;
         }