e25c708766f8e5fc818500141333c7d97a1a8ed9
[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.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18 import static org.opendaylight.controller.md.sal.dom.store.impl.TestModel.createTestContext;
19 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DOM_DATA_TREE_IDENTIFIER;
20 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.DOM_DATA_TREE_SHARD_PRODUCER;
21 import static org.opendaylight.mdsal.dom.store.inmemory.TestUtils.resetMocks;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.util.Collection;
26 import org.junit.After;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34
35 public class InMemoryDOMDataTreeShardTest {
36
37     @Test
38     public void basicTest() throws Exception {
39         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
40                 InMemoryDOMDataTreeShard.create(DOM_DATA_TREE_IDENTIFIER,
41                         MoreExecutors.newDirectExecutorService(), 1, 1);
42
43         final DOMDataTreeIdentifier domDataTreeIdentifier =
44                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION,
45                         YangInstanceIdentifier.of(QName.create("Test")));
46
47         final ReadableWriteableDOMDataTreeShard domDataTreeShard = mock(ReadableWriteableDOMDataTreeShard.class);
48         doReturn("testReadableWriteableDOMDataTreeShard").when(domDataTreeShard).toString();
49         doReturn(DOM_DATA_TREE_SHARD_PRODUCER).when(domDataTreeShard).createProducer(any());
50
51         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
52         inMemoryDOMDataTreeShard.onChildAttached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
53         assertTrue(inMemoryDOMDataTreeShard.getChildShards().containsValue(domDataTreeShard));
54         inMemoryDOMDataTreeShard.onChildAttached(domDataTreeIdentifier, domDataTreeShard);
55
56         final Collection<DOMDataTreeIdentifier> prefixes = ImmutableList.of(DOM_DATA_TREE_IDENTIFIER);
57         assertEquals(prefixes.toString(), inMemoryDOMDataTreeShard.createProducer(prefixes).getPrefixes().toString());
58
59         inMemoryDOMDataTreeShard.onGlobalContextUpdated(createTestContext());
60         inMemoryDOMDataTreeShard.createTransaction(prefixes);
61
62         final DOMDataTreeChangeListener domDataTreeChangeListener = mock(DOMDataTreeChangeListener.class);
63         final ListenerRegistration listenerRegistration = mock(ListenerRegistration.class);
64         doReturn(listenerRegistration).when(domDataTreeShard).registerTreeChangeListener(any(), any());
65         inMemoryDOMDataTreeShard.registerTreeChangeListener(YangInstanceIdentifier.EMPTY, domDataTreeChangeListener);
66         verify(domDataTreeShard, atLeastOnce()).registerTreeChangeListener(any(), any());
67
68         inMemoryDOMDataTreeShard.onChildDetached(DOM_DATA_TREE_IDENTIFIER, domDataTreeShard);
69         assertFalse(inMemoryDOMDataTreeShard.getChildShards().containsKey(DOM_DATA_TREE_IDENTIFIER));
70     }
71
72     @Test(expected = UnsupportedOperationException.class)
73     public void createTransactionWithException() throws Exception {
74         final DOMDataTreeIdentifier domDataTreeIdentifier =
75                 new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY);
76
77         final InMemoryDOMDataTreeShard inMemoryDOMDataTreeShard =
78                 InMemoryDOMDataTreeShard.create(domDataTreeIdentifier,
79                         MoreExecutors.newDirectExecutorService(), 1 ,1);
80
81         final InmemoryDOMDataTreeShardWriteTransaction inmemoryDOMDataTreeShardWriteTransaction =
82                 mock(InmemoryDOMDataTreeShardWriteTransaction.class);
83
84         inMemoryDOMDataTreeShard.createTransaction(inmemoryDOMDataTreeShardWriteTransaction);
85     }
86
87     @After
88     public void reset() {
89         resetMocks();
90     }
91 }