c45ac2effc402d07e38627bc5070df0e7f759c3e
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / test / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDOMDataTreeShardTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.atLeastOnce;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.mdsal.dom.store.inmemory.TestModel.createTestContext;
20 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DOM_DATA_TREE_IDENTIFIER;
21 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DOM_DATA_TREE_SHARD_PRODUCER;
22 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.resetMocks;
23
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.util.concurrent.MoreExecutors;
26 import java.util.Collection;
27 import org.junit.After;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
37
38 public class InMemoryDOMDataTreeShardTest {
39
40     @Test
41     public void basicTest() {
42         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
43                 InMemoryDOMDataTreeShard.create(DOM_DATA_TREE_IDENTIFIER,
44                         MoreExecutors.directExecutor(), 1);
45
46         final DOMDataTreeIdentifier domDataTreeIdentifier =
47                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
48                         YangInstanceIdentifier.of(QName.create("", "Test")));
49
50         final InMemoryDOMDataTreeShard domDataTreeShard = mock(InMemoryDOMDataTreeShard.class);
51         doReturn("testReadableWriteableDOMDataTreeShard").when(domDataTreeShard).toString();
52         doReturn(DOM_DATA_TREE_SHARD_PRODUCER).when(domDataTreeShard).createProducer(any());
53         doReturn(domDataTreeShard).when(DOM_DATA_TREE_SHARD_PRODUCER).getParentShard();
54         doNothing().when(DOM_DATA_TREE_SHARD_PRODUCER).close();
55
56         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
57         inMemoryDOMDataTreeShard.onChildAttached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
58         assertTrue(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
59         inMemoryDOMDataTreeShard.onChildAttached(domDataTreeIdentifier, domDataTreeShard);
60
61         final Collection<DOMDataTreeIdentifier> prefixes = ImmutableList.of(DOM_DATA_TREE_IDENTIFIER);
62         assertEquals(prefixes.toString(), inMemoryDOMDataTreeShard.createProducer(prefixes).getPrefixes().toString());
63
64         final InMemoryDOMDataTreeShardProducer mockProducer = mock(InMemoryDOMDataTreeShardProducer.class);
65         doReturn(prefixes).when(mockProducer).getPrefixes();
66         doReturn(inMemoryDOMDataTreeShard.createModificationFactory(prefixes))
67                 .when(mockProducer).getModificationFactory();
68
69         inMemoryDOMDataTreeShard.onModelContextUpdated(createTestContext());
70         inMemoryDOMDataTreeShard.createTransaction("", mockProducer, mock(CursorAwareDataTreeSnapshot.class));
71
72         final DOMDataTreeChangeListener domDataTreeChangeListener = mock(DOMDataTreeChangeListener.class);
73         final ListenerRegistration<?> listenerRegistration = mock(ListenerRegistration.class);
74         doReturn(listenerRegistration).when(domDataTreeShard).registerTreeChangeListener(any(), any());
75         doNothing().when(domDataTreeChangeListener).onDataTreeChanged(any());
76         inMemoryDOMDataTreeShard.registerTreeChangeListener(YangInstanceIdentifier.empty(), domDataTreeChangeListener);
77         verify(domDataTreeShard, atLeastOnce()).registerTreeChangeListener(any(), any());
78
79         inMemoryDOMDataTreeShard.onChildDetached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
80         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsKey(DOM_DATA_TREE_IDENTIFIER));
81     }
82
83     @Test
84     public void createTransactionWithException() {
85         final DOMDataTreeIdentifier domDataTreeIdentifier =
86                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
87
88         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
89                 InMemoryDOMDataTreeShard.create(domDataTreeIdentifier,
90                         MoreExecutors.newDirectExecutorService(), 1);
91         final CursorAwareDataTreeModification dataTreeModification = mock(CursorAwareDataTreeModification.class);
92
93         final InmemoryDOMDataTreeShardWriteTransaction inmemoryDOMDataTreeShardWriteTransaction =
94                 mock(InmemoryDOMDataTreeShardWriteTransaction.class);
95         doReturn(dataTreeModification).when(inmemoryDOMDataTreeShardWriteTransaction).getRootModification();
96         final Collection<DOMDataTreeIdentifier> prefixes = ImmutableList.of(DOM_DATA_TREE_IDENTIFIER);
97         final InMemoryDOMDataTreeShardProducer mockProducer = mock(InMemoryDOMDataTreeShardProducer.class);
98         doReturn(prefixes).when(mockProducer).getPrefixes();
99         doReturn(inMemoryDOMDataTreeShard.createModificationFactory(prefixes))
100             .when(mockProducer).getModificationFactory();
101
102         inMemoryDOMDataTreeShard.createTransaction("", mockProducer, mock(CursorAwareDataTreeSnapshot.class));
103     }
104
105     @After
106     public void reset() {
107         resetMocks();
108     }
109 }