Bug 5699 - Migrate existing code to use new sharding apis
[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
9 package org.opendaylight.mdsal.dom.broker;
10
11 import static org.junit.Assert.assertEquals;
12
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Maps;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Map;
19 import javax.annotation.Nonnull;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.common.api.ReadFailedException;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeListener;
26 import org.opendaylight.mdsal.dom.api.DOMDataTreeLoopException;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeService;
29 import org.opendaylight.mdsal.dom.broker.util.TestModel;
30 import org.opendaylight.yangtools.concepts.ListenerRegistration;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
33
34 public class ShardedDOMReadTransactionAdapterTest {
35
36     private ShardedDOMReadTransactionAdapter readTx;
37
38     @Before
39     public void setUp() {
40         readTx = new ShardedDOMReadTransactionAdapter("TEST-TX", new TestTreeService());
41     }
42
43     @Test
44     public void testGetIdentifier() {
45         assertEquals("TEST-TX", readTx.getIdentifier());
46     }
47
48     @Test
49     public void testRead() throws Exception {
50         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> readResult =
51                 readTx.read(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
52         assertEquals(readResult.checkedGet().get(), TestUtils.TEST_CONTAINER);
53     }
54
55
56     private static class TestTreeService implements DOMDataTreeService {
57
58         @Nonnull
59         @Override
60         public <T extends DOMDataTreeListener> ListenerRegistration<T>
61         registerListener(@Nonnull final T listener, @Nonnull final Collection<DOMDataTreeIdentifier> subtrees,
62                          final boolean allowRxMerges,
63                          @Nonnull final Collection<DOMDataTreeProducer> producers) throws DOMDataTreeLoopException {
64             final Map<DOMDataTreeIdentifier, NormalizedNode<?, ?>> subtree = Maps.newHashMap();
65             subtree.put(new DOMDataTreeIdentifier(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH),
66                     TestUtils.TEST_CONTAINER);
67
68             listener.onDataTreeChanged(Collections.singleton(
69                     DataTreeCandidates.fromNormalizedNode(TestModel.TEST_PATH, TestUtils.TEST_CONTAINER)), subtree);
70
71             return new ListenerRegistration<T>() {
72                 @Override
73                 public void close() {
74                     // NOOP
75                 }
76
77                 @Override
78                 public T getInstance() {
79                     return listener;
80                 }
81             };
82         }
83
84         @Nonnull
85         @Override
86         public DOMDataTreeProducer createProducer(@Nonnull final Collection<DOMDataTreeIdentifier> subtrees) {
87             return null;
88         }
89     }
90 }