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