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

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