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;
13 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
16 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 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;
30 import org.junit.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
37 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
41 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
42 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
43 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
44 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
46 public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
49 public static void initialize() {
50 dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
54 public void saveCnSnToXmlTest() throws WebApplicationException, IOException, URISyntaxException, XMLStreamException {
55 CompositeNode cnSn = prepareCnSn(createInstanceIdentifier());
56 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
57 StructuredDataToXmlProvider.INSTANCE);
59 // System.out.println(output);
60 validateXmlOutput(output);
66 public void saveCnSnWithLeafListInstIdentifierToXmlTest() throws WebApplicationException, IOException,
67 URISyntaxException, XMLStreamException {
68 CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList());
69 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
70 StructuredDataToXmlProvider.INSTANCE);
72 // System.out.println(output);
73 validateXmlOutputWithLeafList(output);
77 public void saveCnSnToJsonTest() throws WebApplicationException, IOException, URISyntaxException {
78 CompositeNode cnSn = prepareCnSn(createInstanceIdentifier());
79 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
80 StructuredDataToJsonProvider.INSTANCE);
81 boolean strInOutput = false;
83 .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11[augment-module:keyvalue111=\\\"value1\\\"][augment-module:keyvalue112=\\\"value2\\\"]/augment-augment-module:lf112\"");
87 .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11[augment-module:keyvalue111='value1'][augment-module:keyvalue112='value2']/augment-augment-module:lf112\"");
90 // System.out.println(output);
91 assertTrue(strInOutput);
96 public void saveCnSnWithLeafListInstIdentifierToJsonTest() throws WebApplicationException, IOException,
98 CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList());
99 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
100 StructuredDataToJsonProvider.INSTANCE);
101 //uncomment for debug
102 // System.out.println(output);
103 boolean strInOutput = false;
105 .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.='lflst11_1']\"");
108 .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\\"lflst11_1\\\"]\"");
111 assertTrue(strInOutput);
114 private void validateXmlOutput(String xml) throws XMLStreamException {
115 XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
116 XMLEventReader eventReader;
118 eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
119 String aaModulePrefix = null;
120 String aModulePrefix = null;
121 String iiModulePrefix = null;
122 while (eventReader.hasNext()) {
123 XMLEvent nextEvent = eventReader.nextEvent();
124 if (nextEvent.isStartElement()) {
125 StartElement startElement = (StartElement) nextEvent;
126 if (startElement.getName().getLocalPart().equals("lf111")) {
127 Iterator prefixes = startElement.getNamespaceContext().getPrefixes("augment:augment:module");
129 while (prefixes.hasNext() && aaModulePrefix == null) {
130 String prefix = (String) prefixes.next();
131 if (!prefix.isEmpty()) {
132 aaModulePrefix = prefix;
136 aModulePrefix = startElement.getNamespaceContext().getPrefix("augment:module");
137 iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
143 assertNotNull(aaModulePrefix);
144 assertNotNull(aModulePrefix);
145 assertNotNull(iiModulePrefix);
147 String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/" + aModulePrefix
148 + ":lst11[" + aModulePrefix + ":keyvalue111='value1'][" + aModulePrefix + ":keyvalue112='value2']/"
149 + aaModulePrefix + ":lf112";
151 assertTrue(xml.contains(instanceIdentifierValue));
155 private void validateXmlOutputWithLeafList(String xml) throws XMLStreamException {
156 XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
157 XMLEventReader eventReader;
159 eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
160 String aModuleLfLstPrefix = null;
161 String iiModulePrefix = null;
162 while (eventReader.hasNext()) {
163 XMLEvent nextEvent = eventReader.nextEvent();
164 if (nextEvent.isStartElement()) {
165 StartElement startElement = (StartElement) nextEvent;
166 if (startElement.getName().getLocalPart().equals("lf111")) {
167 Iterator prefixes = startElement.getNamespaceContext().getPrefixes("augment:module:leaf:list");
169 while (prefixes.hasNext() && aModuleLfLstPrefix == null) {
170 String prefix = (String) prefixes.next();
171 if (!prefix.isEmpty()) {
172 aModuleLfLstPrefix = prefix;
175 iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
181 assertNotNull(aModuleLfLstPrefix);
182 assertNotNull(iiModulePrefix);
184 String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/"
185 + aModuleLfLstPrefix + ":lflst11[.='lflst11_1']";
187 assertTrue(xml.contains(instanceIdentifierValue));
191 private CompositeNode prepareCnSn(InstanceIdentifier instanceIdentifier) throws URISyntaxException {
192 MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
193 TestUtils.buildQName("cont", "instance:identifier:module", "2014-01-17"), null, null,null,null);
194 MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(
195 TestUtils.buildQName("cont1", "instance:identifier:module", "2014-01-17"), cont, null,null,null);
196 MutableCompositeNode lst11 = NodeFactory.createMutableCompositeNode(
197 TestUtils.buildQName("lst11", "augment:module", "2014-01-17"), cont1, null,null,null);
199 MutableSimpleNode<?> lf111 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111", "augment:augment:module", "2014-01-17"),
200 lst11, instanceIdentifier,null,null);
203 lst11.getChildren().add(lf111);
206 cont1.getChildren().add(lst11);
209 cont.getChildren().add(cont1);
215 private InstanceIdentifier createInstanceIdentifier() throws URISyntaxException {
216 List<PathArgument> pathArguments = new ArrayList<>();
217 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont")));
218 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont1")));
220 QName qName = new QName(new URI("augment:module"), "lst11");
221 Map<QName, Object> keyValues = new HashMap<>();
222 keyValues.put(new QName(new URI("augment:module"), "keyvalue111"), "value1");
223 keyValues.put(new QName(new URI("augment:module"), "keyvalue112"), "value2");
224 NodeIdentifierWithPredicates nodeIdentifierWithPredicates = new NodeIdentifierWithPredicates(qName, keyValues);
225 pathArguments.add(nodeIdentifierWithPredicates);
227 pathArguments.add(new NodeIdentifier(new QName(new URI("augment:augment:module"), "lf112")));
229 return new InstanceIdentifier(pathArguments);
232 private InstanceIdentifier createInstanceIdentifierWithLeafList() throws URISyntaxException {
233 List<PathArgument> pathArguments = new ArrayList<>();
234 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont")));
235 pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont1")));
236 pathArguments.add(new NodeWithValue(new QName(new URI("augment:module:leaf:list"), "lflst11"), "lflst11_1"));
238 return new InstanceIdentifier(pathArguments);