Removed usage of deprecated YangParserImpl from tests in mdsal project
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / test / ShardedDOMDataTreeProducerMultiShardTest.java
1 package org.opendaylight.mdsal.dom.broker.test;
2
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Mockito.doNothing;
5 import static org.mockito.Mockito.doReturn;
6 import static org.mockito.Mockito.verify;
7
8 import com.google.common.util.concurrent.Futures;
9 import java.util.Collection;
10 import java.util.Collections;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.mockito.Mockito;
14 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
15 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
16 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteCursor;
17 import org.opendaylight.mdsal.dom.broker.ShardedDOMDataTree;
18 import org.opendaylight.mdsal.dom.broker.test.util.TestModel;
19 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardProducer;
20 import org.opendaylight.mdsal.dom.store.inmemory.DOMDataTreeShardWriteTransaction;
21 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataTreeShard;
22 import org.opendaylight.mdsal.dom.store.inmemory.WriteableDOMDataTreeShard;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35
36 public class ShardedDOMDataTreeProducerMultiShardTest {
37
38     private SchemaContext schemaContext;
39
40     private static final DOMDataTreeIdentifier ROOT_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
41             YangInstanceIdentifier.EMPTY);
42     private static final DOMDataTreeIdentifier TEST_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
43             TestModel.TEST_PATH);
44
45     private static final DOMDataTreeIdentifier TEST2_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
46             TestModel.TEST2_PATH);
47
48     private static final DOMDataTreeIdentifier INNER_CONTAINER_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
49     private static final DOMDataTreeIdentifier ANOTHER_SHARD_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.ANOTHER_SHARD_PATH);
50
51     private InMemoryDOMDataTreeShard rootShard;
52
53     private InMemoryDOMDataTreeShard anotherInnerShard;
54
55
56     private ShardedDOMDataTree dataTreeService;
57     private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
58     private ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg;
59
60     @Before
61     public void setUp() throws Exception {
62         schemaContext = TestModel.createTestContext();
63
64         rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID);
65         rootShard.onGlobalContextUpdated(schemaContext);
66
67         ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
68         rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard);
69
70         dataTreeService = dataTree;
71     }
72
73     @Test
74     public void testMultipleShards() throws Exception {
75         //FIXME after listeners are implemented add them here and test those
76
77         final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID);
78         innerShard.onGlobalContextUpdated(schemaContext);
79         innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard);
80
81         final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
82         final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
83         final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
84         cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
85
86         final LeafNode<String> shardedValue1 =
87                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
88         final LeafNode<String> shardedValue2 =
89                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
90
91         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
92         final ContainerNode containerNode =
93                 containerNodeBuilder
94                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
95                         .withChild(shardedValue1)
96                         .withChild(shardedValue2)
97                         .build();
98
99         cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
100         cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
101
102         final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
103                 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
104                 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
105                 .build();
106
107         cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
108         cursor.close();
109         transaction.ready();
110         transaction.submit().get();
111
112         //verify listeners have been notified
113     }
114
115     @Test
116     public void testMockedSubshards() throws Exception {
117         final WriteableDOMDataTreeShard mockedInnerShard = Mockito.mock(WriteableDOMDataTreeShard.class);
118         dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, mockedInnerShard);
119         final DOMDataTreeShardProducer mockedProducer = Mockito.mock(DOMDataTreeShardProducer.class);
120         doReturn(mockedProducer).when(mockedInnerShard).createProducer(any(Collection.class));
121
122         final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
123
124         final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
125         final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
126         cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
127
128         final LeafNode<String> shardedValue1 =
129                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
130         final LeafNode<String> shardedValue2 =
131                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
132
133         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
134         final ContainerNode containerNode =
135                 containerNodeBuilder
136                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
137                         .withChild(shardedValue1)
138                         .withChild(shardedValue2)
139                         .build();
140
141         final DOMDataTreeShardWriteTransaction mockedTx = Mockito.mock(DOMDataTreeShardWriteTransaction.class);
142         doReturn(mockedTx).when(mockedProducer).createTransaction();
143
144         doNothing().when(mockedTx).ready();
145         doReturn(Futures.immediateFuture(true)).when(mockedTx).validate();
146         doReturn(Futures.immediateFuture(null)).when(mockedTx).prepare();
147         doReturn(Futures.immediateFuture(null)).when(mockedTx).commit();
148
149         final DOMDataTreeWriteCursor mockedCursor = Mockito.mock(DOMDataTreeWriteCursor.class);
150         doNothing().when(mockedCursor).write(any(PathArgument.class), any(NormalizedNode.class));
151         doNothing().when(mockedCursor).close();
152         doReturn(mockedCursor).when(mockedTx).createCursor(any(DOMDataTreeIdentifier.class));
153
154         cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
155         cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
156
157         final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
158                 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
159                 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
160                 .build();
161
162         cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
163         cursor.close();
164         transaction.ready();
165         transaction.submit().get();
166
167         verify(mockedTx).ready();
168         verify(mockedTx).validate();
169         verify(mockedTx).prepare();
170         verify(mockedTx).commit();
171
172     }
173 }