4671cfc44f88606ee5a312f10007bc1e300b4d23
[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.Matchers.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.controller.md.sal.dom.store.impl.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.mdsal.dom.spi.shard.ReadableWriteableDOMDataTreeShard;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeModification;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.CursorAwareDataTreeSnapshot;
38
39 public class InMemoryDOMDataTreeShardTest {
40
41     @Test
42     public void basicTest() {
43         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
44                 InMemoryDOMDataTreeShard.create(DOM_DATA_TREE_IDENTIFIER,
45                         MoreExecutors.directExecutor(), 1);
46
47         final DOMDataTreeIdentifier domDataTreeIdentifier =
48                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
49                         YangInstanceIdentifier.of(QName.create("", "Test")));
50
51         final ReadableWriteableDOMDataTreeShard domDataTreeShard = mock(ReadableWriteableDOMDataTreeShard.class);
52         doReturn("testReadableWriteableDOMDataTreeShard").when(domDataTreeShard).toString();
53         doReturn(DOM_DATA_TREE_SHARD_PRODUCER).when(domDataTreeShard).createProducer(any());
54
55         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
56         inMemoryDOMDataTreeShard.onChildAttached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
57         assertTrue(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
58         inMemoryDOMDataTreeShard.onChildAttached(domDataTreeIdentifier, domDataTreeShard);
59
60         final Collection<DOMDataTreeIdentifier> prefixes = ImmutableList.of(DOM_DATA_TREE_IDENTIFIER);
61         assertEquals(prefixes.toString(), inMemoryDOMDataTreeShard.createProducer(prefixes).getPrefixes().toString());
62
63         final InMemoryDOMDataTreeShardProducer mockProducer = mock(InMemoryDOMDataTreeShardProducer.class);
64         doReturn(prefixes).when(mockProducer).getPrefixes();
65         doReturn(inMemoryDOMDataTreeShard.createModificationFactory(prefixes))
66             .when(mockProducer).getModificationFactory();
67
68         inMemoryDOMDataTreeShard.onGlobalContextUpdated(createTestContext());
69         inMemoryDOMDataTreeShard.createTransaction("", mockProducer, mock(CursorAwareDataTreeSnapshot.class));
70
71         final DOMDataTreeChangeListener domDataTreeChangeListener = mock(DOMDataTreeChangeListener.class);
72         final ListenerRegistration<?> listenerRegistration = mock(ListenerRegistration.class);
73         doReturn(listenerRegistration).when(domDataTreeShard).registerTreeChangeListener(any(), any());
74         doNothing().when(domDataTreeChangeListener).onDataTreeChanged(any());
75         inMemoryDOMDataTreeShard.registerTreeChangeListener(YangInstanceIdentifier.EMPTY, domDataTreeChangeListener);
76         verify(domDataTreeShard, atLeastOnce()).registerTreeChangeListener(any(), any());
77
78         inMemoryDOMDataTreeShard.onChildDetached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
79         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsKey(DOM_DATA_TREE_IDENTIFIER));
80     }
81
82     @Test
83     public void createTransactionWithException() {
84         final DOMDataTreeIdentifier domDataTreeIdentifier =
85                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
86
87         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
88                 InMemoryDOMDataTreeShard.create(domDataTreeIdentifier,
89                         MoreExecutors.newDirectExecutorService(), 1);
90         final CursorAwareDataTreeModification dataTreeModification = mock(CursorAwareDataTreeModification.class);
91
92         final InmemoryDOMDataTreeShardWriteTransaction inmemoryDOMDataTreeShardWriteTransaction =
93                 mock(InmemoryDOMDataTreeShardWriteTransaction.class);
94         doReturn(dataTreeModification).when(inmemoryDOMDataTreeShardWriteTransaction).getRootModification();
95         final Collection<DOMDataTreeIdentifier> prefixes = ImmutableList.of(DOM_DATA_TREE_IDENTIFIER);
96         final InMemoryDOMDataTreeShardProducer mockProducer = mock(InMemoryDOMDataTreeShardProducer.class);
97         doReturn(prefixes).when(mockProducer).getPrefixes();
98         doReturn(inMemoryDOMDataTreeShard.createModificationFactory(prefixes))
99             .when(mockProducer).getModificationFactory();
100
101         inMemoryDOMDataTreeShard.createTransaction("", mockProducer, mock(CursorAwareDataTreeSnapshot.class));
102     }
103
104     @After
105     public void reset() {
106         resetMocks();
107     }
108 }