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
9 package org.opendaylight.yangtools.yang.data.api.schema;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.ArgumentMatchers.any;
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.Collections;
19 import java.util.Optional;
20 import org.junit.Test;
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.AugmentationIdentifier;
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;
29 public class NormalizedNodesTest {
32 public void testGetDirectChild() {
33 final PathArgument mockedPathArgument = mock(PathArgument.class);
35 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
36 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafNode, mockedPathArgument));
38 final LeafSetEntryNode<?> mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
39 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafSetEntryNode, mockedPathArgument));
41 final DataContainerNode<?> mockedDataContainerNode = mock(DataContainerNode.class);
42 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
43 doReturn(Optional.of(mockedContainerNode)).when(mockedDataContainerNode).getChild(any(PathArgument.class));
44 assertEquals(mockedContainerNode, NormalizedNodes.getDirectChild(mockedDataContainerNode, mockedPathArgument)
47 final MapNode mockedMapNode = mock(MapNode.class);
48 final QName listQName = QName.create("test-ns", "test-list");
49 final QName listKeyQName = QName.create("test-ns", "test-list-key");
50 final NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
51 NodeIdentifierWithPredicates.of(listQName, listKeyQName, "str-value");
52 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
53 doReturn(Optional.of(mockedMapEntryNode)).when(mockedMapNode).getChild(any(NodeIdentifierWithPredicates.class));
54 assertEquals(mockedMapEntryNode, NormalizedNodes.getDirectChild(mockedMapNode, nodeIdentifierWithPredicates)
56 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedMapNode, mockedPathArgument));
58 final LeafSetNode<?> mockedLeafSetNode = mock(LeafSetNode.class);
59 final QName leafListQName = QName.create("test-ns", "test-leaf-list");
60 final NodeWithValue<?> nodeWithValue = new NodeWithValue<>(leafListQName, "str-value");
61 doReturn(Optional.of(mockedLeafSetEntryNode)).when(mockedLeafSetNode).getChild(any(NodeWithValue.class));
62 assertEquals(mockedLeafSetEntryNode, NormalizedNodes.getDirectChild(mockedLeafSetNode, nodeWithValue).get());
66 public void testFindNode() {
67 final DataContainerNode<?> mockedDataContainerNode = mock(DataContainerNode.class);
68 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
69 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
70 doReturn(Optional.of(mockedContainerNode)).when(mockedDataContainerNode).getChild(any(PathArgument.class));
71 doReturn(Optional.of(mockedLeafNode)).when(mockedContainerNode).getChild(any(PathArgument.class));
73 final QName node1QName = QName.create("test-ns", "2016-09-16", "node1");
74 final QName node2Qname = QName.create("test-ns", "2016-09-16", "node2");
75 final QName node3QName = QName.create("test-ns", "2016-09-16", "node3");
76 final QName node4Qname = QName.create("test-ns", "2016-09-16", "node4");
78 final YangInstanceIdentifier rootPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
79 new NodeIdentifier(node2Qname));
80 final YangInstanceIdentifier childPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
81 new NodeIdentifier(node2Qname), new NodeIdentifier(node3QName), new NodeIdentifier(node4Qname));
83 assertEquals(mockedLeafNode, NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath).get());
84 assertEquals(Optional.empty(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));
86 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
87 final PathArgument[] pathArguments = relativePath.get().getPathArguments().toArray(new PathArgument[2]);
89 assertEquals(mockedLeafNode, NormalizedNodes.findNode(Optional.of(mockedDataContainerNode),
90 pathArguments).get());
92 assertEquals(mockedLeafNode, NormalizedNodes.findNode(mockedDataContainerNode, pathArguments).get());
96 public void testToStringTree() {
97 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
98 final QName leafNodeQName = QName.create("test-ns", "2016-09-16", "leaf-node");
99 final NodeIdentifier leafNodeId = new NodeIdentifier(leafNodeQName);
100 doReturn(leafNodeId).when(mockedLeafNode).getIdentifier();
101 doReturn("str-value-1").when(mockedLeafNode).getValue();
103 String stringTree = NormalizedNodes.toStringTree(mockedLeafNode);
104 assertNotNull(stringTree);
105 assertEquals("leaf-node str-value-1\n", stringTree);
107 final AugmentationNode mockedAugmentationNode = mock(AugmentationNode.class);
108 final QName listQName = QName.create("test-ns", "2016-09-16", "list-node");
109 final AugmentationIdentifier augNodeId = new AugmentationIdentifier(ImmutableSet.of(listQName));
110 doReturn(augNodeId).when(mockedAugmentationNode).getIdentifier();
112 final MapNode mockedMapNode = mock(MapNode.class);
113 final NodeIdentifier listNodeId = new NodeIdentifier(listQName);
114 doReturn(listNodeId).when(mockedMapNode).getIdentifier();
115 doReturn(Collections.singletonList(mockedMapNode)).when(mockedAugmentationNode).getValue();
117 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
118 final NodeIdentifierWithPredicates listEntryNodeId = NodeIdentifierWithPredicates.of(listQName,
119 leafNodeQName, "key-leaf-value");
120 doReturn(listEntryNodeId).when(mockedMapEntryNode).getIdentifier();
121 doReturn(Collections.singletonList(mockedMapEntryNode)).when(mockedMapNode).getValue();
123 doReturn(Collections.singletonList(mockedLeafNode)).when(mockedMapEntryNode).getValue();
125 stringTree = NormalizedNodes.toStringTree(mockedAugmentationNode);
126 assertNotNull(stringTree);
127 assertEquals("augmentation {\n list-node {\n list-node[key-leaf-value] {\n leaf-node "
128 + "str-value-1\n }\n }\n}\n", stringTree);