bd18ac0f84830ac0c8e10c3c1b226075c0195be9
[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
35 public class ShardedDOMDataTreeProducerMultiShardTest {
36
37     private final SchemaContext schemaContext = TestModel.createTestContext();
38
39     private static final DOMDataTreeIdentifier ROOT_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
40             YangInstanceIdentifier.EMPTY);
41     private static final DOMDataTreeIdentifier TEST_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
42             TestModel.TEST_PATH);
43
44     private static final DOMDataTreeIdentifier TEST2_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
45             TestModel.TEST2_PATH);
46
47     private static final DOMDataTreeIdentifier INNER_CONTAINER_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.INNER_CONTAINER_PATH);
48     private static final DOMDataTreeIdentifier ANOTHER_SHARD_ID = new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, TestModel.ANOTHER_SHARD_PATH);
49
50     private InMemoryDOMDataTreeShard rootShard;
51
52     private InMemoryDOMDataTreeShard anotherInnerShard;
53
54
55     private ShardedDOMDataTree dataTreeService;
56     private ListenerRegistration<InMemoryDOMDataTreeShard> rootShardReg;
57     private ListenerRegistration<InMemoryDOMDataTreeShard> innerShardReg;
58
59     @Before
60     public void setUp() throws Exception {
61
62         rootShard = InMemoryDOMDataTreeShard.create(ROOT_ID);
63         rootShard.onGlobalContextUpdated(schemaContext);
64
65         ShardedDOMDataTree dataTree = new ShardedDOMDataTree();
66         rootShardReg = dataTree.registerDataTreeShard(ROOT_ID, rootShard);
67
68         dataTreeService = dataTree;
69     }
70
71     @Test
72     public void testMultipleShards() throws Exception {
73         //FIXME after listeners are implemented add them here and test those
74
75         final InMemoryDOMDataTreeShard innerShard = InMemoryDOMDataTreeShard.create(INNER_CONTAINER_ID);
76         innerShard.onGlobalContextUpdated(schemaContext);
77         innerShardReg = dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, innerShard);
78
79         final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
80         final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
81         final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
82         cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
83
84         final LeafNode<String> shardedValue1 =
85                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
86         final LeafNode<String> shardedValue2 =
87                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
88
89         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
90         final ContainerNode containerNode =
91                 containerNodeBuilder
92                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
93                         .withChild(shardedValue1)
94                         .withChild(shardedValue2)
95                         .build();
96
97         cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
98         cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
99
100         final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
101                 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
102                 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
103                 .build();
104
105         cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
106         cursor.close();
107         transaction.ready();
108         transaction.submit().get();
109
110         //verify listeners have been notified
111     }
112
113     @Test
114     public void testMockedSubshards() throws Exception {
115         final WriteableDOMDataTreeShard mockedInnerShard = Mockito.mock(WriteableDOMDataTreeShard.class);
116         dataTreeService.registerDataTreeShard(INNER_CONTAINER_ID, mockedInnerShard);
117         final DOMDataTreeShardProducer mockedProducer = Mockito.mock(DOMDataTreeShardProducer.class);
118         doReturn(mockedProducer).when(mockedInnerShard).createProducer(any(Collection.class));
119
120         final DOMDataTreeShardProducer producer = rootShard.createProducer(Collections.singletonList(TEST_ID));
121
122         final DOMDataTreeShardWriteTransaction transaction = producer.createTransaction();
123         final DOMDataTreeWriteCursor cursor = transaction.createCursor(ROOT_ID);
124         cursor.enter(TestModel.TEST_PATH.getLastPathArgument());
125
126         final LeafNode<String> shardedValue1 =
127                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_1)).withValue("sharded value 1").build();
128         final LeafNode<String> shardedValue2 =
129                 ImmutableLeafNodeBuilder.<String>create().withNodeIdentifier(new NodeIdentifier(TestModel.SHARDED_VALUE_2)).withValue("sharded value 2").build();
130
131         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> containerNodeBuilder = ImmutableContainerNodeBuilder.create();
132         final ContainerNode containerNode =
133                 containerNodeBuilder
134                         .withNodeIdentifier(new NodeIdentifier(TestModel.INNER_CONTAINER))
135                         .withChild(shardedValue1)
136                         .withChild(shardedValue2)
137                         .build();
138
139         final DOMDataTreeShardWriteTransaction mockedTx = Mockito.mock(DOMDataTreeShardWriteTransaction.class);
140         doReturn(mockedTx).when(mockedProducer).createTransaction();
141
142         doNothing().when(mockedTx).ready();
143         doReturn(Futures.immediateFuture(true)).when(mockedTx).validate();
144         doReturn(Futures.immediateFuture(null)).when(mockedTx).prepare();
145         doReturn(Futures.immediateFuture(null)).when(mockedTx).commit();
146
147         final DOMDataTreeWriteCursor mockedCursor = Mockito.mock(DOMDataTreeWriteCursor.class);
148         doNothing().when(mockedCursor).write(any(PathArgument.class), any(NormalizedNode.class));
149         doNothing().when(mockedCursor).close();
150         doReturn(mockedCursor).when(mockedTx).createCursor(any(DOMDataTreeIdentifier.class));
151
152         cursor.write(TestModel.INNER_CONTAINER_PATH.getLastPathArgument(), containerNode);
153         cursor.enter(TestModel.INNER_CONTAINER_PATH.getLastPathArgument());
154
155         final ContainerNode lowerShardContainer = ImmutableContainerNodeBuilder.create()
156                 .withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_CONTAINER))
157                 .withChild(ImmutableLeafNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(TestModel.ANOTHER_SHARD_VALUE)).build())
158                 .build();
159
160         cursor.write(TestModel.ANOTHER_SHARD_PATH.getLastPathArgument(), lowerShardContainer);
161         cursor.close();
162         transaction.ready();
163         transaction.submit().get();
164
165         verify(mockedTx).ready();
166         verify(mockedTx).validate();
167         verify(mockedTx).prepare();
168         verify(mockedTx).commit();
169
170     }
171 }