BUG-5280: switch transactionIdentifier
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DebugThreePhaseCommitCohortTest.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.assertSame;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Matchers.same;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.never;
17 import static org.mockito.Mockito.reset;
18 import static org.mockito.Mockito.verify;
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import java.util.ArrayList;
22 import java.util.List;
23 import org.junit.Test;
24 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
25 import org.slf4j.Logger;
26 import scala.concurrent.Future;
27
28 /**
29  * Unit tests for DebugThreePhaseCommitCohort.
30  *
31  * @author Thomas Pantelis
32  */
33 public class DebugThreePhaseCommitCohortTest {
34     private final TransactionIdentifier transactionId = MockIdentifiers.transactionIdentifier(
35         DebugThreePhaseCommitCohortTest.class, "mock");
36
37     @Test
38     public void test() {
39         AbstractThreePhaseCommitCohort<?> mockDelegate = mock(AbstractThreePhaseCommitCohort.class);
40         Exception failure = new Exception("mock failure");
41         ListenableFuture<Object> expFailedFuture = Futures.immediateFailedFuture(failure);
42         doReturn(expFailedFuture).when(mockDelegate).canCommit();
43         doReturn(expFailedFuture).when(mockDelegate).preCommit();
44         doReturn(expFailedFuture).when(mockDelegate).commit();
45
46         ListenableFuture<Object> expAbortFuture = Futures.immediateFuture(null);
47         doReturn(expAbortFuture).when(mockDelegate).abort();
48
49         List<Future<Object>> expCohortFutures = new ArrayList<>();
50         doReturn(expCohortFutures).when(mockDelegate).getCohortFutures();
51
52         Throwable debugContext = new RuntimeException("mock");
53         DebugThreePhaseCommitCohort cohort = new DebugThreePhaseCommitCohort(transactionId , mockDelegate , debugContext);
54
55         Logger mockLogger = mock(Logger.class);
56         cohort.setLogger(mockLogger);
57
58         assertSame("canCommit", expFailedFuture, cohort.canCommit());
59         verify(mockLogger).warn(anyString(), same(transactionId), same(failure), same(debugContext));
60
61         reset(mockLogger);
62         assertSame("preCommit", expFailedFuture, cohort.preCommit());
63         verify(mockLogger).warn(anyString(), same(transactionId), same(failure), same(debugContext));
64
65         reset(mockLogger);
66         assertSame("commit", expFailedFuture, cohort.commit());
67         verify(mockLogger).warn(anyString(), same(transactionId), same(failure), same(debugContext));
68
69         assertSame("abort", expAbortFuture, cohort.abort());
70
71         assertSame("getCohortFutures", expCohortFutures, cohort.getCohortFutures());
72
73         reset(mockLogger);
74         ListenableFuture<Boolean> expSuccessFuture = Futures.immediateFuture(Boolean.TRUE);
75         doReturn(expSuccessFuture).when(mockDelegate).canCommit();
76
77         assertSame("canCommit", expSuccessFuture, cohort.canCommit());
78         verify(mockLogger, never()).warn(anyString(), any(TransactionIdentifier.class), any(Throwable.class),
79                 any(Throwable.class));
80     }
81 }