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.Assert.assertEquals;
11 import static org.junit.Assert.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;
17 import com.google.common.collect.ImmutableSet;
18 import java.util.List;
19 import java.util.Optional;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.junit.MockitoJUnitRunner;
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;
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class NormalizedNodesTest {
35 public void testGetDirectChild() {
36 final PathArgument mockedPathArgument = mock(PathArgument.class);
38 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
39 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafNode, mockedPathArgument));
41 final LeafSetEntryNode<?> mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
42 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafSetEntryNode, mockedPathArgument));
44 final DataContainerNode mockedDataContainerNode = mock(DataContainerNode.class);
45 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
46 doReturn(mockedContainerNode).when(mockedDataContainerNode).childByArg(any(PathArgument.class));
47 doCallRealMethod().when(mockedDataContainerNode).findChildByArg(any(PathArgument.class));
49 assertEquals(mockedContainerNode, NormalizedNodes.getDirectChild(mockedDataContainerNode, mockedPathArgument)
52 final SystemMapNode mockedMapNode = mock(SystemMapNode.class);
53 final QName listQName = QName.create("test-ns", "test-list");
54 final QName listKeyQName = QName.create("test-ns", "test-list-key");
55 final NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
56 NodeIdentifierWithPredicates.of(listQName, listKeyQName, "str-value");
57 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
58 doReturn(mockedMapEntryNode).when(mockedMapNode).childByArg(any(NodeIdentifierWithPredicates.class));
59 doCallRealMethod().when(mockedMapNode).findChildByArg(any(NodeIdentifierWithPredicates.class));
61 assertEquals(mockedMapEntryNode, NormalizedNodes.getDirectChild(mockedMapNode, nodeIdentifierWithPredicates)
63 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedMapNode, mockedPathArgument));
65 final SystemLeafSetNode<?> mockedLeafSetNode = mock(SystemLeafSetNode.class);
66 final QName leafListQName = QName.create("test-ns", "test-leaf-list");
67 final NodeWithValue<?> nodeWithValue = new NodeWithValue<>(leafListQName, "str-value");
68 doReturn(mockedLeafSetEntryNode).when(mockedLeafSetNode).childByArg(any(NodeWithValue.class));
69 doCallRealMethod().when(mockedLeafSetNode).findChildByArg(any(NodeWithValue.class));
70 assertEquals(mockedLeafSetEntryNode, NormalizedNodes.getDirectChild(mockedLeafSetNode, nodeWithValue).get());
74 public void testFindNode() {
75 final DataContainerNode mockedDataContainerNode = mock(DataContainerNode.class);
76 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
77 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
78 doReturn(mockedContainerNode).when(mockedDataContainerNode).childByArg(any(PathArgument.class));
79 doCallRealMethod().when(mockedDataContainerNode).findChildByArg(any(PathArgument.class));
80 doReturn(mockedLeafNode).when(mockedContainerNode).childByArg(any(PathArgument.class));
81 doCallRealMethod().when(mockedContainerNode).findChildByArg(any(PathArgument.class));
83 final QName node1QName = QName.create("test-ns", "2016-09-16", "node1");
84 final QName node2Qname = QName.create("test-ns", "2016-09-16", "node2");
85 final QName node3QName = QName.create("test-ns", "2016-09-16", "node3");
86 final QName node4Qname = QName.create("test-ns", "2016-09-16", "node4");
88 final YangInstanceIdentifier rootPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
89 new NodeIdentifier(node2Qname));
90 final YangInstanceIdentifier childPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
91 new NodeIdentifier(node2Qname), new NodeIdentifier(node3QName), new NodeIdentifier(node4Qname));
93 assertEquals(mockedLeafNode, NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath).get());
94 assertEquals(Optional.empty(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));
96 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
97 final PathArgument[] pathArguments = relativePath.get().getPathArguments().toArray(new PathArgument[2]);
99 assertEquals(mockedLeafNode, NormalizedNodes.findNode(Optional.of(mockedDataContainerNode),
100 pathArguments).get());
102 assertEquals(mockedLeafNode, NormalizedNodes.findNode(mockedDataContainerNode, pathArguments).get());
106 public void testToStringTree() {
107 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
108 final QName leafNodeQName = QName.create("test-ns", "2016-09-16", "leaf-node");
109 final NodeIdentifier leafNodeId = new NodeIdentifier(leafNodeQName);
110 doReturn(leafNodeId).when(mockedLeafNode).getIdentifier();
111 doReturn("str-value-1").when(mockedLeafNode).body();
113 String stringTree = NormalizedNodes.toStringTree(mockedLeafNode);
114 assertNotNull(stringTree);
115 assertEquals("leaf-node str-value-1\n", stringTree);
117 final AugmentationNode mockedAugmentationNode = mock(AugmentationNode.class);
118 final QName listQName = QName.create("test-ns", "2016-09-16", "list-node");
119 final AugmentationIdentifier augNodeId = new AugmentationIdentifier(ImmutableSet.of(listQName));
120 doReturn(augNodeId).when(mockedAugmentationNode).getIdentifier();
122 final SystemMapNode mockedMapNode = mock(SystemMapNode.class);
123 final NodeIdentifier listNodeId = new NodeIdentifier(listQName);
124 doReturn(listNodeId).when(mockedMapNode).getIdentifier();
125 doReturn(List.of(mockedMapNode)).when(mockedAugmentationNode).body();
127 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
128 final NodeIdentifierWithPredicates listEntryNodeId = NodeIdentifierWithPredicates.of(listQName,
129 leafNodeQName, "key-leaf-value");
130 doReturn(listEntryNodeId).when(mockedMapEntryNode).getIdentifier();
131 doReturn(List.of(mockedMapEntryNode)).when(mockedMapNode).body();
133 doReturn(List.of(mockedLeafNode)).when(mockedMapEntryNode).body();
135 stringTree = NormalizedNodes.toStringTree(mockedAugmentationNode);
136 assertNotNull(stringTree);
137 assertEquals("augmentation {\n list-node {\n list-node[key-leaf-value] {\n leaf-node "
138 + "str-value-1\n }\n }\n}\n", stringTree);