da49718a4142938b3ad0ababbed63d033a08c1d2
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / ElectionTermImplTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.raft;
9
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.RaftActor.UpdateElectionTerm;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Unit tests for ElectionTermImpl.
25  *
26  * @author Thomas Pantelis
27  */
28 public class ElectionTermImplTest {
29     private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
30
31     @Mock
32     private DataPersistenceProvider mockPersistence;
33
34     @Before
35     public void setup() {
36         MockitoAnnotations.initMocks(this);
37     }
38
39     @SuppressWarnings({ "rawtypes", "unchecked" })
40     @Test
41     public void testUpdateAndPersist() throws Exception {
42         ElectionTermImpl impl = new ElectionTermImpl(mockPersistence, "test", LOG);
43
44         impl.updateAndPersist(10, "member-1");
45
46         assertEquals("getCurrentTerm", 10, impl.getCurrentTerm());
47         assertEquals("getVotedFor", "member-1", impl.getVotedFor());
48
49         ArgumentCaptor<Object> message = ArgumentCaptor.forClass(Object.class);
50         ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
51         verify(mockPersistence).persist(message.capture(), procedure.capture());
52
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());
57
58         procedure.getValue().apply(null);
59     }
60 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.