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