Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorSnapshotMessageSupportTest.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.Matchers.anyLong;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Matchers.same;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.verify;
16
17 import akka.actor.ActorRef;
18 import akka.persistence.SaveSnapshotFailure;
19 import akka.persistence.SaveSnapshotSuccess;
20 import akka.persistence.SnapshotMetadata;
21 import java.util.Collections;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.cluster.DataPersistenceProvider;
27 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
28 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
29 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Unit tests for RaftActorSnapshotMessageSupport.
35  *
36  * @author Thomas Pantelis
37  */
38 public class RaftActorSnapshotMessageSupportTest {
39
40     private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
41
42     @Mock
43     private DataPersistenceProvider mockPersistence;
44
45     @Mock
46     private RaftActorBehavior mockBehavior;
47
48     @Mock
49     private RaftActorSnapshotCohort mockCohort;
50
51     @Mock
52     private SnapshotManager mockSnapshotManager;
53
54     @Mock
55     ActorRef mockRaftActorRef;
56
57     private RaftActorSnapshotMessageSupport support;
58
59     private RaftActorContext context;
60     private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
61
62     @Before
63     public void setup() {
64         MockitoAnnotations.initMocks(this);
65
66         context = new RaftActorContextImpl(mockRaftActorRef, null, "test",
67                 new ElectionTermImpl(mockPersistence, "test", LOG),
68                 -1, -1, Collections.<String,String>emptyMap(), configParams, mockPersistence, LOG) {
69             @Override
70             public SnapshotManager getSnapshotManager() {
71                 return mockSnapshotManager;
72             }
73         };
74
75         support = new RaftActorSnapshotMessageSupport(context, mockCohort);
76
77         doReturn(true).when(mockPersistence).isRecoveryApplicable();
78
79         context.setReplicatedLog(ReplicatedLogImpl.newInstance(context));
80     }
81
82     private void sendMessageToSupport(Object message) {
83         sendMessageToSupport(message, true);
84     }
85
86     private void sendMessageToSupport(Object message, boolean expHandled) {
87         boolean handled = support.handleSnapshotMessage(message, mockRaftActorRef);
88         assertEquals("complete", expHandled, handled);
89     }
90
91     @Test
92     public void testOnApplySnapshot() {
93
94         long lastAppliedDuringSnapshotCapture = 1;
95         long lastIndexDuringSnapshotCapture = 2;
96         byte[] snapshotBytes = {1,2,3,4,5};
97
98         Snapshot snapshot = Snapshot.create(snapshotBytes, Collections.<ReplicatedLogEntry>emptyList(),
99                 lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1);
100
101         ApplySnapshot applySnapshot = new ApplySnapshot(snapshot);
102         sendMessageToSupport(applySnapshot);
103
104         verify(mockSnapshotManager).apply(applySnapshot);
105     }
106
107     @Test
108     public void testOnCaptureSnapshotReply() {
109
110         byte[] snapshot = {1,2,3,4,5};
111         sendMessageToSupport(new CaptureSnapshotReply(snapshot));
112
113         verify(mockSnapshotManager).persist(same(snapshot), anyLong());
114     }
115
116     @Test
117     public void testOnSaveSnapshotSuccess() {
118
119         long sequenceNumber = 100;
120         long timeStamp = 1234L;
121         sendMessageToSupport(new SaveSnapshotSuccess(new SnapshotMetadata("foo", sequenceNumber, timeStamp)));
122
123         verify(mockSnapshotManager).commit(eq(sequenceNumber), eq(timeStamp));
124     }
125
126     @Test
127     public void testOnSaveSnapshotFailure() {
128
129         sendMessageToSupport(new SaveSnapshotFailure(new SnapshotMetadata("foo", 100, 1234L),
130                 new Throwable("mock")));
131
132         verify(mockSnapshotManager).rollback();
133     }
134
135     @Test
136     public void testOnCommitSnapshot() {
137
138         sendMessageToSupport(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
139
140         verify(mockSnapshotManager).commit(eq(-1L), eq(-1L));
141     }
142
143     @Test
144     public void testUnhandledMessage() {
145
146         sendMessageToSupport("unhandled", false);
147     }
148 }

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