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

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