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