aabd3725c8b092929884832e1e22f7aa34b660ca
[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.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35
36 public class InMemoryDOMDataTreeShardTest {
37
38     @Test
39     public void basicTest() throws Exception {
40         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
41                 InMemoryDOMDataTreeShard.create(DOM_DATA_TREE_IDENTIFIER,
42                         MoreExecutors.newDirectExecutorService(), 1, 1);
43
44         final DOMDataTreeIdentifier domDataTreeIdentifier =
45                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
46                         YangInstanceIdentifier.of(QName.create("Test")));
47
48         final ReadableWriteableDOMDataTreeShard domDataTreeShard = mock(ReadableWriteableDOMDataTreeShard.class);
49         doReturn("testReadableWriteableDOMDataTreeShard").when(domDataTreeShard).toString();
50         doReturn(DOM_DATA_TREE_SHARD_PRODUCER).when(domDataTreeShard).createProducer(any());
51
52         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
53         inMemoryDOMDataTreeShard.onChildAttached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
54         assertTrue(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
55         inMemoryDOMDataTreeShard.onChildAttached(domDataTreeIdentifier, domDataTreeShard);
56
57         final Collection<DOMDataTreeIdentifier> prefixes = ImmutableList.of(DOM_DATA_TREE_IDENTIFIER);
58         assertEquals(prefixes.toString(), inMemoryDOMDataTreeShard.createProducer(prefixes).getPrefixes().toString());
59
60         inMemoryDOMDataTreeShard.onGlobalContextUpdated(createTestContext());
61         inMemoryDOMDataTreeShard.createTransaction(prefixes);
62
63         final DOMDataTreeChangeListener domDataTreeChangeListener = mock(DOMDataTreeChangeListener.class);
64         final ListenerRegistration listenerRegistration = mock(ListenerRegistration.class);
65         doReturn(listenerRegistration).when(domDataTreeShard).registerTreeChangeListener(any(), any());
66         doNothing().when(domDataTreeChangeListener).onDataTreeChanged(any());
67         inMemoryDOMDataTreeShard.registerTreeChangeListener(YangInstanceIdentifier.EMPTY, domDataTreeChangeListener);
68         verify(domDataTreeShard, atLeastOnce()).registerTreeChangeListener(any(), any());
69
70         inMemoryDOMDataTreeShard.onChildDetached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
71         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsKey(DOM_DATA_TREE_IDENTIFIER));
72     }
73
74     @Test(expected = UnsupportedOperationException.class)
75     public void createTransactionWithException() throws Exception {
76         final DOMDataTreeIdentifier domDataTreeIdentifier =
77                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
78
79         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
80                 InMemoryDOMDataTreeShard.create(domDataTreeIdentifier,
81                         MoreExecutors.newDirectExecutorService(), 1 ,1);
82
83         final InmemoryDOMDataTreeShardWriteTransaction inmemoryDOMDataTreeShardWriteTransaction =
84                 mock(InmemoryDOMDataTreeShardWriteTransaction.class);
85
86         inMemoryDOMDataTreeShard.createTransaction(inmemoryDOMDataTreeShardWriteTransaction);
87     }
88
89     @After
90     public void reset() {
91         resetMocks();
92     }
93 }