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.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
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.io.OutputStream;
22 import java.util.Collections;
23 import java.util.Optional;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.cluster.DataPersistenceProvider;
29 import org.opendaylight.controller.cluster.raft.base.messages.ApplySnapshot;
30 import org.opendaylight.controller.cluster.raft.base.messages.CaptureSnapshotReply;
31 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
32 import org.opendaylight.controller.cluster.raft.persisted.ByteState;
33 import org.opendaylight.controller.cluster.raft.persisted.Snapshot;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Unit tests for RaftActorSnapshotMessageSupport.
40 * @author Thomas Pantelis
42 public class RaftActorSnapshotMessageSupportTest {
44 private static final Logger LOG = LoggerFactory.getLogger(RaftActorRecoverySupportTest.class);
47 private DataPersistenceProvider mockPersistence;
50 private RaftActorBehavior mockBehavior;
53 private RaftActorSnapshotCohort mockCohort;
56 private SnapshotManager mockSnapshotManager;
59 ActorRef mockRaftActorRef;
61 private RaftActorSnapshotMessageSupport support;
63 private RaftActorContext context;
64 private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
68 MockitoAnnotations.initMocks(this);
70 context = new RaftActorContextImpl(mockRaftActorRef, null, "test",
71 new ElectionTermImpl(mockPersistence, "test", LOG), -1, -1, Collections.<String,String>emptyMap(),
72 configParams, mockPersistence, applyState -> { }, LOG) {
74 public SnapshotManager getSnapshotManager() {
75 return mockSnapshotManager;
79 support = new RaftActorSnapshotMessageSupport(context, mockCohort);
81 doReturn(true).when(mockPersistence).isRecoveryApplicable();
83 context.setReplicatedLog(ReplicatedLogImpl.newInstance(context));
86 private void sendMessageToSupport(Object message) {
87 sendMessageToSupport(message, true);
90 private void sendMessageToSupport(Object message, boolean expHandled) {
91 boolean handled = support.handleSnapshotMessage(message, mockRaftActorRef);
92 assertEquals("complete", expHandled, handled);
96 public void testOnApplySnapshot() {
98 long lastAppliedDuringSnapshotCapture = 1;
99 long lastIndexDuringSnapshotCapture = 2;
100 byte[] snapshotBytes = {1,2,3,4,5};
102 Snapshot snapshot = Snapshot.create(ByteState.of(snapshotBytes), Collections.<ReplicatedLogEntry>emptyList(),
103 lastIndexDuringSnapshotCapture, 1, lastAppliedDuringSnapshotCapture, 1, -1, null, null);
105 ApplySnapshot applySnapshot = new ApplySnapshot(snapshot);
106 sendMessageToSupport(applySnapshot);
108 verify(mockSnapshotManager).apply(applySnapshot);
112 public void testOnCaptureSnapshotReply() {
113 ByteState state = ByteState.of(new byte[]{1,2,3,4,5});
114 Optional<OutputStream> optionalStream = Optional.of(mock(OutputStream.class));
115 sendMessageToSupport(new CaptureSnapshotReply(state, optionalStream));
117 verify(mockSnapshotManager).persist(eq(state), eq(optionalStream), anyLong());
121 public void testOnSaveSnapshotSuccess() {
123 long sequenceNumber = 100;
124 long timeStamp = 1234L;
125 sendMessageToSupport(new SaveSnapshotSuccess(new SnapshotMetadata("foo", sequenceNumber, timeStamp)));
127 verify(mockSnapshotManager).commit(eq(sequenceNumber), eq(timeStamp));
131 public void testOnSaveSnapshotFailure() {
133 sendMessageToSupport(new SaveSnapshotFailure(new SnapshotMetadata("foo", 100, 1234L),
134 new Throwable("mock")));
136 verify(mockSnapshotManager).rollback();
140 public void testOnCommitSnapshot() {
142 sendMessageToSupport(RaftActorSnapshotMessageSupport.COMMIT_SNAPSHOT);
144 verify(mockSnapshotManager).commit(eq(-1L), eq(-1L));
148 public void testUnhandledMessage() {
150 sendMessageToSupport("unhandled", false);