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