2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api.schema;
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.doReturn;
14 import static org.mockito.Mockito.mock;
16 import java.util.List;
17 import java.util.Optional;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20 import org.mockito.junit.jupiter.MockitoExtension;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
28 @ExtendWith(MockitoExtension.class)
29 public class NormalizedNodesTest {
31 public void testGetDirectChild() {
32 final var mockedPathArgument = new NodeIdentifier(QName.create("test", "test"));
34 final var mockedLeafNode = mock(LeafNode.class);
35 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafNode, mockedPathArgument));
37 final var mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
38 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafSetEntryNode, mockedPathArgument));
40 final var mockedDataContainerNode = mock(DataContainerNode.class);
41 final var mockedContainerNode = mock(ContainerNode.class);
42 doReturn(mockedContainerNode).when(mockedDataContainerNode).childByArg(any());
44 assertEquals(Optional.of(mockedContainerNode),
45 NormalizedNodes.getDirectChild(mockedDataContainerNode, mockedPathArgument));
47 final var mockedMapNode = mock(SystemMapNode.class);
48 final QName listQName = QName.create("test-ns", "test-list");
49 final QName listKeyQName = QName.create("test-ns", "test-list-key");
50 final var nodeIdentifierWithPredicates = NodeIdentifierWithPredicates.of(listQName, listKeyQName, "str-value");
51 final var mockedMapEntryNode = mock(MapEntryNode.class);
52 doReturn(mockedMapEntryNode).when(mockedMapNode).childByArg(any(NodeIdentifierWithPredicates.class));
54 assertEquals(Optional.of(mockedMapEntryNode),
55 NormalizedNodes.getDirectChild(mockedMapNode, nodeIdentifierWithPredicates));
56 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedMapNode, mockedPathArgument));
58 final SystemLeafSetNode<?> mockedLeafSetNode = mock(SystemLeafSetNode.class);
59 final QName leafListQName = QName.create("test-ns", "test-leaf-list");
60 final NodeWithValue<?> nodeWithValue = new NodeWithValue<>(leafListQName, "str-value");
61 doReturn(mockedLeafSetEntryNode).when(mockedLeafSetNode).childByArg(any(NodeWithValue.class));
62 assertEquals(Optional.of(mockedLeafSetEntryNode),
63 NormalizedNodes.getDirectChild(mockedLeafSetNode, nodeWithValue));
67 public void testFindNode() {
68 final DataContainerNode mockedDataContainerNode = mock(DataContainerNode.class);
69 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
70 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
71 doReturn(mockedContainerNode).when(mockedDataContainerNode).childByArg(any());
72 doReturn(mockedLeafNode).when(mockedContainerNode).childByArg(any());
74 final QName node1QName = QName.create("test-ns", "2016-09-16", "node1");
75 final QName node2Qname = QName.create("test-ns", "2016-09-16", "node2");
76 final QName node3QName = QName.create("test-ns", "2016-09-16", "node3");
77 final QName node4Qname = QName.create("test-ns", "2016-09-16", "node4");
79 final YangInstanceIdentifier rootPath = YangInstanceIdentifier.of(new NodeIdentifier(node1QName),
80 new NodeIdentifier(node2Qname));
81 final YangInstanceIdentifier childPath = YangInstanceIdentifier.of(new NodeIdentifier(node1QName),
82 new NodeIdentifier(node2Qname), new NodeIdentifier(node3QName), new NodeIdentifier(node4Qname));
84 assertEquals(Optional.of(mockedLeafNode),
85 NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath));
86 assertEquals(Optional.empty(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));
88 final var pathArguments = childPath.relativeTo(rootPath).orElseThrow().getPathArguments()
89 .toArray(new PathArgument[0]);
91 assertEquals(Optional.of(mockedLeafNode),
92 NormalizedNodes.findNode(Optional.of(mockedDataContainerNode), pathArguments));
94 assertEquals(Optional.of(mockedLeafNode), NormalizedNodes.findNode(mockedDataContainerNode, pathArguments));
98 public void testToStringTree() {
99 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
100 final QName leafNodeQName = QName.create("test-ns", "2016-09-16", "leaf-node");
101 final NodeIdentifier leafNodeId = new NodeIdentifier(leafNodeQName);
102 doReturn(leafNodeId).when(mockedLeafNode).name();
103 doReturn("str-value-1").when(mockedLeafNode).body();
105 String stringTree = NormalizedNodes.toStringTree(mockedLeafNode);
106 assertNotNull(stringTree);
107 assertEquals("leaf-node str-value-1\n", stringTree);
109 final QName listQName = QName.create("test-ns", "2016-09-16", "list-node");
111 final SystemMapNode mockedMapNode = mock(SystemMapNode.class);
112 final NodeIdentifier listNodeId = new NodeIdentifier(listQName);
113 doReturn(listNodeId).when(mockedMapNode).name();
115 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
116 final NodeIdentifierWithPredicates listEntryNodeId = NodeIdentifierWithPredicates.of(listQName,
117 leafNodeQName, "key-leaf-value");
118 doReturn(listEntryNodeId).when(mockedMapEntryNode).name();
119 doReturn(List.of(mockedMapEntryNode)).when(mockedMapNode).body();
121 doReturn(List.of(mockedLeafNode)).when(mockedMapEntryNode).body();
123 stringTree = NormalizedNodes.toStringTree(mockedMapNode);
124 assertNotNull(stringTree);
127 list-node[key-leaf-value] {
128 leaf-node str-value-1