Fix warnings in sal-akka-raft test classes
[controller.git] / opendaylight / md-sal / sal-akka-raft / src / test / java / org / opendaylight / controller / cluster / raft / RaftActorDelegatingPersistentDataProviderTest.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.mockito.Matchers.eq;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.never;
13 import static org.mockito.Mockito.verify;
14
15 import akka.japi.Procedure;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.cluster.DataPersistenceProvider;
22 import org.opendaylight.controller.cluster.PersistentDataProvider;
23 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.Payload;
24 import org.opendaylight.controller.cluster.raft.protobuff.client.messages.PersistentPayload;
25
26 /**
27  * Unit tests for RaftActorDelegatingPersistentDataProvider.
28  *
29  * @author Thomas Pantelis
30  */
31 public class RaftActorDelegatingPersistentDataProviderTest {
32     private static final Payload PERSISTENT_PAYLOAD = new TestPersistentPayload();
33
34     private static final Payload NON_PERSISTENT_PAYLOAD = new TestNonPersistentPayload();
35
36     private static final Object OTHER_DATA_OBJECT = new Object();
37
38     @Mock
39     private ReplicatedLogEntry mockPersistentLogEntry;
40
41     @Mock
42     private ReplicatedLogEntry mockNonPersistentLogEntry;
43
44     @Mock
45     private DataPersistenceProvider mockDelegateProvider;
46
47     @Mock
48     private PersistentDataProvider mockPersistentProvider;
49
50     @SuppressWarnings("rawtypes")
51     @Mock
52     private Procedure mockProcedure;
53
54     private RaftActorDelegatingPersistentDataProvider provider;
55
56     @Before
57     public void setup() {
58         MockitoAnnotations.initMocks(this);
59         doReturn(PERSISTENT_PAYLOAD).when(mockPersistentLogEntry).getData();
60         doReturn(NON_PERSISTENT_PAYLOAD).when(mockNonPersistentLogEntry).getData();
61         provider = new RaftActorDelegatingPersistentDataProvider(mockDelegateProvider, mockPersistentProvider);
62     }
63
64     @SuppressWarnings("unchecked")
65     @Test
66     public void testPersistWithPersistenceEnabled() {
67         doReturn(true).when(mockDelegateProvider).isRecoveryApplicable();
68
69         provider.persist(mockPersistentLogEntry, mockProcedure);
70         verify(mockDelegateProvider).persist(mockPersistentLogEntry, mockProcedure);
71
72         provider.persist(mockNonPersistentLogEntry, mockProcedure);
73         verify(mockDelegateProvider).persist(mockNonPersistentLogEntry, mockProcedure);
74
75         provider.persist(OTHER_DATA_OBJECT, mockProcedure);
76         verify(mockDelegateProvider).persist(OTHER_DATA_OBJECT, mockProcedure);
77     }
78
79     @SuppressWarnings({ "unchecked", "rawtypes" })
80     @Test
81     public void testPersistWithPersistenceDisabled() throws Exception {
82         doReturn(false).when(mockDelegateProvider).isRecoveryApplicable();
83
84         provider.persist(mockPersistentLogEntry, mockProcedure);
85
86         ArgumentCaptor<Procedure> procedureCaptor = ArgumentCaptor.forClass(Procedure.class);
87         verify(mockPersistentProvider).persist(eq(PERSISTENT_PAYLOAD), procedureCaptor.capture());
88         verify(mockDelegateProvider, never()).persist(mockNonPersistentLogEntry, mockProcedure);
89         procedureCaptor.getValue().apply(PERSISTENT_PAYLOAD);
90         verify(mockProcedure).apply(mockPersistentLogEntry);
91
92         provider.persist(mockNonPersistentLogEntry, mockProcedure);
93         verify(mockDelegateProvider).persist(mockNonPersistentLogEntry, mockProcedure);
94
95         provider.persist(OTHER_DATA_OBJECT, mockProcedure);
96         verify(mockDelegateProvider).persist(OTHER_DATA_OBJECT, mockProcedure);
97     }
98
99     static class TestNonPersistentPayload extends Payload {
100         @Override
101         public int size() {
102             return 0;
103         }
104     }
105
106     static class TestPersistentPayload extends TestNonPersistentPayload implements PersistentPayload {
107     }
108 }

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