Bug 4992: Removed old leader's candidates on leader change
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / SimpleShardDataTreeCohortTest.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.datastore;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertSame;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.doThrow;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.ConflictingModificationAppliedException;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateTip;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
33
34 /**
35  * Unit tests for SimpleShardDataTreeCohort.
36  *
37  * @author Thomas Pantelis
38  */
39 public class SimpleShardDataTreeCohortTest {
40     @Mock
41     private TipProducingDataTree mockDataTree;
42
43     @Mock
44     private ShardDataTree mockShardDataTree;
45
46     @Mock
47     private DataTreeModification mockModification;
48
49     private SimpleShardDataTreeCohort cohort;
50
51     @Before
52     public void setup() {
53         MockitoAnnotations.initMocks(this);
54
55         doReturn(mockDataTree).when(mockShardDataTree).getDataTree();
56
57         cohort = new SimpleShardDataTreeCohort(mockShardDataTree, mockModification, "tx");
58     }
59
60     @Test
61     public void testCanCommitSuccess() throws Exception {
62         ListenableFuture<Boolean> future = cohort.canCommit();
63         assertNotNull("Future is null", future);
64         assertEquals("Future", true, future.get(3, TimeUnit.SECONDS));
65         verify(mockDataTree).validate(mockModification);
66     }
67
68     @Test(expected=OptimisticLockFailedException.class)
69     public void testCanCommitWithConflictingModEx() throws Throwable {
70         doThrow(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "mock")).
71                 when(mockDataTree).validate(mockModification);
72         try {
73             cohort.canCommit().get();
74         } catch (ExecutionException e) {
75             throw e.getCause();
76         }
77     }
78
79     @Test(expected=TransactionCommitFailedException.class)
80     public void testCanCommitWithDataValidationEx() throws Throwable {
81         doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock")).
82                 when(mockDataTree).validate(mockModification);
83         try {
84             cohort.canCommit().get();
85         } catch (ExecutionException e) {
86             throw e.getCause();
87         }
88     }
89
90     @Test(expected=IllegalArgumentException.class)
91     public void testCanCommitWithIllegalArgumentEx() throws Throwable {
92         doThrow(new IllegalArgumentException("mock")).when(mockDataTree).validate(mockModification);
93         try {
94             cohort.canCommit().get();
95         } catch (ExecutionException e) {
96             throw e.getCause();
97         }
98     }
99
100     @Test
101     public void testPreCommitAndCommitSuccess() throws Exception {
102         DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class);
103         doReturn(mockCandidate ).when(mockDataTree).prepare(mockModification);
104
105         ListenableFuture<Void> future = cohort.preCommit();
106         assertNotNull("Future is null", future);
107         future.get();
108         verify(mockDataTree).prepare(mockModification);
109
110         assertSame("getCandidate", mockCandidate, cohort.getCandidate());
111
112         future = cohort.commit();
113         assertNotNull("Future is null", future);
114         future.get();
115         verify(mockDataTree).commit(mockCandidate);
116     }
117
118     @Test(expected=IllegalArgumentException.class)
119     public void testPreCommitWithIllegalArgumentEx() throws Throwable {
120         doThrow(new IllegalArgumentException("mock")).when(mockDataTree).prepare(mockModification);
121         try {
122             cohort.preCommit().get();
123         } catch (ExecutionException e) {
124             throw e.getCause();
125         }
126     }
127
128     @Test(expected=IllegalArgumentException.class)
129     public void testCommitWithIllegalArgumentEx() throws Throwable {
130         doThrow(new IllegalArgumentException("mock")).when(mockDataTree).commit(any(DataTreeCandidateTip.class));
131         try {
132             cohort.commit().get();
133         } catch (ExecutionException e) {
134             throw e.getCause();
135         }
136     }
137
138     @Test
139     public void testAbort() throws Exception {
140         cohort.abort().get();
141     }
142 }