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