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.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
17 import java.util.Collections;
18 import java.util.Optional;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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 public class NormalizedNodesTest {
31 public void testGetDirectChild() {
32 final PathArgument mockedPathArgument = mock(PathArgument.class);
34 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
35 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafNode, mockedPathArgument));
37 final LeafSetEntryNode<?> mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
38 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedLeafSetEntryNode, mockedPathArgument));
40 final DataContainerNode<?> mockedDataContainerNode = mock(DataContainerNode.class);
41 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
42 doReturn(Optional.of(mockedContainerNode)).when(mockedDataContainerNode).getChild(any(PathArgument.class));
43 assertEquals(mockedContainerNode, NormalizedNodes.getDirectChild(mockedDataContainerNode, mockedPathArgument)
46 final MapNode mockedMapNode = mock(MapNode.class);
47 final QName listQName = QName.create("test-ns", "test-list");
48 final QName listKeyQName = QName.create("test-ns", "test-list-key");
49 final NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
50 new NodeIdentifierWithPredicates(listQName, listKeyQName, "str-value");
51 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
52 doReturn(Optional.of(mockedMapEntryNode)).when(mockedMapNode).getChild(any(NodeIdentifierWithPredicates.class));
53 assertEquals(mockedMapEntryNode, NormalizedNodes.getDirectChild(mockedMapNode, nodeIdentifierWithPredicates)
55 assertEquals(Optional.empty(), NormalizedNodes.getDirectChild(mockedMapNode, mockedPathArgument));
57 final LeafSetNode<?> mockedLeafSetNode = mock(LeafSetNode.class);
58 final QName leafListQName = QName.create("test-ns", "test-leaf-list");
59 final NodeWithValue<?> nodeWithValue = new NodeWithValue<>(leafListQName, "str-value");
60 doReturn(Optional.of(mockedLeafSetEntryNode)).when(mockedLeafSetNode).getChild(any(NodeWithValue.class));
61 assertEquals(mockedLeafSetEntryNode, NormalizedNodes.getDirectChild(mockedLeafSetNode, nodeWithValue).get());
65 public void testFindNode() {
66 final DataContainerNode<?> mockedDataContainerNode = mock(DataContainerNode.class);
67 final ContainerNode mockedContainerNode = mock(ContainerNode.class);
68 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
69 doReturn(Optional.of(mockedContainerNode)).when(mockedDataContainerNode).getChild(any(PathArgument.class));
70 doReturn(Optional.of(mockedLeafNode)).when(mockedContainerNode).getChild(any(PathArgument.class));
72 final QName node1QName = QName.create("test-ns", "2016-09-16", "node1");
73 final QName node2Qname = QName.create("test-ns", "2016-09-16", "node2");
74 final QName node3QName = QName.create("test-ns", "2016-09-16", "node3");
75 final QName node4Qname = QName.create("test-ns", "2016-09-16", "node4");
77 final YangInstanceIdentifier rootPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
78 new NodeIdentifier(node2Qname));
79 final YangInstanceIdentifier childPath = YangInstanceIdentifier.create(new NodeIdentifier(node1QName),
80 new NodeIdentifier(node2Qname), new NodeIdentifier(node3QName), new NodeIdentifier(node4Qname));
82 assertEquals(mockedLeafNode, NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath).get());
83 assertEquals(Optional.empty(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));
85 final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
86 final PathArgument[] pathArguments = relativePath.get().getPathArguments().toArray(new PathArgument[2]);
88 assertEquals(mockedLeafNode, NormalizedNodes.findNode(Optional.of(mockedDataContainerNode),
89 pathArguments).get());
91 assertEquals(mockedLeafNode, NormalizedNodes.findNode(mockedDataContainerNode, pathArguments).get());
95 public void testToStringTree() {
96 final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
97 final QName leafNodeQName = QName.create("test-ns", "2016-09-16", "leaf-node");
98 final NodeIdentifier leafNodeId = new NodeIdentifier(leafNodeQName);
99 doReturn(leafNodeId).when(mockedLeafNode).getIdentifier();
100 doReturn("str-value-1").when(mockedLeafNode).getValue();
102 String stringTree = NormalizedNodes.toStringTree(mockedLeafNode);
103 assertNotNull(stringTree);
104 assertEquals("leaf-node str-value-1\n", stringTree);
106 final AugmentationNode mockedAugmentationNode = mock(AugmentationNode.class);
107 final QName listQName = QName.create("test-ns", "2016-09-16", "list-node");
108 final AugmentationIdentifier augNodeId = new AugmentationIdentifier(Collections.singleton(listQName));
109 doReturn(augNodeId).when(mockedAugmentationNode).getIdentifier();
111 final MapNode mockedMapNode = mock(MapNode.class);
112 final NodeIdentifier listNodeId = new NodeIdentifier(listQName);
113 doReturn(listNodeId).when(mockedMapNode).getIdentifier();
114 doReturn(Collections.singletonList(mockedMapNode)).when(mockedAugmentationNode).getValue();
116 final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
117 final NodeIdentifierWithPredicates listEntryNodeId = new NodeIdentifierWithPredicates(listQName,
118 leafNodeQName, "key-leaf-value");
119 doReturn(listEntryNodeId).when(mockedMapEntryNode).getIdentifier();
120 doReturn(Collections.singletonList(mockedMapEntryNode)).when(mockedMapNode).getValue();
122 doReturn(Collections.singletonList(mockedLeafNode)).when(mockedMapEntryNode).getValue();
124 stringTree = NormalizedNodes.toStringTree(mockedAugmentationNode);
125 assertNotNull(stringTree);
126 assertEquals("augmentation {\n list-node {\n list-node[key-leaf-value] {\n leaf-node "
127 + "str-value-1\n }\n }\n}\n", stringTree);