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