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

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