Remove Augmentation{Identifier,Node}
[yangtools.git] / data / yang-data-api / src / test / java / org / opendaylight / yangtools / yang / data / api / schema / NormalizedNodesTest.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.api.schema;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doCallRealMethod;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import java.util.List;
18 import java.util.Optional;
19 import org.junit.jupiter.api.Test;
20 import org.junit.jupiter.api.extension.ExtendWith;
21 import org.mockito.junit.jupiter.MockitoExtension;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28
29 @ExtendWith(MockitoExtension.class)
30 public class NormalizedNodesTest {
31     @Test
32     public void testGetDirectChild() {
33         final var mockedPathArgument = new NodeIdentifier(QName.create("test", "test"));
34
35         final var mockedLeafNode = mock(LeafNode.class);
36         assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafNode, mockedPathArgument));
37
38         final var mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
39         assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafSetEntryNode, mockedPathArgument));
40
41         final var mockedDataContainerNode = mock(DataContainerNode.class);
42         final var mockedContainerNode = mock(ContainerNode.class);
43         doReturn(mockedContainerNode).when(mockedDataContainerNode).childByArg(any(PathArgument.class));
44         doCallRealMethod().when(mockedDataContainerNode).findChildByArg(any(PathArgument.class));
45
46         assertEquals(Optional.of(mockedContainerNode),
47             NormalizedNodes.getDirectChild(mockedDataContainerNode, mockedPathArgument));
48
49         final var mockedMapNode = mock(SystemMapNode.class);
50         final QName listQName = QName.create("test-ns", "test-list");
51         final QName listKeyQName = QName.create("test-ns", "test-list-key");
52         final var nodeIdentifierWithPredicates = NodeIdentifierWithPredicates.of(listQName, listKeyQName, "str-value");
53         final var mockedMapEntryNode = mock(MapEntryNode.class);
54         doReturn(mockedMapEntryNode).when(mockedMapNode).childByArg(any(NodeIdentifierWithPredicates.class));
55         doCallRealMethod().when(mockedMapNode).findChildByArg(any(NodeIdentifierWithPredicates.class));
56
57         assertEquals(Optional.of(mockedMapEntryNode),
58             NormalizedNodes.getDirectChild(mockedMapNode, nodeIdentifierWithPredicates));
59         assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedMapNode, mockedPathArgument));
60
61         final SystemLeafSetNode<?> mockedLeafSetNode = mock(SystemLeafSetNode.class);
62         final QName leafListQName = QName.create("test-ns", "test-leaf-list");
63         final NodeWithValue<?> nodeWithValue = new NodeWithValue<>(leafListQName, "str-value");
64         doReturn(mockedLeafSetEntryNode).when(mockedLeafSetNode).childByArg(any(NodeWithValue.class));
65         doCallRealMethod().when(mockedLeafSetNode).findChildByArg(any(NodeWithValue.class));
66         assertEquals(Optional.of(mockedLeafSetEntryNode),
67             NormalizedNodes.getDirectChild(mockedLeafSetNode, nodeWithValue));
68     }
69
70     @Test
71     public void testFindNode() {
72         final DataContainerNode mockedDataContainerNode = mock(DataContainerNode.class);
73         final ContainerNode mockedContainerNode = mock(ContainerNode.class);
74         final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
75         doReturn(mockedContainerNode).when(mockedDataContainerNode).childByArg(any(PathArgument.class));
76         doCallRealMethod().when(mockedDataContainerNode).findChildByArg(any(PathArgument.class));
77         doReturn(mockedLeafNode).when(mockedContainerNode).childByArg(any(PathArgument.class));
78         doCallRealMethod().when(mockedContainerNode).findChildByArg(any(PathArgument.class));
79
80         final QName node1QName = QName.create("test-ns", "2016-09-16", "node1");
81         final QName node2Qname = QName.create("test-ns", "2016-09-16", "node2");
82         final QName node3QName = QName.create("test-ns", "2016-09-16", "node3");
83         final QName node4Qname = QName.create("test-ns", "2016-09-16", "node4");
84
85         final YangInstanceIdentifier rootPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
86                 new NodeIdentifier(node2Qname));
87         final YangInstanceIdentifier childPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
88                 new NodeIdentifier(node2Qname), new NodeIdentifier(node3QName), new NodeIdentifier(node4Qname));
89
90         assertEquals(Optional.of(mockedLeafNode),
91             NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath));
92         assertEquals(Optional.empty(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));
93
94         final var pathArguments = childPath.relativeTo(rootPath).orElseThrow().getPathArguments()
95             .toArray(new PathArgument[0]);
96
97         assertEquals(Optional.of(mockedLeafNode),
98             NormalizedNodes.findNode(Optional.of(mockedDataContainerNode), pathArguments));
99
100         assertEquals(Optional.of(mockedLeafNode), NormalizedNodes.findNode(mockedDataContainerNode, pathArguments));
101     }
102
103     @Test
104     public void testToStringTree() {
105         final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
106         final QName leafNodeQName = QName.create("test-ns", "2016-09-16", "leaf-node");
107         final NodeIdentifier leafNodeId = new NodeIdentifier(leafNodeQName);
108         doReturn(leafNodeId).when(mockedLeafNode).getIdentifier();
109         doReturn("str-value-1").when(mockedLeafNode).body();
110
111         String stringTree = NormalizedNodes.toStringTree(mockedLeafNode);
112         assertNotNull(stringTree);
113         assertEquals("leaf-node str-value-1\n", stringTree);
114
115         final QName listQName = QName.create("test-ns", "2016-09-16", "list-node");
116
117         final SystemMapNode mockedMapNode = mock(SystemMapNode.class);
118         final NodeIdentifier listNodeId = new NodeIdentifier(listQName);
119         doReturn(listNodeId).when(mockedMapNode).getIdentifier();
120
121         final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
122         final NodeIdentifierWithPredicates listEntryNodeId = NodeIdentifierWithPredicates.of(listQName,
123                 leafNodeQName, "key-leaf-value");
124         doReturn(listEntryNodeId).when(mockedMapEntryNode).getIdentifier();
125         doReturn(List.of(mockedMapEntryNode)).when(mockedMapNode).body();
126
127         doReturn(List.of(mockedLeafNode)).when(mockedMapEntryNode).body();
128
129         stringTree = NormalizedNodes.toStringTree(mockedMapNode);
130         assertNotNull(stringTree);
131         assertEquals("""
132             list-node {
133                 list-node[key-leaf-value] {
134                     leaf-node str-value-1
135                 }
136             }
137             """, stringTree);
138     }
139 }