9604fcdebbec305e952bb362e8daacbf03389aab
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / restconf / jersey / providers / ParameterAwareNormalizedNodeWriterParametersTest.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.restconf.jersey.providers;
10
11 import com.google.common.collect.Sets;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Set;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.InOrder;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
31
32 /**
33  * Unit test for {@link ParameterAwareNormalizedNodeWriter} used with all parameters.
34  */
35 public class ParameterAwareNormalizedNodeWriterParametersTest {
36
37     @Mock
38     private NormalizedNodeStreamWriter writer;
39     @Mock
40     private ContainerNode containerNodeData;
41     @Mock
42     private LeafSetNode<String> leafSetNodeData;
43     @Mock
44     private LeafSetEntryNode<String> leafSetEntryNodeData;
45     @Mock
46     private ContainerNode rootDataContainerData;
47
48     private NodeIdentifier containerNodeIdentifier;
49     private NodeIdentifier leafSetNodeIdentifier;
50     private NodeWithValue<?> leafSetEntryNodeIdentifier;
51     private NodeIdentifier rootDataContainerIdentifier;
52
53     private Collection<DataContainerChild<?, ?>> containerNodeValue;
54     private Collection<LeafSetEntryNode<String>> leafSetNodeValue;
55     private String leafSetEntryNodeValue;
56     private Collection<DataContainerChild<?, ?>> rootDataContainerValue;
57
58     @Before
59     public void setUp() {
60         MockitoAnnotations.initMocks(this);
61
62         // identifiers
63         containerNodeIdentifier = NodeIdentifier.create(QName.create("namespace", "container"));
64         Mockito.when(containerNodeData.getIdentifier()).thenReturn(containerNodeIdentifier);
65         Mockito.when(containerNodeData.getNodeType()).thenReturn(containerNodeIdentifier.getNodeType());
66
67         final QName leafSetEntryNodeQName = QName.create("namespace", "leaf-set-entry");
68         leafSetEntryNodeValue = "leaf-set-value";
69         leafSetEntryNodeIdentifier = new NodeWithValue<>(leafSetEntryNodeQName, leafSetEntryNodeValue);
70         Mockito.when(leafSetEntryNodeData.getIdentifier()).thenReturn(leafSetEntryNodeIdentifier);
71         Mockito.when(leafSetEntryNodeData.getNodeType()).thenReturn(leafSetEntryNodeIdentifier.getNodeType());
72         Mockito.when(leafSetEntryNodeData.getNodeType()).thenReturn(leafSetEntryNodeQName);
73
74         leafSetNodeIdentifier = NodeIdentifier.create(QName.create("namespace", "leaf-set"));
75         Mockito.when(leafSetNodeData.getIdentifier()).thenReturn(leafSetNodeIdentifier);
76         Mockito.when(leafSetNodeData.getNodeType()).thenReturn(leafSetNodeIdentifier.getNodeType());
77
78         rootDataContainerIdentifier = NodeIdentifier.create(
79                 QName.create("urn:ietf:params:xml:ns:netconf:base:1.0", "data"));
80         Mockito.when(rootDataContainerData.getIdentifier()).thenReturn(rootDataContainerIdentifier);
81         Mockito.when(rootDataContainerData.getNodeType()).thenReturn(rootDataContainerIdentifier.getNodeType());
82
83         // values
84         Mockito.when(leafSetEntryNodeData.getValue()).thenReturn(leafSetEntryNodeValue);
85
86         leafSetNodeValue = Collections.singletonList(leafSetEntryNodeData);
87         Mockito.when(leafSetNodeData.getValue()).thenReturn(leafSetNodeValue);
88
89         containerNodeValue = Collections.singleton(leafSetNodeData);
90         Mockito.when(containerNodeData.getValue()).thenReturn(containerNodeValue);
91
92         rootDataContainerValue = Collections.singleton(leafSetNodeData);
93         Mockito.when(rootDataContainerData.getValue()).thenReturn(rootDataContainerValue);
94     }
95
96     /**
97      * Test write {@link ContainerNode} when all its children are selected to be written by fields parameter.
98      * Depth parameter is also used and limits output to depth 1.
99      * Fields parameter has effect limiting depth parameter in the way that selected nodes and its ancestors are
100      * written regardless of their depth (some of container children have depth > 1).
101      * Fields parameter selects all container children to be written and also all children of those children.
102      */
103     @Test
104     public void writeContainerParameterPrioritiesTest() throws Exception {
105         final List<Set<QName>> limitedFields = new ArrayList<>();
106         limitedFields.add(Sets.newHashSet(leafSetNodeIdentifier.getNodeType()));
107         limitedFields.add(Sets.newHashSet(leafSetEntryNodeIdentifier.getNodeType()));
108
109         final ParameterAwareNormalizedNodeWriter parameterWriter = ParameterAwareNormalizedNodeWriter.forStreamWriter(
110                 writer, 1, limitedFields);
111
112         parameterWriter.write(containerNodeData);
113
114         final InOrder inOrder = Mockito.inOrder(writer);
115         inOrder.verify(writer, Mockito.times(1)).startContainerNode(containerNodeIdentifier, containerNodeValue.size());
116         inOrder.verify(writer, Mockito.times(1)).startLeafSet(leafSetNodeIdentifier, leafSetNodeValue.size());
117         inOrder.verify(writer, Mockito.times(1)).leafSetEntryNode(
118                 leafSetEntryNodeIdentifier.getNodeType(), leafSetEntryNodeValue);
119         inOrder.verify(writer, Mockito.times(2)).endNode();
120         Mockito.verifyNoMoreInteractions(writer);
121     }
122
123     /**
124      * Test write {@link ContainerNode} which represents data at restconf/data root.
125      * No parameters are used.
126      */
127     @Test
128     public void writeRootDataTest() throws Exception {
129         final ParameterAwareNormalizedNodeWriter parameterWriter = ParameterAwareNormalizedNodeWriter.forStreamWriter(
130                 writer, null, null);
131
132         parameterWriter.write(rootDataContainerData);
133
134         final InOrder inOrder = Mockito.inOrder(writer);
135         inOrder.verify(writer, Mockito.times(1)).startLeafSet(leafSetNodeIdentifier, leafSetNodeValue.size());
136         inOrder.verify(writer, Mockito.times(1)).leafSetEntryNode(
137                 leafSetEntryNodeIdentifier.getNodeType(), leafSetEntryNodeValue);
138         inOrder.verify(writer, Mockito.times(1)).endNode();
139         Mockito.verifyNoMoreInteractions(writer);
140     }
141 }