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