Fix eclipse/checkstyle warnings
[yangtools.git] / yang / 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
9 package org.opendaylight.yangtools.yang.data.api.schema;
10
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;
16
17 import com.google.common.base.Optional;
18 import com.google.common.collect.Lists;
19 import com.google.common.collect.Sets;
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;
28
29 public class NormalizedNodesTest {
30
31     @Test
32     public void testGetDirectChild() {
33         final PathArgument mockedPathArgument = mock(PathArgument.class);
34
35         final LeafNode<?> mockedLeafNode = mock(LeafNode.class);
36         assertEquals(Optional.absent(), NormalizedNodes.getDirectChild(mockedLeafNode, mockedPathArgument));
37
38         final LeafSetEntryNode<?> mockedLeafSetEntryNode = mock(LeafSetEntryNode.class);
39         assertEquals(Optional.absent(), NormalizedNodes.getDirectChild(mockedLeafSetEntryNode, mockedPathArgument));
40
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)
45                 .get());
46
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                 new NodeIdentifierWithPredicates(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)
55                 .get());
56         assertEquals(Optional.absent(), NormalizedNodes.getDirectChild(mockedMapNode, mockedPathArgument));
57
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());
63     }
64
65     @Test
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));
72
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");
77
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));
82
83         assertEquals(mockedLeafNode, NormalizedNodes.findNode(rootPath, mockedDataContainerNode, childPath).get());
84         assertEquals(Optional.absent(), NormalizedNodes.findNode(childPath, mockedDataContainerNode, rootPath));
85
86         final Optional<YangInstanceIdentifier> relativePath = childPath.relativeTo(rootPath);
87         final PathArgument[] pathArguments = relativePath.get().getPathArguments().toArray(new PathArgument[2]);
88
89         assertEquals(mockedLeafNode, NormalizedNodes.findNode(Optional.of(mockedDataContainerNode),
90                 pathArguments).get());
91
92         assertEquals(mockedLeafNode, NormalizedNodes.findNode(mockedDataContainerNode, pathArguments).get());
93     }
94
95     @Test
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();
102
103         String stringTree = NormalizedNodes.toStringTree(mockedLeafNode);
104         assertNotNull(stringTree);
105         assertEquals("leaf-node str-value-1\n", stringTree);
106
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(Sets.newHashSet(listQName));
110         doReturn(augNodeId).when(mockedAugmentationNode).getIdentifier();
111
112         final MapNode mockedMapNode = mock(MapNode.class);
113         final NodeIdentifier listNodeId = new NodeIdentifier(listQName);
114         doReturn(listNodeId).when(mockedMapNode).getIdentifier();
115         doReturn(Lists.newArrayList(mockedMapNode)).when(mockedAugmentationNode).getValue();
116
117         final MapEntryNode mockedMapEntryNode = mock(MapEntryNode.class);
118         final NodeIdentifierWithPredicates listEntryNodeId = new NodeIdentifierWithPredicates(listQName,
119                 leafNodeQName, "key-leaf-value");
120         doReturn(listEntryNodeId).when(mockedMapEntryNode).getIdentifier();
121         doReturn(Lists.newArrayList(mockedMapEntryNode)).when(mockedMapNode).getValue();
122
123         doReturn(Lists.newArrayList(mockedLeafNode)).when(mockedMapEntryNode).getValue();
124
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);
129     }
130 }