2 * Copyright (c) 2016 Cisco 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.mdsal.dom.store.inmemory;
10 import static org.junit.Assert.assertTrue;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DATA_TREE;
18 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.resetMocks;
20 import com.google.common.collect.ImmutableMap;
21 import com.google.common.collect.ImmutableSet;
22 import com.google.common.util.concurrent.MoreExecutors;
23 import java.util.concurrent.ExecutionException;
24 import org.junit.After;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
34 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
37 public class InMemoryDOMDataTreeShardThreePhaseCommitCohortTest {
39 private static final DataTreeCandidate DATA_TREE_CANDIDATE = mock(DataTreeCandidate.class);
40 private static final DataTreeCandidateNode DATA_TREE_CANDIDATE_NODE = mock(DataTreeCandidateNode.class);
41 private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
42 private static final InMemoryDOMDataTreeShardChangePublisher IN_MEMORY_DOM_DATA_TREE_SHARD_CHANGE_PUBLISHER =
43 new InMemoryDOMDataTreeShardChangePublisher(MoreExecutors.newDirectExecutorService(), 1, DATA_TREE,
44 YangInstanceIdentifier.of(QName.create("test")), ImmutableMap.of());
45 private static final InMemoryDOMDataTreeShardThreePhaseCommitCohort
46 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT =
47 new InMemoryDOMDataTreeShardThreePhaseCommitCohort(DATA_TREE, DATA_TREE_MODIFICATION,
48 IN_MEMORY_DOM_DATA_TREE_SHARD_CHANGE_PUBLISHER);
51 public void setUp() throws Exception {
52 doReturn(YangInstanceIdentifier.EMPTY).when(DATA_TREE_CANDIDATE).getRootPath();
53 doReturn("testDataTreeCandidate").when(DATA_TREE_CANDIDATE).toString();
54 doReturn(DATA_TREE_CANDIDATE_NODE).when(DATA_TREE_CANDIDATE).getRootNode();
55 doReturn(DATA_TREE_CANDIDATE).when(DATA_TREE).prepare(any());
57 doReturn(ModificationType.WRITE).when(DATA_TREE_CANDIDATE_NODE).getModificationType();
58 doReturn(ImmutableSet.of()).when(DATA_TREE_CANDIDATE_NODE).getChildNodes();
60 doNothing().when(DATA_TREE).validate(any());
61 doNothing().when(DATA_TREE).commit(any());
63 doReturn("testDataTreeModification").when(DATA_TREE_MODIFICATION).toString();
67 public void basicTest() throws Exception {
68 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.canCommit();
69 verify(DATA_TREE).validate(any());
70 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.preCommit();
71 verify(DATA_TREE).prepare(any());
72 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.commit();
73 verify(DATA_TREE).commit(any());
74 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.abort();
77 @Test(expected = IllegalStateException.class)
78 public void abortWithExceptionTest() throws Exception {
79 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.abort();
80 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.commit();
84 public void preCommitWithExceptionTest() throws Exception {
85 doThrow(new RuntimeException("testException")).when(DATA_TREE).prepare(any());
87 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.preCommit().get();
88 Assert.fail("Expected Exception");
89 } catch (ExecutionException e) {
90 assertTrue(e.getCause().getMessage().contains("testException"));
95 public void canCommitWithDataValidationFailedExceptionTest() throws Exception {
96 doThrow(new DataValidationFailedException(YangInstanceIdentifier.EMPTY, "testException"))
97 .when(DATA_TREE).validate(any());
99 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.canCommit().get();
100 } catch (ExecutionException e) {
101 assertTrue(e.getCause() instanceof TransactionCommitFailedException);
106 public void canCommitWithExceptionTest() throws Exception {
107 doThrow(new RuntimeException("testException")).when(DATA_TREE).validate(any());
109 IN_MEMORY_DOM_DATA_TREE_SHARD_THREE_PHASE_COMMIT_COHORT.canCommit().get();
110 Assert.fail("Expected Exception");
111 } catch (ExecutionException e) {
112 assertTrue(e.getCause().getMessage().contains("testException"));
117 public void reset() {