Fix unit test errors due to Mockito upgrade
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / MockRaftActor.java
index 38650e834f1eb015a0831a6078dd88bf904a22ba..c1a87e8c7b283e1f83b9114471dba911426127cb 100644 (file)
@@ -12,6 +12,7 @@ import static org.junit.Assert.assertEquals;
 import static org.mockito.Mockito.mock;
 import akka.actor.ActorRef;
 import akka.actor.Props;
+import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.util.concurrent.Uninterruptibles;
 import java.io.ByteArrayInputStream;
@@ -29,7 +30,6 @@ import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
 
 public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort, RaftActorSnapshotCohort {
-
     public static final short PAYLOAD_VERSION = 5;
 
     final RaftActor actorDelegate;
@@ -43,8 +43,9 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
     private RaftActorSnapshotMessageSupport snapshotMessageSupport;
     private final byte[] restoreFromSnapshot;
     final CountDownLatch snapshotCommitted = new CountDownLatch(1);
+    private final Function<Runnable, Void> pauseLeaderFunction;
 
-    protected MockRaftActor(Builder builder) {
+    protected MockRaftActor(AbstractBuilder<?, ?> builder) {
         super(builder.id, builder.peerAddresses, Optional.fromNullable(builder.config), PAYLOAD_VERSION);
         state = new ArrayList<>();
         this.actorDelegate = mock(RaftActor.class);
@@ -60,6 +61,7 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
         roleChangeNotifier = builder.roleChangeNotifier;
         snapshotMessageSupport = builder.snapshotMessageSupport;
         restoreFromSnapshot = builder.restoreFromSnapshot;
+        pauseLeaderFunction = builder.pauseLeaderFunction;
     }
 
     public void setRaftActorRecoverySupport(RaftActorRecoverySupport support) {
@@ -216,6 +218,15 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
         }
     }
 
+    @Override
+    protected void pauseLeader(Runnable operation) {
+        if(pauseLeaderFunction != null) {
+            pauseLeaderFunction.apply(operation);
+        } else {
+            super.pauseLeader(operation);
+        }
+    }
+
     public static Object toObject(byte[] bs) throws ClassNotFoundException, IOException {
         Object obj = null;
         ByteArrayInputStream bis = null;
@@ -259,7 +270,7 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
         return new Builder();
     }
 
-    public static class Builder {
+    public static class AbstractBuilder<T extends AbstractBuilder<T, A>, A extends MockRaftActor> {
         private Map<String, String> peerAddresses = Collections.emptyMap();
         private String id;
         private ConfigParams config;
@@ -268,49 +279,71 @@ public class MockRaftActor extends RaftActor implements RaftActorRecoveryCohort,
         private RaftActorSnapshotMessageSupport snapshotMessageSupport;
         private byte[] restoreFromSnapshot;
         private Optional<Boolean> persistent = Optional.absent();
+        private final Class<A> actorClass;
+        private Function<Runnable, Void> pauseLeaderFunction;
+
+        protected AbstractBuilder(Class<A> actorClass) {
+            this.actorClass = actorClass;
+        }
 
-        public Builder id(String id) {
+        @SuppressWarnings("unchecked")
+        private T self() {
+            return (T) this;
+        }
+
+        public T id(String id) {
             this.id = id;
-            return this;
+            return self();
         }
 
-        public Builder peerAddresses(Map<String, String> peerAddresses) {
+        public T peerAddresses(Map<String, String> peerAddresses) {
             this.peerAddresses = peerAddresses;
-            return this;
+            return self();
         }
 
-        public Builder config(ConfigParams config) {
+        public T config(ConfigParams config) {
             this.config = config;
-            return this;
+            return self();
         }
 
-        public Builder dataPersistenceProvider(DataPersistenceProvider dataPersistenceProvider) {
+        public T dataPersistenceProvider(DataPersistenceProvider dataPersistenceProvider) {
             this.dataPersistenceProvider = dataPersistenceProvider;
-            return this;
+            return self();
         }
 
-        public Builder roleChangeNotifier(ActorRef roleChangeNotifier) {
+        public T roleChangeNotifier(ActorRef roleChangeNotifier) {
             this.roleChangeNotifier = roleChangeNotifier;
-            return this;
+            return self();
         }
 
-        public Builder snapshotMessageSupport(RaftActorSnapshotMessageSupport snapshotMessageSupport) {
+        public T snapshotMessageSupport(RaftActorSnapshotMessageSupport snapshotMessageSupport) {
             this.snapshotMessageSupport = snapshotMessageSupport;
-            return this;
+            return self();
         }
 
-        public Builder restoreFromSnapshot(byte[] restoreFromSnapshot) {
+        public T restoreFromSnapshot(byte[] restoreFromSnapshot) {
             this.restoreFromSnapshot = restoreFromSnapshot;
-            return this;
+            return self();
         }
 
-        public Builder persistent(Optional<Boolean> persistent) {
+        public T persistent(Optional<Boolean> persistent) {
             this.persistent = persistent;
-            return this;
+            return self();
+        }
+
+        public T pauseLeaderFunction(Function<Runnable, Void> pauseLeaderFunction) {
+            this.pauseLeaderFunction = pauseLeaderFunction;
+            return self();
         }
 
         public Props props() {
-            return Props.create(MockRaftActor.class, this);
+            return Props.create(actorClass, this);
+        }
+    }
+
+    public static class Builder extends AbstractBuilder<Builder, MockRaftActor> {
+        private Builder() {
+            super(MockRaftActor.class);
         }
     }
 }