Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / ReplicatedLogImplTest.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.Matchers.same;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.reset;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.verifyNoMoreInteractions;
16
17 import akka.japi.Procedure;
18 import java.util.Collections;
19 import org.hamcrest.BaseMatcher;
20 import org.hamcrest.Description;
21 import org.hamcrest.Matcher;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Matchers;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.internal.matchers.Same;
30 import org.opendaylight.controller.cluster.DataPersistenceProvider;
31 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockPayload;
32 import org.opendaylight.controller.cluster.raft.MockRaftActorContext.MockReplicatedLogEntry;
33 import org.opendaylight.controller.cluster.raft.behaviors.RaftActorBehavior;
34 import org.opendaylight.controller.cluster.raft.persisted.DeleteEntries;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Unit tests for ReplicatedLogImpl.
40  *
41  * @author Thomas Pantelis
42  */
43 public class ReplicatedLogImplTest {
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     private RaftActorContext context;
53     private final DefaultConfigParamsImpl configParams = new DefaultConfigParamsImpl();
54
55     @Before
56     public void setup() {
57         MockitoAnnotations.initMocks(this);
58
59         context = new RaftActorContextImpl(null, null, "test",
60                 new ElectionTermImpl(mockPersistence, "test", LOG),
61                 -1, -1, Collections.<String,String>emptyMap(), configParams, mockPersistence, LOG);
62     }
63
64     private void verifyPersist(Object message) throws Exception {
65         verifyPersist(message, new Same(message));
66     }
67
68     @SuppressWarnings({ "unchecked", "rawtypes" })
69     private void verifyPersist(Object message, Matcher<?> matcher) throws Exception {
70         ArgumentCaptor<Procedure> procedure = ArgumentCaptor.forClass(Procedure.class);
71         verify(mockPersistence).persist(Matchers.argThat(matcher), procedure.capture());
72
73         procedure.getValue().apply(message);
74     }
75
76     @SuppressWarnings("unchecked")
77     @Test
78     public void testAppendAndPersistExpectingNoCapture() throws Exception {
79         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
80
81         MockReplicatedLogEntry logEntry1 = new MockReplicatedLogEntry(1, 1, new MockPayload("1"));
82
83         log.appendAndPersist(logEntry1);
84
85         verifyPersist(logEntry1);
86
87         assertEquals("size", 1, log.size());
88
89         reset(mockPersistence);
90
91         MockReplicatedLogEntry logEntry2 = new MockReplicatedLogEntry(1, 2, new MockPayload("2"));
92         Procedure<ReplicatedLogEntry> mockCallback = Mockito.mock(Procedure.class);
93         log.appendAndPersist(logEntry2, mockCallback);
94
95         verifyPersist(logEntry2);
96
97         verify(mockCallback).apply(same(logEntry2));
98
99         assertEquals("size", 2, log.size());
100     }
101
102     @SuppressWarnings("unchecked")
103     @Test
104     public void testAppendAndPersisWithDuplicateEntry() throws Exception {
105         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
106
107         Procedure<ReplicatedLogEntry> mockCallback = Mockito.mock(Procedure.class);
108         MockReplicatedLogEntry logEntry = new MockReplicatedLogEntry(1, 1, new MockPayload("1"));
109
110         log.appendAndPersist(logEntry, mockCallback);
111
112         verifyPersist(logEntry);
113
114         assertEquals("size", 1, log.size());
115
116         reset(mockPersistence, mockCallback);
117
118         log.appendAndPersist(logEntry, mockCallback);
119
120         verifyNoMoreInteractions(mockPersistence, mockCallback);
121
122         assertEquals("size", 1, log.size());
123     }
124
125     @Test
126     public void testAppendAndPersistExpectingCaptureDueToJournalCount() throws Exception {
127         configParams.setSnapshotBatchCount(2);
128
129         doReturn(1L).when(mockBehavior).getReplicatedToAllIndex();
130
131         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
132
133         final MockReplicatedLogEntry logEntry1 = new MockReplicatedLogEntry(1, 2, new MockPayload("2"));
134         final MockReplicatedLogEntry logEntry2 = new MockReplicatedLogEntry(1, 3, new MockPayload("3"));
135
136         log.appendAndPersist(logEntry1);
137         verifyPersist(logEntry1);
138
139         reset(mockPersistence);
140
141         log.appendAndPersist(logEntry2);
142         verifyPersist(logEntry2);
143
144
145         assertEquals("size", 2, log.size());
146     }
147
148     @Test
149     public void testAppendAndPersistExpectingCaptureDueToDataSize() throws Exception {
150         doReturn(1L).when(mockBehavior).getReplicatedToAllIndex();
151
152         context.setTotalMemoryRetriever(() -> 100);
153
154         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
155
156         int dataSize = 600;
157         MockReplicatedLogEntry logEntry = new MockReplicatedLogEntry(1, 2, new MockPayload("2", dataSize));
158
159         log.appendAndPersist(logEntry);
160         verifyPersist(logEntry);
161
162         reset(mockPersistence);
163
164         logEntry = new MockReplicatedLogEntry(1, 3, new MockPayload("3", 5));
165
166         log.appendAndPersist(logEntry);
167         verifyPersist(logEntry);
168
169         assertEquals("size", 2, log.size());
170     }
171
172     @Test
173     public void testRemoveFromAndPersist() throws Exception {
174
175         ReplicatedLog log = ReplicatedLogImpl.newInstance(context);
176
177         log.append(new MockReplicatedLogEntry(1, 0, new MockPayload("0")));
178         log.append(new MockReplicatedLogEntry(1, 1, new MockPayload("1")));
179         log.append(new MockReplicatedLogEntry(1, 2, new MockPayload("2")));
180
181         log.removeFromAndPersist(1);
182
183         DeleteEntries deleteEntries = new DeleteEntries(1);
184         verifyPersist(deleteEntries, match(deleteEntries));
185
186         assertEquals("size", 1, log.size());
187
188         reset(mockPersistence);
189
190         log.removeFromAndPersist(1);
191
192         verifyNoMoreInteractions(mockPersistence);
193     }
194
195     public Matcher<DeleteEntries> match(final DeleteEntries actual) {
196         return new BaseMatcher<DeleteEntries>() {
197             @Override
198             public boolean matches(Object obj) {
199                 DeleteEntries other = (DeleteEntries) obj;
200                 return actual.getFromIndex() == other.getFromIndex();
201             }
202
203             @Override
204             public void describeTo(Description description) {
205                 description.appendText("DeleteEntries: fromIndex: " + actual.getFromIndex());
206             }
207         };
208     }
209 }

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