BUG 392: Resolved translation to JSON behind mount point
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / CnSnToXmlAndJsonInstanceIdentifierTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.ByteArrayInputStream;
14 import java.io.IOException;
15 import java.net.URI;
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;
21 import java.util.Map;
22
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
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.controller.sal.restconf.impl.CompositeNodeWrapper;
36 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
42 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
43
44 public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
45
46     @BeforeClass
47     public static void initialize() {
48         dataLoad("/instanceidentifier/yang", 3, "instance-identifier-module", "cont");
49     }
50
51     @Test
52     public void saveCnSnToXml() throws WebApplicationException, IOException, URISyntaxException, XMLStreamException {
53         CompositeNode cnSn = prepareCnSn();
54         String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
55                 StructuredDataToXmlProvider.INSTANCE);
56         validateXmlOutput(output);
57         // System.out.println(output);
58
59     }
60
61     @Ignore
62     @Test
63     public void saveCnSnToJson() throws WebApplicationException, IOException, URISyntaxException {
64         CompositeNode cnSn = prepareCnSn();
65         String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
66                 StructuredDataToJsonProvider.INSTANCE);
67         assertTrue(output
68                 .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\""));
69         // System.out.println(output);
70     }
71
72     private void validateXmlOutput(String xml) throws XMLStreamException {
73         XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
74         XMLEventReader eventReader;
75
76         eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
77         String aaModulePrefix = null;
78         String aModulePrefix = null;
79         String iiModulePrefix = null;
80         while (eventReader.hasNext()) {
81             XMLEvent nextEvent = eventReader.nextEvent();
82             if (nextEvent.isStartElement()) {
83                 StartElement startElement = (StartElement) nextEvent;
84                 if (startElement.getName().getLocalPart().equals("lf111")) {
85                     Iterator prefixes = startElement.getNamespaceContext().getPrefixes("augment:augment:module");
86
87                     while (prefixes.hasNext() && aaModulePrefix == null) {
88                         String prefix = (String) prefixes.next();
89                         if (!prefix.isEmpty()) {
90                             aaModulePrefix = prefix;
91                         }
92                     }
93
94                     aModulePrefix = startElement.getNamespaceContext().getPrefix("augment:module");
95                     iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
96                     break;
97                 }
98             }
99         }
100
101         assertNotNull(aaModulePrefix);
102         assertNotNull(aModulePrefix);
103         assertNotNull(iiModulePrefix);
104
105         String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/" + aModulePrefix
106                 + ":lst11[" + aModulePrefix + ":keyvalue111='value1'][" + aModulePrefix + ":keyvalue112='value2']/"
107                 + aaModulePrefix + ":lf112";
108
109 //        System.out.println(xml);
110         assertTrue(xml.contains(instanceIdentifierValue));
111
112     }
113
114     private CompositeNode prepareCnSn() throws URISyntaxException {
115         CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("instance:identifier:module"), "cont");
116         CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("instance:identifier:module"), "cont1");
117         CompositeNodeWrapper lst11 = new CompositeNodeWrapper(new URI("augment:module"), "lst11");
118         InstanceIdentifier instanceIdentifier = createInstanceIdentifier();
119         SimpleNodeWrapper lf111 = new SimpleNodeWrapper(new URI("augment:augment:module"), "lf111", instanceIdentifier);
120
121         lst11.addValue(lf111);
122         cont1.addValue(lst11);
123         cont.addValue(cont1);
124
125         return cont;
126     }
127
128     private InstanceIdentifier createInstanceIdentifier() throws URISyntaxException {
129         List<PathArgument> pathArguments = new ArrayList<>();
130         pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont")));
131         pathArguments.add(new NodeIdentifier(new QName(new URI("instance:identifier:module"), "cont1")));
132
133         QName qName = new QName(new URI("augment:module"), "lst11");
134         Map<QName, Object> keyValues = new HashMap<>();
135         keyValues.put(new QName(new URI("augment:module"), "keyvalue111"), "value1");
136         keyValues.put(new QName(new URI("augment:module"), "keyvalue112"), "value2");
137         NodeIdentifierWithPredicates nodeIdentifierWithPredicates = new NodeIdentifierWithPredicates(qName, keyValues);
138         pathArguments.add(nodeIdentifierWithPredicates);
139
140         pathArguments.add(new NodeIdentifier(new QName(new URI("augment:augment:module"), "lf112")));
141
142         return new InstanceIdentifier(pathArguments);
143     }
144
145 }