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.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;
35 * Unit tests for SimpleShardDataTreeCohort.
37 * @author Thomas Pantelis
39 public class SimpleShardDataTreeCohortTest {
41 private TipProducingDataTree mockDataTree;
44 private ShardDataTree mockShardDataTree;
47 private DataTreeModification mockModification;
49 private SimpleShardDataTreeCohort cohort;
53 MockitoAnnotations.initMocks(this);
55 doReturn(mockDataTree).when(mockShardDataTree).getDataTree();
57 cohort = new SimpleShardDataTreeCohort(mockShardDataTree, mockModification, "tx");
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);
68 @Test(expected=OptimisticLockFailedException.class)
69 public void testCanCommitWithConflictingModEx() throws Throwable {
70 doThrow(new ConflictingModificationAppliedException(YangInstanceIdentifier.EMPTY, "mock")).
71 when(mockDataTree).validate(mockModification);
73 cohort.canCommit().get();
74 } catch (ExecutionException e) {
79 @Test(expected=TransactionCommitFailedException.class)
80 public void testCanCommitWithDataValidationEx() throws Throwable {
81 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "mock")).
82 when(mockDataTree).validate(mockModification);
84 cohort.canCommit().get();
85 } catch (ExecutionException e) {
90 @Test(expected=IllegalArgumentException.class)
91 public void testCanCommitWithIllegalArgumentEx() throws Throwable {
92 doThrow(new IllegalArgumentException("mock")).when(mockDataTree).validate(mockModification);
94 cohort.canCommit().get();
95 } catch (ExecutionException e) {
101 public void testPreCommitAndCommitSuccess() throws Exception {
102 DataTreeCandidateTip mockCandidate = mock(DataTreeCandidateTip.class);
103 doReturn(mockCandidate ).when(mockDataTree).prepare(mockModification);
105 ListenableFuture<Void> future = cohort.preCommit();
106 assertNotNull("Future is null", future);
108 verify(mockDataTree).prepare(mockModification);
110 assertSame("getCandidate", mockCandidate, cohort.getCandidate());
112 future = cohort.commit();
113 assertNotNull("Future is null", future);
115 verify(mockDataTree).commit(mockCandidate);
118 @Test(expected=IllegalArgumentException.class)
119 public void testPreCommitWithIllegalArgumentEx() throws Throwable {
120 doThrow(new IllegalArgumentException("mock")).when(mockDataTree).prepare(mockModification);
122 cohort.preCommit().get();
123 } catch (ExecutionException e) {
128 @Test(expected=IllegalArgumentException.class)
129 public void testCommitWithIllegalArgumentEx() throws Throwable {
130 doThrow(new IllegalArgumentException("mock")).when(mockDataTree).commit(any(DataTreeCandidateTip.class));
132 cohort.commit().get();
133 } catch (ExecutionException e) {
139 public void testAbort() throws Exception {
140 cohort.abort().get();