2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.opendaylight.controller.sal.restconf.impl.test.TestUtils.containsStringData;
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.List;
23 import javax.ws.rs.WebApplicationException;
24 import javax.xml.stream.XMLEventReader;
25 import javax.xml.stream.XMLInputFactory;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.events.StartElement;
28 import javax.xml.stream.events.XMLEvent;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
32 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
35 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
41 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
42 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
43 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
45 public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
48 public static void initialize() {
49 dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
53 public void saveCnSnToXmlTest() throws WebApplicationException, IOException, URISyntaxException, XMLStreamException {
54 CompositeNode cnSn = prepareCnSn(createInstanceIdentifier());
55 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
56 StructuredDataToXmlProvider.INSTANCE);
57 validateXmlOutput(output);
62 public void saveCnSnWithLeafListInstIdentifierToXmlTest() throws WebApplicationException, IOException,
63 URISyntaxException, XMLStreamException {
64 CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList());
65 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
66 StructuredDataToXmlProvider.INSTANCE);
67 validateXmlOutputWithLeafList(output);
71 public void saveCnSnToJsonTest() throws WebApplicationException, IOException, URISyntaxException {
72 CompositeNode cnSn = prepareCnSn(createInstanceIdentifier());
73 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
74 StructuredDataToJsonProvider.INSTANCE);
75 boolean strInOutput = false;
76 strInOutput = containsStringData(
78 "\"augment-augment-module:lf111\"",
80 "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11\\[augment-module:keyvalue111=\\\\\"value1\\\\\"\\]\\[augment-module:keyvalue112=\\\\\"value2\\\\\"\\]/augment-augment-module:lf112\"");
83 strInOutput = containsStringData(
85 "\"augment-augment-module:lf111\"",
87 "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11\\[augment-module:keyvalue111='value1'\\]\\[augment-module:keyvalue112='value2'\\]/augment-augment-module:lf112\"");
89 assertTrue(strInOutput);
93 public void saveCnSnWithLeafListInstIdentifierToJsonTest() throws WebApplicationException, IOException,
95 CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList());
96 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
97 StructuredDataToJsonProvider.INSTANCE);
98 boolean strInOutput = false;
99 strInOutput = containsStringData(
101 "\"augment-augment-module:lf111\"",
103 "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11\\[.='lflst11_1'\\]\"");
105 strInOutput = containsStringData(
107 "\"augment-augment-module:lf111\"",
109 "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11\\[.=\\\\\"lflst11_1\\\\\"\\]\"");
112 assertTrue(strInOutput);
115 private void validateXmlOutput(final String xml) throws XMLStreamException {
116 XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
117 XMLEventReader eventReader;
119 eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
120 String aaModulePrefix = null;
121 String aModulePrefix = null;
122 String iiModulePrefix = null;
123 while (eventReader.hasNext()) {
124 XMLEvent nextEvent = eventReader.nextEvent();
125 if (nextEvent.isStartElement()) {
126 StartElement startElement = (StartElement) nextEvent;
127 if (startElement.getName().getLocalPart().equals("lf111")) {
128 Iterator<?> prefixes = startElement.getNamespaceContext().getPrefixes("augment:augment:module");
130 while (prefixes.hasNext() && aaModulePrefix == null) {
131 String prefix = (String) prefixes.next();
132 if (!prefix.isEmpty()) {
133 aaModulePrefix = prefix;
137 aModulePrefix = startElement.getNamespaceContext().getPrefix("augment:module");
138 iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
144 assertNotNull(aaModulePrefix);
145 assertNotNull(aModulePrefix);
146 assertNotNull(iiModulePrefix);
148 String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/" + aModulePrefix
149 + ":lst11[" + aModulePrefix + ":keyvalue111='value1'][" + aModulePrefix + ":keyvalue112='value2']/"
150 + aaModulePrefix + ":lf112";
152 assertTrue(xml.contains(instanceIdentifierValue));
156 private void validateXmlOutputWithLeafList(final String xml) throws XMLStreamException {
157 XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
158 XMLEventReader eventReader;
160 eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
161 String aModuleLfLstPrefix = null;
162 String iiModulePrefix = null;
163 while (eventReader.hasNext()) {
164 XMLEvent nextEvent = eventReader.nextEvent();
165 if (nextEvent.isStartElement()) {
166 StartElement startElement = (StartElement) nextEvent;
167 if (startElement.getName().getLocalPart().equals("lf111")) {
168 Iterator<?> prefixes = startElement.getNamespaceContext().getPrefixes("augment:module:leaf:list");
170 while (prefixes.hasNext() && aModuleLfLstPrefix == null) {
171 String prefix = (String) prefixes.next();
172 if (!prefix.isEmpty()) {
173 aModuleLfLstPrefix = prefix;
176 iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
182 assertNotNull(aModuleLfLstPrefix);
183 assertNotNull(iiModulePrefix);
185 String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/"
186 + aModuleLfLstPrefix + ":lflst11[.='lflst11_1']";
188 assertTrue(xml.contains(instanceIdentifierValue));
192 private CompositeNode prepareCnSn(final InstanceIdentifier instanceIdentifier) throws URISyntaxException {
193 CompositeNodeBuilder<ImmutableCompositeNode> cont = ImmutableCompositeNode.builder();
194 cont.setQName(QName.create("instance:identifier:module", "2014-01-17", "cont"));
196 CompositeNodeBuilder<ImmutableCompositeNode> cont1 = ImmutableCompositeNode.builder();
197 cont1.setQName(QName.create("instance:identifier:module", "2014-01-17", "cont1"));
199 CompositeNodeBuilder<ImmutableCompositeNode> lst11 = ImmutableCompositeNode.builder();
200 lst11.setQName(QName.create("augment:module", "2014-01-17", "lst11"));
202 SimpleNode<?> lf111 = NodeFactory.createImmutableSimpleNode(
203 QName.create("augment:augment:module", "2014-01-17", "lf111"), null, instanceIdentifier);
205 cont1.add(lst11.toInstance());
206 cont.add(cont1.toInstance());
207 return cont.toInstance();
210 private InstanceIdentifier createInstanceIdentifier() throws URISyntaxException {
211 List<PathArgument> pathArguments = new ArrayList<>();
212 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont")));
213 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont1")));
215 QName qName = new QName(new URI("augment:module"), "lst11");
216 Map<QName, Object> keyValues = new HashMap<>();
217 keyValues.put(new QName(new URI("augment:module"), "keyvalue111"), "value1");
218 keyValues.put(new QName(new URI("augment:module"), "keyvalue112"), "value2");
219 NodeIdentifierWithPredicates nodeIdentifierWithPredicates = new NodeIdentifierWithPredicates(qName, keyValues);
220 pathArguments.add(nodeIdentifierWithPredicates);
222 pathArguments.add(new NodeIdentifier(new QName(new URI("augment:augment:module"), "lf112")));
224 return InstanceIdentifier.create(pathArguments);
227 private InstanceIdentifier createInstanceIdentifierWithLeafList() throws URISyntaxException {
228 List<PathArgument> pathArguments = new ArrayList<>();
229 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont")));
230 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont1")));
231 pathArguments.add(new NodeWithValue(new QName(new URI("augment:module:leaf:list"), "lflst11"), "lflst11_1"));
233 return InstanceIdentifier.create(pathArguments);