2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
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;
37 public class NormalizedNodeAggregatorTest {
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);
46 Optional<NormalizedNode<?, ?>> optional = NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.EMPTY,
48 Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode1, schemaContext)),
49 Optional.<NormalizedNode<?, ?>>of(getRootNode(expectedNode2, schemaContext))),
50 schemaContext, LogicalDatastoreType.CONFIGURATION);
53 NormalizedNode<?,?> normalizedNode = optional.get();
55 assertTrue("Expect value to be a Collection", normalizedNode.getValue() instanceof Collection);
57 @SuppressWarnings("unchecked")
58 Collection<NormalizedNode<?,?>> collection = (Collection<NormalizedNode<?,?>>) normalizedNode.getValue();
60 for (NormalizedNode<?,?> node : collection) {
61 assertTrue("Expected " + node + " to be a ContainerNode", node instanceof ContainerNode);
64 assertTrue("Child with QName = " + TestModel.TEST_QNAME + " not found",
65 findChildWithQName(collection, TestModel.TEST_QNAME) != null);
67 assertEquals(expectedNode1, findChildWithQName(collection, TestModel.TEST_QNAME));
69 assertTrue("Child with QName = " + CarsModel.BASE_QNAME + " not found",
70 findChildWithQName(collection, CarsModel.BASE_QNAME) != null);
72 assertEquals(expectedNode2, findChildWithQName(collection, CarsModel.BASE_QNAME));
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);
81 DOMStoreWriteTransaction writeTransaction = store.newWriteOnlyTransaction();
83 writeTransaction.merge(YangInstanceIdentifier.of(moduleNode.getNodeType()), moduleNode);
85 DOMStoreThreePhaseCommitCohort ready = writeTransaction.ready();
87 ready.canCommit().get();
88 ready.preCommit().get();
91 DOMStoreReadTransaction readTransaction = store.newReadOnlyTransaction();
93 FluentFuture<Optional<NormalizedNode<?, ?>>> read = readTransaction.read(YangInstanceIdentifier.EMPTY);
95 Optional<NormalizedNode<?, ?>> nodeOptional = read.get();
97 return nodeOptional.get();
101 public static NormalizedNode<?,?> findChildWithQName(Collection<NormalizedNode<?, ?>> collection, QName qname) {
102 for (NormalizedNode<?, ?> node : collection) {
103 if (node.getNodeType().equals(qname)) {