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.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;
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;
34 * Unit tests for RaftActorSnapshotMessageSupport.
36 * @author Thomas Pantelis
38 public class RaftActorSnapshotMessageSupportTest {
40 private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
43 private DataPersistenceProvider mockPersistence;
46 private RaftActorBehavior mockBehavior;
49 private RaftActorSnapshotCohort mockCohort;
52 private SnapshotManager mockSnapshotManager;
55 ActorRef mockRaftActorRef;
57 private RaftActorSnapshotMessageSupport support;
59 private RaftActorContext context;
60 private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
64 MockitoAnnotations.initMocks(this);
66 context = new RaftActorContextImpl(mockRaftActorRef, null, "test",
67 new ElectionTermImpl(mockPersistence, "test", LOG),
68 -1, -1, Collections.<String,String>emptyMap(), configParams, mockPersistence, LOG) {
70 public SnapshotManager getSnapshotManager() {
71 return mockSnapshotManager;
75 support = new RaftActorSnapshotMessageSupport(context, mockCohort);
77 doReturn(true).when(mockPersistence).isRecoveryApplicable();
79 context.setReplicatedLog(ReplicatedLogImpl.newInstance(context));
82 private void sendMessageToSupport(Object message) {
83 sendMessageToSupport(message, true);
86 private void sendMessageToSupport(Object message, boolean expHandled) {
87 boolean handled = support.handleSnapshotMessage(message, mockRaftActorRef);
88 assertEquals("complete", expHandled, handled);
92 public void testOnApplySnapshot() {
94 long lastAppliedDuringSnapshotCapture = 1;
95 long lastIndexDuringSnapshotCapture = 2;
96 byte[] snapshotBytes = {1,2,3,4,5};
98 Snapshot snapshot = Snapshot.create(snapshotBytes, Collections.<ReplicatedLogEntry>emptyList(),
99 lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1);
101 ApplySnapshot applySnapshot = new ApplySnapshot(snapshot);
102 sendMessageToSupport(applySnapshot);
104 verify(mockSnapshotManager).apply(applySnapshot);
108 public void testOnCaptureSnapshotReply() {
110 byte[] snapshot = {1,2,3,4,5};
111 sendMessageToSupport(new CaptureSnapshotReply(snapshot));
113 verify(mockSnapshotManager).persist(same(snapshot), anyLong());
117 public void testOnSaveSnapshotSuccess() {
119 long sequenceNumber = 100;
120 long timeStamp = 1234L;
121 sendMessageToSupport(new SaveSnapshotSuccess(new SnapshotMetadata("foo", sequenceNumber, timeStamp)));
123 verify(mockSnapshotManager).commit(eq(sequenceNumber), eq(timeStamp));
127 public void testOnSaveSnapshotFailure() {
129 sendMessageToSupport(new SaveSnapshotFailure(new SnapshotMetadata("foo", 100, 1234L),
130 new Throwable("mock")));
132 verify(mockSnapshotManager).rollback();
136 public void testOnCommitSnapshot() {
138 sendMessageToSupport(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
140 verify(mockSnapshotManager).commit(eq(-1L), eq(-1L));
144 public void testUnhandledMessage() {
146 sendMessageToSupport("unhandled", false);