From: Tom Pantelis Date: Sun, 29 Mar 2015 12:23:38 +0000 (-0400) Subject: Add unit test for ElectionTermImpl X-Git-Tag: release/lithium~277^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=d72a15b68533728c36fa23b6ffe254e3da2d3d50 Add unit test for ElectionTermImpl Added a unit test class ElectionTermImplTest and removed the equivalent test case from RaftActorTest. Change-Id: I1c3f0e59784422777d742ade4c01e1e18914a213 Signed-off-by: Tom Pantelis --- diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/ElectionTermImplTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/ElectionTermImplTest.java new file mode 100644 index 0000000000..da49718a41 --- /dev/null +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/ElectionTermImplTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.raft; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.verify; +import akka.japi.Procedure; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.cluster.DataPersistenceProvider; +import org.opendaylight.controller.cluster.raft.RaftActor.UpdateElectionTerm; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Unit tests for ElectionTermImpl. + * + * @author Thomas Pantelis + */ +public class ElectionTermImplTest { + private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class); + + @Mock + private DataPersistenceProvider mockPersistence; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + } + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + public void testUpdateAndPersist() throws Exception { + ElectionTermImpl impl = new ElectionTermImpl(mockPersistence, "test", LOG); + + impl.updateAndPersist(10, "member-1"); + + assertEquals("getCurrentTerm", 10, impl.getCurrentTerm()); + assertEquals("getVotedFor", "member-1", impl.getVotedFor()); + + ArgumentCaptor message = ArgumentCaptor.forClass(Object.class); + ArgumentCaptor procedure = ArgumentCaptor.forClass(Procedure.class); + verify(mockPersistence).persist(message.capture(), procedure.capture()); + + assertEquals("Message type", UpdateElectionTerm.class, message.getValue().getClass()); + UpdateElectionTerm update = (UpdateElectionTerm)message.getValue(); + assertEquals("getCurrentTerm", 10, update.getCurrentTerm()); + assertEquals("getVotedFor", "member-1", update.getVotedFor()); + + procedure.getValue().apply(null); + } +} diff --git a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTest.java b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTest.java index 44dffe352c..2928266648 100644 --- a/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTest.java +++ b/opendaylight/md-sal/sal-akka-raft/src/test/java/org/opendaylight/controller/cluster/raft/RaftActorTest.java @@ -35,14 +35,12 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.opendaylight.controller.cluster.DataPersistenceProvider; import org.opendaylight.controller.cluster.NonPersistentDataProvider; -import org.opendaylight.controller.cluster.datastore.DataPersistenceProviderMonitor; import org.opendaylight.controller.cluster.notifications.LeaderStateChanged; import org.opendaylight.controller.cluster.notifications.RoleChanged; import org.opendaylight.controller.cluster.raft.RaftActor.DeleteEntries; @@ -299,34 +297,6 @@ public class RaftActorTest extends AbstractActorTest { verify(mockSupport).handleSnapshotMessage(same(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT)); } - @Test - public void testUpdatingElectionTermCallsDataPersistence() throws Exception { - new JavaTestKit(getSystem()) { - { - String persistenceId = factory.generateActorId("leader-"); - - DefaultConfigParamsImpl config = new DefaultConfigParamsImpl(); - - config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS)); - - CountDownLatch persistLatch = new CountDownLatch(1); - DataPersistenceProviderMonitor dataPersistenceProviderMonitor = new DataPersistenceProviderMonitor(); - dataPersistenceProviderMonitor.setPersistLatch(persistLatch); - - TestActorRef mockActorRef = factory.createTestActor(MockRaftActor.props(persistenceId, - Collections.emptyMap(), Optional.of(config), dataPersistenceProviderMonitor), persistenceId); - - MockRaftActor mockRaftActor = mockActorRef.underlyingActor(); - - mockRaftActor.waitForInitializeBehaviorComplete(); - - mockRaftActor.getRaftActorContext().getTermInformation().updateAndPersist(10, "foobar"); - - assertEquals("Persist called", true, persistLatch.await(5, TimeUnit.SECONDS)); - } - }; - } - @Test public void testApplyJournalEntriesCallsDataPersistence() throws Exception { new JavaTestKit(getSystem()) {