Revert "Unify ORv1 and IIv5"
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / NormalizedNodeUtilsTest.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 package org.opendaylight.yangtools.yang.data.impl.schema;
9
10 import static org.junit.jupiter.api.Assertions.assertNotNull;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import org.junit.jupiter.api.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
20 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
21
22 /*
23 * Schema structure of document is:
24 *
25 * container root { 
26 *      list list-a {
27 *              key leaf-a;
28 *              leaf leaf-a;
29 *              choice choice-a {
30 *                      case one {
31 *                              leaf one;
32 *                      }
33 *                      case two-three {
34 *                              leaf two;
35 *                              leaf three;
36 *                      }
37 *              }
38 *              list list-b {
39 *                      key leaf-b;
40 *                      leaf leaf-b;
41 *              }
42 *      }
43 * }
44 */
45 class NormalizedNodeUtilsTest {
46     private static final QName ROOT_QNAME = QName.create("urn:opendaylight:controller:sal:dom:store:test", "2014-03-13",
47             "root");
48     private static final QName LIST_A_QNAME = QName.create(ROOT_QNAME, "list-a");
49     private static final QName LIST_B_QNAME = QName.create(ROOT_QNAME, "list-b");
50     private static final QName LEAF_A_QNAME = QName.create(ROOT_QNAME, "leaf-a");
51     private static final QName LEAF_B_QNAME = QName.create(ROOT_QNAME, "leaf-b");
52     private static final String FOO = "foo";
53     private static final String BAR = "bar";
54     private static final String ONE = "one";
55     private static final String TWO = "two";
56
57     private static final YangInstanceIdentifier LIST_A_FOO_PATH = YangInstanceIdentifier.builder()
58         .node(LIST_A_QNAME)
59         .nodeWithKey(LIST_A_QNAME, LEAF_A_QNAME, FOO)
60         .build();
61     private static final YangInstanceIdentifier LIST_B_TWO_PATH = YangInstanceIdentifier.builder()
62         .node(LIST_A_QNAME)
63         .nodeWithKey(LIST_A_QNAME, LEAF_A_QNAME, BAR)
64         .node(LIST_B_QNAME)
65         .nodeWithKey(LIST_B_QNAME,LEAF_B_QNAME,TWO)
66         .build();
67
68     /**
69      * Returns a test document.
70      *
71      * <pre>
72      * root
73      *     list-a
74      *          leaf-a "foo"
75      *     list-a
76      *          leaf-a "bar"
77      *          list-b
78      *                  leaf-b "one"
79      *          list-b
80      *                  leaf-b "two"
81      *
82      * </pre>
83      *
84      * @return A test document
85      */
86     private static NormalizedNode createDocumentOne() {
87         return ImmutableNodes.newContainerBuilder()
88             .withNodeIdentifier(new NodeIdentifier(ROOT_QNAME))
89             .withChild(ImmutableNodes.newSystemMapBuilder()
90                 .withNodeIdentifier(new NodeIdentifier(LIST_A_QNAME))
91                 .withChild(ImmutableNodes.newMapEntryBuilder()
92                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_A_QNAME, LEAF_A_QNAME, FOO))
93                     .withChild(ImmutableNodes.leafNode(LEAF_A_QNAME, FOO))
94                     .build())
95                 .withChild(ImmutableNodes.newMapEntryBuilder()
96                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_A_QNAME, LEAF_A_QNAME, BAR))
97                     .withChild(ImmutableNodes.leafNode(LEAF_A_QNAME, BAR))
98                     .withChild(ImmutableNodes.newSystemMapBuilder()
99                         .withNodeIdentifier(new NodeIdentifier(LIST_B_QNAME))
100                         .withChild(ImmutableNodes.newMapEntryBuilder()
101                             .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_B_QNAME, LEAF_B_QNAME, ONE))
102                             .withChild(ImmutableNodes.leafNode(LEAF_B_QNAME, ONE))
103                             .build())
104                         .withChild(ImmutableNodes.newMapEntryBuilder()
105                             .withNodeIdentifier(NodeIdentifierWithPredicates.of(LIST_B_QNAME, LEAF_B_QNAME, TWO))
106                             .withChild(ImmutableNodes.leafNode(LEAF_B_QNAME, TWO))
107                             .build())
108                         .build())
109                     .build())
110                 .build())
111             .build();
112     }
113
114     @Test
115     void findNodeTest() {
116         final var tree = createDocumentOne();
117         assertNotNull(tree);
118
119         final var listFooResult = NormalizedNodes.findNode(tree, LIST_A_FOO_PATH);
120         assertTrue(listFooResult.isPresent());
121
122         final var listTwoResult = NormalizedNodes.findNode(tree, LIST_B_TWO_PATH);
123         assertTrue(listTwoResult.isPresent());
124     }
125 }