Move transaction-invariants into producer
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / test / java / org / opendaylight / mdsal / dom / store / inmemory / ShardDataModificationCursorTest.java
1 /*
2  * Copyright (c) 2016 Cisco 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.mdsal.dom.store.inmemory;
9
10 import static org.mockito.Mockito.doNothing;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.NORMALIZED_NODE;
16 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.PATH_ARGUMENT;
17 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.WRITEABLE_MODIFICATION_NODE;
18 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.WRITE_CURSOR_STRATEGY;
19 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.resetMocks;
20
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.ImmutableMap;
23 import java.lang.reflect.Field;
24 import java.util.Deque;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.junit.After;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
31
32 public class ShardDataModificationCursorTest {
33
34     @Test
35     public void basicTest() throws Exception {
36         final DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
37         final DataTreeModificationCursorAdaptor dataTreeModificationCursorAdaptor =
38                 DataTreeModificationCursorAdaptor.of(dataTreeModificationCursor);
39         final ShardRootModificationContext shardRootModificationContext = mock(ShardRootModificationContext.class);
40         final Map<PathArgument, WriteableModificationNode> children = new HashMap<>();
41         children.put(PATH_ARGUMENT, WRITEABLE_MODIFICATION_NODE);
42         final ShardDataModification root =  new ShardDataModification(shardRootModificationContext, children,
43             ImmutableMap.of());
44
45         doReturn(dataTreeModificationCursorAdaptor).when(shardRootModificationContext).cursor();
46         InmemoryDOMDataTreeShardWriteTransaction inmemoryDOMDataTreeShardWriteTransaction =
47                 mock(InmemoryDOMDataTreeShardWriteTransaction.class);
48         ShardDataModificationCursor shardDataModificationCursor =
49                 new ShardDataModificationCursor(root, inmemoryDOMDataTreeShardWriteTransaction);
50
51         final Field stackField = ShardDataModificationCursor.class.getDeclaredField("stack");
52         stackField.setAccessible(true);
53
54         final Deque<WriteCursorStrategy> stack =
55                 (Deque<WriteCursorStrategy>) stackField.get(shardDataModificationCursor);
56         stack.clear();
57         stack.push(WRITE_CURSOR_STRATEGY);
58         stackField.set(shardDataModificationCursor, stack);
59
60         doNothing().when(WRITE_CURSOR_STRATEGY).delete(PATH_ARGUMENT);
61         doNothing().when(WRITE_CURSOR_STRATEGY).merge(PATH_ARGUMENT, NORMALIZED_NODE);
62         doNothing().when(WRITE_CURSOR_STRATEGY).write(PATH_ARGUMENT, NORMALIZED_NODE);
63         doReturn("testPathArgument").when(PATH_ARGUMENT).toString();
64
65         shardDataModificationCursor.delete(PATH_ARGUMENT);
66         verify(WRITE_CURSOR_STRATEGY).delete(PATH_ARGUMENT);
67         shardDataModificationCursor.merge(PATH_ARGUMENT, NORMALIZED_NODE);
68         verify(WRITE_CURSOR_STRATEGY).merge(PATH_ARGUMENT, NORMALIZED_NODE);
69         shardDataModificationCursor.write(PATH_ARGUMENT, NORMALIZED_NODE);
70         verify(WRITE_CURSOR_STRATEGY).write(PATH_ARGUMENT, NORMALIZED_NODE);
71
72         doReturn(WRITE_CURSOR_STRATEGY).when(WRITE_CURSOR_STRATEGY).enter(PATH_ARGUMENT);
73         shardDataModificationCursor.enter(ImmutableList.of(PATH_ARGUMENT));
74         shardDataModificationCursor.enter(PATH_ARGUMENT, PATH_ARGUMENT);
75         verify(WRITE_CURSOR_STRATEGY, times(3)).enter(PATH_ARGUMENT);
76
77         doNothing().when(inmemoryDOMDataTreeShardWriteTransaction).cursorClosed();
78         shardDataModificationCursor.close();
79         verify(inmemoryDOMDataTreeShardWriteTransaction).cursorClosed();
80
81         doNothing().when(WRITE_CURSOR_STRATEGY).exit();
82         shardDataModificationCursor.exit(1);
83         verify(WRITE_CURSOR_STRATEGY).exit();
84     }
85
86     @After
87     public void reset() {
88         resetMocks();
89     }
90 }