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

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