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