Migrate sal-akka-raft tests
[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
13 import akka.japi.Procedure;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.mockito.ArgumentCaptor;
17 import org.mockito.Mock;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.opendaylight.controller.cluster.DataPersistenceProvider;
20 import org.opendaylight.controller.cluster.raft.persisted.UpdateElectionTerm;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Unit tests for ElectionTermImpl.
26  *
27  * @author Thomas Pantelis
28  */
29 @RunWith(MockitoJUnitRunner.StrictStubs.class)
30 public class ElectionTermImplTest {
31     private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
32
33     @Mock
34     private DataPersistenceProvider mockPersistence;
35
36     @Test
37     @SuppressWarnings({ "rawtypes", "unchecked" })
38     public void testUpdateAndPersist() throws Exception {
39         ElectionTermImpl impl = new ElectionTermImpl(mockPersistence, "test", LOG);
40
41         impl.updateAndPersist(10, "member-1");
42
43         assertEquals("getCurrentTerm", 10, impl.getCurrentTerm());
44         assertEquals("getVotedFor", "member-1", impl.getVotedFor());
45
46         ArgumentCaptor<Object> message = ArgumentCaptor.forClass(Object.class);
47         ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
48         verify(mockPersistence).persist(message.capture(), procedure.capture());
49
50         assertEquals("Message type", UpdateElectionTerm.class, message.getValue().getClass());
51         UpdateElectionTerm update = (UpdateElectionTerm)message.getValue();
52         assertEquals("getCurrentTerm", 10, update.getCurrentTerm());
53         assertEquals("getVotedFor", "member-1", update.getVotedFor());
54
55         procedure.getValue().apply(null);
56     }
57 }

©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.