Bug 7433 - Remove use of YangSchemaSourceImpl from restconf tests
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlBodyReader.java
1 /**
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import com.google.common.base.Optional;
16 import com.google.common.collect.Sets;
17 import java.io.File;
18 import java.io.InputStream;
19 import java.net.URI;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.Collection;
23 import javax.ws.rs.core.MediaType;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
27 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
28 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.QNameModule;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
36 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41
42 /**
43  * sal-rest-connector
44  * org.opendaylight.controller.sal.rest.impl.test.providers
45  *
46  *
47  *
48  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
49  *
50  * Created: Mar 7, 2015
51  */
52 public class TestXmlBodyReader extends AbstractBodyReaderTest {
53
54     private final XmlNormalizedNodeBodyReader xmlBodyReader;
55     private static SchemaContext schemaContext;
56     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
57
58     private static QNameModule initializeInstanceIdentifierModule() {
59         try {
60             return QNameModule.create(URI.create("instance:identifier:module"),
61                     new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
62         } catch (final ParseException e) {
63             throw new Error(e);
64         }
65     }
66
67     public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
68         super();
69         this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
70     }
71
72     @Override
73     protected MediaType getMediaType() {
74         return new MediaType(MediaType.APPLICATION_XML, null);
75     }
76
77     @BeforeClass
78     public static void initialization() throws Exception {
79         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
80         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
81         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
82         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
83         controllerContext.setSchemas(schemaContext);
84     }
85
86     @Test
87     public void moduleDataTest() throws Exception {
88         final DataSchemaNode dataSchemaNode =
89                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
90         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
91         final String uri = "instance-identifier-module:cont";
92         mockBodyReader(uri, this.xmlBodyReader, false);
93         final InputStream inputStream = TestXmlBodyReader.class
94                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
95         final NormalizedNodeContext returnValue = this.xmlBodyReader
96                 .readFrom(null, null, null, this.mediaType, null, inputStream);
97         checkNormalizedNodeContext(returnValue);
98         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
99     }
100
101     @Test
102     public void moduleSubContainerDataPutTest() throws Exception {
103         final DataSchemaNode dataSchemaNode =
104                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
105         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
106         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
107         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
108         final String uri = "instance-identifier-module:cont/cont1";
109         mockBodyReader(uri, this.xmlBodyReader, false);
110         final InputStream inputStream = TestXmlBodyReader.class
111                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
112         final NormalizedNodeContext returnValue = this.xmlBodyReader
113                 .readFrom(null, null, null, this.mediaType, null, inputStream);
114         checkNormalizedNodeContext(returnValue);
115         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
116     }
117
118     @Test
119     public void moduleSubContainerDataPostTest() throws Exception {
120         final DataSchemaNode dataSchemaNode =
121                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
122         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
123         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
124         final String uri = "instance-identifier-module:cont";
125         mockBodyReader(uri, this.xmlBodyReader, true);
126         final InputStream inputStream = TestXmlBodyReader.class
127                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
128         final NormalizedNodeContext returnValue = this.xmlBodyReader
129                 .readFrom(null, null, null, this.mediaType, null, inputStream);
130         checkNormalizedNodeContext(returnValue);
131         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
132     }
133
134     @Test
135     public void moduleSubContainerAugmentDataPostTest() throws Exception {
136         final DataSchemaNode dataSchemaNode =
137                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
138         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
139         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
140         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
141                 Sets.newHashSet(contAugmentQName));
142         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
143                 .node(augII).node(contAugmentQName);
144         final String uri = "instance-identifier-module:cont";
145         mockBodyReader(uri, this.xmlBodyReader, true);
146         final InputStream inputStream = TestXmlBodyReader.class
147                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
148         final NormalizedNodeContext returnValue = this.xmlBodyReader
149                 .readFrom(null, null, null, this.mediaType, null, inputStream);
150         checkNormalizedNodeContext(returnValue);
151         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
152     }
153
154     @Test
155     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
156         final DataSchemaNode dataSchemaNode =
157                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
158         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
159         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
160         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
161         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
162         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
163                 Sets.newHashSet(augmentChoice1QName));
164         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
165                 Sets.newHashSet(augmentChoice2QName));
166         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
167                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
168                 .node(containerQName);
169         final String uri = "instance-identifier-module:cont";
170         mockBodyReader(uri, this.xmlBodyReader, true);
171         final InputStream inputStream = TestXmlBodyReader.class
172                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
173         final NormalizedNodeContext returnValue = this.xmlBodyReader
174                 .readFrom(null, null, null, this.mediaType, null, inputStream);
175         checkNormalizedNodeContext(returnValue);
176         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
177     }
178
179     @Test
180     public void rpcModuleInputTest() throws Exception {
181         final String uri = "invoke-rpc-module:rpc-test";
182         mockBodyReader(uri, this.xmlBodyReader, true);
183         final InputStream inputStream = TestXmlBodyReader.class
184                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
185         final NormalizedNodeContext returnValue = this.xmlBodyReader
186                 .readFrom(null, null, null, this.mediaType, null, inputStream);
187         checkNormalizedNodeContext(returnValue);
188         final ContainerNode contNode = (ContainerNode) returnValue.getData();
189         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
190         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
191                 .getLastPathArgument());
192         assertTrue(contDataNodePotential.isPresent());
193         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
194         final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
195         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
196                 .getLastPathArgument());
197         assertTrue(leafDataNode.isPresent());
198         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
199     }
200
201     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
202             final NormalizedNodeContext nnContext) {
203         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
204     }
205
206     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
207                                                       final NormalizedNodeContext nnContext,
208                                                       final YangInstanceIdentifier dataNodeIdent) {
209         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
210         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
211         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
212     }
213
214     /**
215      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
216      * used to distinguish between them to find correct one. Check if container was found not only according to its name
217      * but also by correct namespace used in payload.
218      */
219     @Test
220     public void findFooContainerUsingNamespaceTest() throws Exception {
221         mockBodyReader("", this.xmlBodyReader, true);
222         final InputStream inputStream = TestXmlBodyReader.class
223                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
224         final NormalizedNodeContext returnValue = this.xmlBodyReader
225                 .readFrom(null, null, null, this.mediaType, null, inputStream);
226
227         // check return value
228         checkNormalizedNodeContext(returnValue);
229         // check if container was found both according to its name and namespace
230         assertEquals("Not correct container found, name was ignored",
231                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
232         assertEquals("Not correct container found, namespace was ignored",
233                 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
234     }
235
236     /**
237      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
238      * used to distinguish between them to find correct one. Check if container was found not only according to its name
239      * but also by correct namespace used in payload.
240      */
241     @Test
242     public void findBarContainerUsingNamespaceTest() throws Exception {
243         mockBodyReader("", this.xmlBodyReader, true);
244         final InputStream inputStream = TestXmlBodyReader.class
245                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
246         final NormalizedNodeContext returnValue = this.xmlBodyReader
247                 .readFrom(null, null, null, this.mediaType, null, inputStream);
248
249         // check return value
250         checkNormalizedNodeContext(returnValue);
251         // check if container was found both according to its name and namespace
252         assertEquals("Not correct container found, name was ignored",
253                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
254         assertEquals("Not correct container found, namespace was ignored",
255                 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
256     }
257 }