2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.raft;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.verify;
12 import akka.japi.Procedure;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.ArgumentCaptor;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18 import org.opendaylight.controller.cluster.DataPersistenceProvider;
19 import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Unit tests for ElectionTermImpl.
26 * @author Thomas Pantelis
28 public class ElectionTermImplTest {
29 private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
32 private DataPersistenceProvider mockPersistence;
36 MockitoAnnotations.initMocks(this);
39 @SuppressWarnings({ "rawtypes", "unchecked" })
41 public void testUpdateAndPersist() throws Exception {
42 ElectionTermImpl impl = new ElectionTermImpl(mockPersistence, "test", LOG);
44 impl.updateAndPersist(10, "member-1");
46 assertEquals("getCurrentTerm", 10, impl.getCurrentTerm());
47 assertEquals("getVotedFor", "member-1", impl.getVotedFor());
49 ArgumentCaptor<Object> message = ArgumentCaptor.forClass(Object.class);
50 ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
51 verify(mockPersistence).persist(message.capture(), procedure.capture());
53 assertEquals("Message type", UpdateElectionTerm.class, message.getValue().getClass());
54 UpdateElectionTerm update = (UpdateElectionTerm)message.getValue();
55 assertEquals("getCurrentTerm", 10, update.getCurrentTerm());
56 assertEquals("getVotedFor", "member-1", update.getVotedFor());
58 procedure.getValue().apply(null);