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