Merge "Fix problems with checkstyle in interfacemanager-api module"
[genius.git] / mdsalutil / mdsalutil-api / src / test / java / org / opendaylight / genius / mdsalutil / interfaces / testutils / TestIMdsalApiManager.java
index fe91b98c9d4701eead45f415dd0addca3d79923a..3e58e65b4cc51e30a2877251d5e3f4ecca0af7fc 100644 (file)
@@ -10,12 +10,16 @@ package org.opendaylight.genius.mdsalutil.interfaces.testutils;
 import static org.junit.Assert.assertTrue;
 import static org.opendaylight.yangtools.testutils.mockito.MoreAnswers.realOrException;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Lists;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+import java.math.BigInteger;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.genius.mdsalutil.FlowEntity;
 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
 import org.opendaylight.mdsal.binding.testutils.AssertDataObjects;
@@ -41,30 +45,42 @@ public abstract class TestIMdsalApiManager implements IMdsalApiManager {
         return Mockito.mock(TestIMdsalApiManager.class, realOrException());
     }
 
-    private synchronized List<FlowEntity> initializeFlows() {
-        return Collections.synchronizedList(new ArrayList<>());
+    /**
+     * Get list of installed flows.
+     * Prefer the {@link #assertFlows(Iterable)} instead of using this and checking yourself.
+     * @return immutable copy of list of flows
+     */
+    public synchronized List<FlowEntity> getFlows() {
+        return ImmutableList.copyOf(getOrNewFlows());
     }
 
-    public List<FlowEntity> getFlows() {
+    private synchronized List<FlowEntity> getOrNewFlows() {
         if (flows == null) {
-            flows = initializeFlows();
+            flows = new ArrayList<>();
         }
         return flows;
     }
 
-    public void assertFlows(Iterable<FlowEntity> expectedFlows) {
-        List<FlowEntity> flows = this.getFlows();
+    public synchronized void assertFlows(Iterable<FlowEntity> expectedFlows) {
+        List<FlowEntity> nonNullFlows = getOrNewFlows();
         if (!Iterables.isEmpty(expectedFlows)) {
-            assertTrue("No Flows created (bean wiring may be broken?)", !flows.isEmpty());
+            assertTrue("No Flows created (bean wiring may be broken?)", !nonNullFlows.isEmpty());
         }
         // TODO Support Iterable <-> List directly within XtendBeanGenerator
         List<FlowEntity> expectedFlowsAsNewArrayList = Lists.newArrayList(expectedFlows);
-        AssertDataObjects.assertEqualBeans(expectedFlowsAsNewArrayList, flows);
+        AssertDataObjects.assertEqualBeans(expectedFlowsAsNewArrayList, nonNullFlows);
     }
 
     @Override
-    public void installFlow(FlowEntity flowEntity) {
-        getFlows().add(flowEntity);
+    public synchronized void installFlow(FlowEntity flowEntity) {
+        getOrNewFlows().add(flowEntity);
+    }
+
+    @Override
+    public synchronized CheckedFuture<Void, TransactionCommitFailedException> removeFlow(BigInteger dpnId,
+            FlowEntity flowEntity) {
+        getOrNewFlows().remove(flowEntity);
+        return Futures.immediateCheckedFuture(null);
     }
 
 }