2321fd1edf86d258175c3583650ddfefb7fafcff
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / NormalizedNodeAggregatorTest.java
1 /*
2  * Copyright (c) 2015 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.controller.cluster.datastore.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import java.util.Collection;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.cluster.datastore.model.CarsModel;
21 import org.opendaylight.controller.md.cluster.datastore.model.SchemaContextHelper;
22 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
23 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
24 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
27 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35
36 public class NormalizedNodeAggregatorTest {
37
38     @Test
39     public void testAggregate() throws InterruptedException, ExecutionException, ReadFailedException, DataValidationFailedException {
40         SchemaContext schemaContext = SchemaContextHelper.full();
41         NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
42         NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);
43
44         Optional<NormalizedNode<?, ?>> optional = NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.builder().build(),
45                 ImmutableList.of(
46                         Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode1, schemaContext)),
47                         Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode2, schemaContext))),
48                 schemaContext);
49
50
51         NormalizedNode<?,?> normalizedNode = optional.get();
52
53         assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
54
55         Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
56
57         for(NormalizedNode<?,?> node : collection){
58             assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
59         }
60
61         assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
62                 findChildWithQName(collection, TestModel.TEST_QNAME) != null);
63
64         assertEquals(expectedNode1, findChildWithQName(collection, TestModel.TEST_QNAME));
65
66         assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
67                 findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
68
69         assertEquals(expectedNode2, findChildWithQName(collection, CarsModel.BASE_QNAME));
70
71     }
72
73     public static NormalizedNode<?, ?> getRootNode(NormalizedNode<?, ?> moduleNode, SchemaContext schemaContext)
74             throws ReadFailedException, ExecutionException, InterruptedException {
75         try (InMemoryDOMDataStore store = new InMemoryDOMDataStore("test", Executors.newSingleThreadExecutor())) {
76             store.onGlobalContextUpdated(schemaContext);
77
78             DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
79
80             writeTransaction.merge(YangInstanceIdentifier.builder().node(moduleNode.getNodeType()).build(), moduleNode);
81
82             DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
83
84             ready.canCommit().get();
85             ready.preCommit().get();
86             ready.commit().get();
87
88             DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
89
90             CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = readTransaction
91                     .read(YangInstanceIdentifier.builder().build());
92
93             Optional<NormalizedNode<?, ?>> nodeOptional = read.checkedGet();
94
95             return nodeOptional.get();
96         }
97     }
98
99     public static NormalizedNode<?,?> findChildWithQName(Collection<NormalizedNode<?, ?>> collection, QName qName) {
100         for(NormalizedNode<?,?> node : collection){
101             if(node.getNodeType().equals(qName)){
102                 return node;
103             }
104         }
105
106         return null;
107     }
108
109 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.