380ff29d2abb4a558cd7cd6346b8f4b200e911e7
[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 java.lang.reflect.Field;
23 import java.util.Deque;
24 import java.util.HashMap;
25 import java.util.Map;
26 import org.junit.After;
27 import org.junit.Test;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
30
31 public class ShardDataModificationCursorTest {
32
33     @Test
34     public void basicTest() throws Exception {
35         final DataTreeModificationCursor dataTreeModificationCursor = mock(DataTreeModificationCursor.class);
36         final DataTreeModificationCursorAdaptor dataTreeModificationCursorAdaptor =
37                 DataTreeModificationCursorAdaptor.of(dataTreeModificationCursor);
38         final ShardRootModificationContext shardRootModificationContext = mock(ShardRootModificationContext.class);
39         final Map<PathArgument, WriteableModificationNode> children = new HashMap<>();
40         children.put(PATH_ARGUMENT, WRITEABLE_MODIFICATION_NODE);
41         final ShardDataModification root =
42                 new ShardDataModificationBuilder(shardRootModificationContext).build(children);
43         doReturn(dataTreeModificationCursorAdaptor).when(shardRootModificationContext).cursor();
44         InmemoryDOMDataTreeShardWriteTransaction inmemoryDOMDataTreeShardWriteTransaction =
45                 mock(InmemoryDOMDataTreeShardWriteTransaction.class);
46         ShardDataModificationCursor shardDataModificationCursor =
47                 new ShardDataModificationCursor(root, inmemoryDOMDataTreeShardWriteTransaction);
48
49         final Field stackField = ShardDataModificationCursor.class.getDeclaredField("stack");
50         stackField.setAccessible(true);
51
52         final Deque<WriteCursorStrategy> stack =
53                 (Deque<WriteCursorStrategy>) stackField.get(shardDataModificationCursor);
54         stack.clear();
55         stack.push(WRITE_CURSOR_STRATEGY);
56         stackField.set(shardDataModificationCursor, stack);
57
58         doNothing().when(WRITE_CURSOR_STRATEGY).delete(PATH_ARGUMENT);
59         doNothing().when(WRITE_CURSOR_STRATEGY).merge(PATH_ARGUMENT, NORMALIZED_NODE);
60         doNothing().when(WRITE_CURSOR_STRATEGY).write(PATH_ARGUMENT, NORMALIZED_NODE);
61         doReturn("testPathArgument").when(PATH_ARGUMENT).toString();
62
63         shardDataModificationCursor.delete(PATH_ARGUMENT);
64         verify(WRITE_CURSOR_STRATEGY).delete(PATH_ARGUMENT);
65         shardDataModificationCursor.merge(PATH_ARGUMENT, NORMALIZED_NODE);
66         verify(WRITE_CURSOR_STRATEGY).merge(PATH_ARGUMENT, NORMALIZED_NODE);
67         shardDataModificationCursor.write(PATH_ARGUMENT, NORMALIZED_NODE);
68         verify(WRITE_CURSOR_STRATEGY).write(PATH_ARGUMENT, NORMALIZED_NODE);
69
70         doReturn(WRITE_CURSOR_STRATEGY).when(WRITE_CURSOR_STRATEGY).enter(PATH_ARGUMENT);
71         shardDataModificationCursor.enter(ImmutableList.of(PATH_ARGUMENT));
72         shardDataModificationCursor.enter(PATH_ARGUMENT, PATH_ARGUMENT);
73         verify(WRITE_CURSOR_STRATEGY, times(3)).enter(PATH_ARGUMENT);
74
75         doNothing().when(inmemoryDOMDataTreeShardWriteTransaction).cursorClosed();
76         shardDataModificationCursor.close();
77         verify(inmemoryDOMDataTreeShardWriteTransaction).cursorClosed();
78
79         doNothing().when(WRITE_CURSOR_STRATEGY).exit();
80         shardDataModificationCursor.exit(1);
81         verify(WRITE_CURSOR_STRATEGY).exit();
82     }
83
84     @After
85     public void reset() {
86         resetMocks();
87     }
88 }