02f97585e5fb1253bf2991d3d42436386fc68b24
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / ShardedDOMReadTransactionAdapterTest.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.broker;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import com.google.common.collect.ClassToInstanceMap;
14 import com.google.common.collect.ImmutableClassToInstanceMap;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Optional;
21 import java.util.concurrent.ExecutionException;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeServiceExtension;
32 import org.opendaylight.mdsal.dom.broker.util.TestModel;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
36
37 public class ShardedDOMReadTransactionAdapterTest {
38
39     private ShardedDOMReadTransactionAdapter readTx;
40
41     @Before
42     public void setUp() {
43         readTx = new ShardedDOMReadTransactionAdapter("TEST-TX", new TestTreeService());
44     }
45
46     @Test
47     public void testGetIdentifier() {
48         assertEquals("TEST-TX", readTx.getIdentifier());
49     }
50
51     @Test
52     public void testRead() throws InterruptedException, ExecutionException {
53         final ListenableFuture<Optional<NormalizedNode<?, ?>>> readResult =
54                 readTx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
55         assertTrue(readTx.exists(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH).get());
56         assertEquals(readResult.get().get(), TestUtils.TEST_CONTAINER);
57     }
58
59     @After
60     public void close() {
61         readTx.close();
62     }
63
64     private static class TestTreeService implements DOMDataTreeService {
65         @Override
66         public ClassToInstanceMap<DOMDataTreeServiceExtension> getExtensions() {
67             return ImmutableClassToInstanceMap.of();
68         }
69
70         @Override
71         public <T extends DOMDataTreeListener> ListenerRegistration<T>
72             registerListener(final T listener, final Collection<DOMDataTreeIdentifier> subtrees,
73                     final boolean allowRxMerges, final Collection<DOMDataTreeProducer> producers)
74                             throws DOMDataTreeLoopException {
75             final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtree = new HashMap<>();
76             subtree.put(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH),
77                     TestUtils.TEST_CONTAINER);
78
79             listener.onDataTreeChanged(Collections.singleton(
80                     DataTreeCandidates.fromNormalizedNode(TestModel.TEST_PATH, TestUtils.TEST_CONTAINER)), subtree);
81
82             return new ListenerRegistration<T>() {
83                 @Override
84                 public void close() {
85                     // NOOP
86                 }
87
88                 @Override
89                 public T getInstance() {
90                     return listener;
91                 }
92             };
93         }
94
95         @Override
96         public DOMDataTreeProducer createProducer(final Collection<DOMDataTreeIdentifier> subtrees) {
97             throw new UnsupportedOperationException();
98         }
99     }
100 }