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