BUG-868: do not use deprecated InstanceIdentifier methods
[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.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;
45
46 public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
47
48     @BeforeClass
49     public static void initialize() {
50         dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
51     }
52
53     @Test
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);
58         //uncomment for debug
59         // System.out.println(output);
60         validateXmlOutput(output);
61
62     }
63
64     @Ignore
65     @Test
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);
71         //uncomment for debug
72         // System.out.println(output);
73         validateXmlOutputWithLeafList(output);
74     }
75
76     @Test
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;
82         strInOutput = output
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\"");
84
85         if (!strInOutput) {
86             strInOutput = output
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\"");
88         }
89         //uncomment for debug
90         // System.out.println(output);
91         assertTrue(strInOutput);
92     }
93
94
95     @Test
96     public void saveCnSnWithLeafListInstIdentifierToJsonTest() throws WebApplicationException, IOException,
97     URISyntaxException {
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;
104         strInOutput = output
105                 .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.='lflst11_1']\"");
106         if (!strInOutput) {
107             strInOutput = output
108                     .contains("\"augment-augment-module:lf111\": \"/instance-identifier-module:cont/instance-identifier-module:cont1/augment-module-leaf-list:lflst11[.=\\\"lflst11_1\\\"]\"");
109         }
110
111         assertTrue(strInOutput);
112     }
113
114     private void validateXmlOutput(final String xml) throws XMLStreamException {
115         XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
116         XMLEventReader eventReader;
117
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");
128
129                     while (prefixes.hasNext() && aaModulePrefix == null) {
130                         String prefix = (String) prefixes.next();
131                         if (!prefix.isEmpty()) {
132                             aaModulePrefix = prefix;
133                         }
134                     }
135
136                     aModulePrefix = startElement.getNamespaceContext().getPrefix("augment:module");
137                     iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
138                     break;
139                 }
140             }
141         }
142
143         assertNotNull(aaModulePrefix);
144         assertNotNull(aModulePrefix);
145         assertNotNull(iiModulePrefix);
146
147         String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/" + aModulePrefix
148                 + ":lst11[" + aModulePrefix + ":keyvalue111='value1'][" + aModulePrefix + ":keyvalue112='value2']/"
149                 + aaModulePrefix + ":lf112";
150
151         assertTrue(xml.contains(instanceIdentifierValue));
152
153     }
154
155     private void validateXmlOutputWithLeafList(final String xml) throws XMLStreamException {
156         XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
157         XMLEventReader eventReader;
158
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");
168
169                     while (prefixes.hasNext() && aModuleLfLstPrefix == null) {
170                         String prefix = (String) prefixes.next();
171                         if (!prefix.isEmpty()) {
172                             aModuleLfLstPrefix = prefix;
173                         }
174                     }
175                     iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
176                     break;
177                 }
178             }
179         }
180
181         assertNotNull(aModuleLfLstPrefix);
182         assertNotNull(iiModulePrefix);
183
184         String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/"
185                 + aModuleLfLstPrefix + ":lflst11[.='lflst11_1']";
186
187         assertTrue(xml.contains(instanceIdentifierValue));
188
189     }
190
191     private CompositeNode prepareCnSn(final 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);
198
199         MutableSimpleNode<?> lf111 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111", "augment:augment:module", "2014-01-17"),
200                 lst11, instanceIdentifier,null,null);
201
202
203         lst11.getValue().add(lf111);
204         lst11.init();
205
206         cont1.getValue().add(lst11);
207         cont1.init();
208
209         cont.getValue().add(cont1);
210         cont.init();
211
212         return cont;
213     }
214
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")));
219
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);
226
227         pathArguments.add(new NodeIdentifier(new QName(new URI("augment:augment:module"), "lf112")));
228
229         return InstanceIdentifier.create(pathArguments);
230     }
231
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"));
237
238         return InstanceIdentifier.create(pathArguments);
239     }
240
241 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.