Merge "Bug 1224, Bug 1221: Added Test case scenarios for Data Change Events."
[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 import static org.opendaylight.controller.sal.restconf.impl.test.TestUtils.containsStringData;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.IOException;
16 import java.net.URI;
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;
22 import java.util.Map;
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;
44
45 public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
46
47     @BeforeClass
48     public static void initialize() {
49         dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
50     }
51
52     @Test
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);
58
59     }
60
61     @Test
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);
68     }
69
70     @Test
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(
77                 output,
78                 "\"augment-augment-module:lf111\"",
79                 ":",
80                 "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11\\[augment-module:keyvalue111=\\\\\"value1\\\\\"\\]\\[augment-module:keyvalue112=\\\\\"value2\\\\\"\\]/augment-augment-module:lf112\"");
81
82         if (!strInOutput) {
83             strInOutput = containsStringData(
84                     output,
85                     "\"augment-augment-module:lf111\"",
86                     ":",
87                     "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module:lst11\\[augment-module:keyvalue111='value1'\\]\\[augment-module:keyvalue112='value2'\\]/augment-augment-module:lf112\"");
88         }
89         assertTrue(strInOutput);
90     }
91
92     @Test
93     public void saveCnSnWithLeafListInstIdentifierToJsonTest() throws WebApplicationException, IOException,
94             URISyntaxException {
95         CompositeNode cnSn = prepareCnSn(createInstanceIdentifierWithLeafList());
96         String output = TestUtils.writeCompNodeWithSchemaContextToOutput(cnSn, modules, dataSchemaNode,
97                 StructuredDataToJsonProvider.INSTANCE);
98         boolean strInOutput = false;
99         strInOutput = containsStringData(
100                 output,
101                 "\"augment-augment-module:lf111\"",
102                 ":",
103                 "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11\\[.='lflst11_1'\\]\"");
104         if (!strInOutput) {
105             strInOutput = containsStringData(
106                     output,
107                     "\"augment-augment-module:lf111\"",
108                     ":",
109                     "\"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11\\[.=\\\\\"lflst11_1\\\\\"\\]\"");
110         }
111
112         assertTrue(strInOutput);
113     }
114
115     private void validateXmlOutput(final String xml) throws XMLStreamException {
116         XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
117         XMLEventReader eventReader;
118
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");
129
130                     while (prefixes.hasNext() && aaModulePrefix == null) {
131                         String prefix = (String) prefixes.next();
132                         if (!prefix.isEmpty()) {
133                             aaModulePrefix = prefix;
134                         }
135                     }
136
137                     aModulePrefix = startElement.getNamespaceContext().getPrefix("augment:module");
138                     iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
139                     break;
140                 }
141             }
142         }
143
144         assertNotNull(aaModulePrefix);
145         assertNotNull(aModulePrefix);
146         assertNotNull(iiModulePrefix);
147
148         String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/" + aModulePrefix
149                 + ":lst11[" + aModulePrefix + ":keyvalue111='value1'][" + aModulePrefix + ":keyvalue112='value2']/"
150                 + aaModulePrefix + ":lf112";
151
152         assertTrue(xml.contains(instanceIdentifierValue));
153
154     }
155
156     private void validateXmlOutputWithLeafList(final String xml) throws XMLStreamException {
157         XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
158         XMLEventReader eventReader;
159
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");
169
170                     while (prefixes.hasNext() && aModuleLfLstPrefix == null) {
171                         String prefix = (String) prefixes.next();
172                         if (!prefix.isEmpty()) {
173                             aModuleLfLstPrefix = prefix;
174                         }
175                     }
176                     iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
177                     break;
178                 }
179             }
180         }
181
182         assertNotNull(aModuleLfLstPrefix);
183         assertNotNull(iiModulePrefix);
184
185         String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/"
186                 + aModuleLfLstPrefix + ":lflst11[.='lflst11_1']";
187
188         assertTrue(xml.contains(instanceIdentifierValue));
189
190     }
191
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"));
195
196         CompositeNodeBuilder<ImmutableCompositeNode> cont1 = ImmutableCompositeNode.builder();
197         cont1.setQName(QName.create("instance:identifier:module", "2014-01-17", "cont1"));
198
199         CompositeNodeBuilder<ImmutableCompositeNode> lst11 = ImmutableCompositeNode.builder();
200         lst11.setQName(QName.create("augment:module", "2014-01-17", "lst11"));
201
202         SimpleNode<?> lf111 = NodeFactory.createImmutableSimpleNode(
203                 QName.create("augment:augment:module", "2014-01-17", "lf111"), null, instanceIdentifier);
204         lst11.add(lf111);
205         cont1.add(lst11.toInstance());
206         cont.add(cont1.toInstance());
207         return cont.toInstance();
208     }
209
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")));
214
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);
221
222         pathArguments.add(new NodeIdentifier(new QName(new URI("augment:augment:module"), "lf112")));
223
224         return InstanceIdentifier.create(pathArguments);
225     }
226
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"));
232
233         return InstanceIdentifier.create(pathArguments);
234     }
235
236 }