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.restconf.nb.rfc8040.jersey.providers;
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;
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;
34 * Unit test for {@link ParameterAwareNormalizedNodeWriter} used with all parameters.
36 @RunWith(MockitoJUnitRunner.StrictStubs.class)
37 public class ParameterAwareNormalizedNodeWriterParametersTest {
40 private NormalizedNodeStreamWriter writer;
42 private ContainerNode containerNodeData;
44 private LeafSetNode<String> leafSetNodeData;
46 private LeafSetEntryNode<String> leafSetEntryNodeData;
48 private ContainerNode rootDataContainerData;
50 private NodeIdentifier containerNodeIdentifier;
51 private NodeIdentifier leafSetNodeIdentifier;
52 private NodeWithValue<String> leafSetEntryNodeIdentifier;
54 private Collection<DataContainerChild> containerNodeValue;
55 private Collection<LeafSetEntryNode<String>> leafSetNodeValue;
56 private String leafSetEntryNodeValue;
57 private Collection<DataContainerChild> rootDataContainerValue;
62 containerNodeIdentifier = NodeIdentifier.create(QName.create("namespace", "container"));
63 Mockito.when(containerNodeData.getIdentifier()).thenReturn(containerNodeIdentifier);
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);
70 leafSetNodeIdentifier = NodeIdentifier.create(QName.create("namespace", "leaf-set"));
71 Mockito.when(leafSetNodeData.getIdentifier()).thenReturn(leafSetNodeIdentifier);
74 Mockito.when(leafSetEntryNodeData.body()).thenReturn(leafSetEntryNodeValue);
76 leafSetNodeValue = Collections.singletonList(leafSetEntryNodeData);
77 Mockito.when(leafSetNodeData.body()).thenReturn(leafSetNodeValue);
79 containerNodeValue = Collections.singleton(leafSetNodeData);
80 Mockito.when(containerNodeData.body()).thenReturn(containerNodeValue);
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);
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.
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()));
101 final ParameterAwareNormalizedNodeWriter parameterWriter = ParameterAwareNormalizedNodeWriter.forStreamWriter(
102 writer, 1, limitedFields);
104 parameterWriter.write(containerNodeData);
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);
116 * Test write {@link ContainerNode} which represents data at restconf/data root.
117 * No parameters are used.
120 public void writeRootDataTest() throws Exception {
121 final ParameterAwareNormalizedNodeWriter parameterWriter = ParameterAwareNormalizedNodeWriter.forStreamWriter(
124 parameterWriter.write(rootDataContainerData);
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);