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

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