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