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.any;
12 import static org.mockito.Mockito.doAnswer;
13 import static org.mockito.Mockito.verify;
14 import akka.japi.Procedure;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations;
20 import org.mockito.invocation.InvocationOnMock;
21 import org.mockito.stubbing.Answer;
22 import org.opendaylight.controller.cluster.DataPersistenceProvider;
23 import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Unit tests for ElectionTermImpl.
30 * @author Thomas Pantelis
32 public class ElectionTermImplTest {
33 private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
36 private DataPersistenceProvider mockPersistence;
38 @SuppressWarnings("unchecked")
41 MockitoAnnotations.initMocks(this);
43 doAnswer(new Answer<Void>() {
45 public Void answer(InvocationOnMock invocation) throws Exception {
46 final Object[] args = invocation.getArguments();
47 ((Procedure<Object>)args[1]).apply(args[0]);
50 }).when(mockPersistence).persist(any(Object.class), any(Procedure.class));
53 @SuppressWarnings({ "rawtypes", "unchecked" })
55 public void testUpdateAndPersist() throws Exception {
56 ElectionTermImpl impl = new ElectionTermImpl(mockPersistence, "test", LOG);
58 impl.updateAndPersist(10, "member-1");
60 assertEquals("getCurrentTerm", 10, impl.getCurrentTerm());
61 assertEquals("getVotedFor", "member-1", impl.getVotedFor());
63 ArgumentCaptor<Object> message = ArgumentCaptor.forClass(Object.class);
64 ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
65 verify(mockPersistence).persist(message.capture(), procedure.capture());
67 assertEquals("Message type", UpdateElectionTerm.class, message.getValue().getClass());
68 UpdateElectionTerm update = (UpdateElectionTerm)message.getValue();
69 assertEquals("getCurrentTerm", 10, update.getCurrentTerm());
70 assertEquals("getVotedFor", "member-1", update.getVotedFor());
72 procedure.getValue().apply(null);