2 * Copyright (c) 2015 Brocade Communications Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.cluster.datastore;
10 import static org.junit.Assert.assertSame;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyString;
13 import static org.mockito.ArgumentMatchers.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;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.junit.Test;
25 import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier;
26 import org.slf4j.Logger;
27 import scala.concurrent.Future;
30 * Unit tests for DebugThreePhaseCommitCohort.
32 * @author Thomas Pantelis
34 public class DebugThreePhaseCommitCohortTest {
35 private final TransactionIdentifier transactionId = MockIdentifiers.transactionIdentifier(
36 DebugThreePhaseCommitCohortTest.class, "mock");
40 AbstractThreePhaseCommitCohort<?> mockDelegate = mock(AbstractThreePhaseCommitCohort.class);
41 Exception failure = new Exception("mock failure");
42 ListenableFuture<Object> expFailedFuture = Futures.immediateFailedFuture(failure);
43 doReturn(expFailedFuture).when(mockDelegate).canCommit();
44 doReturn(expFailedFuture).when(mockDelegate).preCommit();
45 doReturn(expFailedFuture).when(mockDelegate).commit();
47 ListenableFuture<Object> expAbortFuture = Futures.immediateFuture(null);
48 doReturn(expAbortFuture).when(mockDelegate).abort();
50 List<Future<Object>> expCohortFutures = new ArrayList<>();
51 doReturn(expCohortFutures).when(mockDelegate).getCohortFutures();
53 Throwable debugContext = new RuntimeException("mock");
54 DebugThreePhaseCommitCohort cohort = new DebugThreePhaseCommitCohort(transactionId, mockDelegate, debugContext);
56 Logger mockLogger = mock(Logger.class);
57 cohort.setLogger(mockLogger);
59 assertSame("canCommit", expFailedFuture, cohort.canCommit());
60 verify(mockLogger).warn(anyString(), same(transactionId), same(failure), same(debugContext));
63 assertSame("preCommit", expFailedFuture, cohort.preCommit());
64 verify(mockLogger).warn(anyString(), same(transactionId), same(failure), same(debugContext));
67 assertSame("commit", expFailedFuture, cohort.commit());
68 verify(mockLogger).warn(anyString(), same(transactionId), same(failure), same(debugContext));
70 assertSame("abort", expAbortFuture, cohort.abort());
72 assertSame("getCohortFutures", expCohortFutures, cohort.getCohortFutures());
75 ListenableFuture<Boolean> expSuccessFuture = Futures.immediateFuture(Boolean.TRUE);
76 doReturn(expSuccessFuture).when(mockDelegate).canCommit();
78 assertSame("canCommit", expSuccessFuture, cohort.canCommit());
79 verify(mockLogger, never()).warn(anyString(), any(TransactionIdentifier.class), any(Throwable.class),
80 any(Throwable.class));