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