Sal-rest-connector unit tests switch to new yang parser.
[netconf.git] / opendaylight / 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 java.io.File;
16 import java.io.InputStream;
17 import java.net.URI;
18 import java.util.Collection;
19
20 import javax.ws.rs.core.MediaType;
21
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
26 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
30 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
33 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 import com.google.common.base.Optional;
39 import com.google.common.collect.Sets;
40
41 /**
42  * sal-rest-connector
43  * org.opendaylight.controller.sal.rest.impl.test.providers
44  *
45  *
46  *
47  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
48  *
49  * Created: Mar 7, 2015
50  */
51 public class TestXmlBodyReader extends AbstractBodyReaderTest {
52
53     private final XmlNormalizedNodeBodyReader xmlBodyReader;
54     private static SchemaContext schemaContext;
55
56     public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
57         super();
58         xmlBodyReader = new XmlNormalizedNodeBodyReader();
59     }
60
61     @Override
62     protected MediaType getMediaType() {
63         return new MediaType(MediaType.APPLICATION_XML, null);
64     }
65
66     @BeforeClass
67     public static void initialization() throws Exception {
68         Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
69         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
70         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
71         schemaContext = TestRestconfUtils.parseYangSources(testFiles);
72         controllerContext.setSchemas(schemaContext);
73     }
74
75     @Test
76     public void moduleDataTest() throws Exception {
77         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
78         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
79         final String uri = "instance-identifier-module:cont";
80         mockBodyReader(uri, xmlBodyReader, false);
81         final InputStream inputStream = TestXmlBodyReader.class
82                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
83         final NormalizedNodeContext returnValue = xmlBodyReader
84                 .readFrom(null, null, null, mediaType, null, inputStream);
85         checkNormalizedNodeContext(returnValue);
86         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
87     }
88
89     @Test
90     public void moduleSubContainerDataPutTest() throws Exception {
91         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
92         QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
93         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
94         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
95         final String uri = "instance-identifier-module:cont/cont1";
96         mockBodyReader(uri, xmlBodyReader, false);
97         final InputStream inputStream = TestXmlBodyReader.class
98                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
99         final NormalizedNodeContext returnValue = xmlBodyReader
100                 .readFrom(null, null, null, mediaType, null, inputStream);
101         checkNormalizedNodeContext(returnValue);
102         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
103     }
104
105     @Test
106     public void moduleSubContainerDataPostTest() throws Exception {
107         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
108         QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
109         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
110         final String uri = "instance-identifier-module:cont";
111         mockBodyReader(uri, xmlBodyReader, true);
112         final InputStream inputStream = TestXmlBodyReader.class
113                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
114         final NormalizedNodeContext returnValue = xmlBodyReader
115                 .readFrom(null, null, null, mediaType, null, inputStream);
116         checkNormalizedNodeContext(returnValue);
117         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
118     }
119
120     @Test
121     public void moduleSubContainerAugmentDataPostTest() throws Exception {
122         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
123         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
124         QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
125         YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
126                 Sets.newHashSet(contAugmentQName));
127         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
128                 .node(augII).node(contAugmentQName);
129         final String uri = "instance-identifier-module:cont";
130         mockBodyReader(uri, xmlBodyReader, true);
131         final InputStream inputStream = TestXmlBodyReader.class
132                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
133         final NormalizedNodeContext returnValue = xmlBodyReader
134                 .readFrom(null, null, null, mediaType, null, inputStream);
135         checkNormalizedNodeContext(returnValue);
136         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
137     }
138
139     @Test
140     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
141         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
142         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
143         QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
144         QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
145         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
146         YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
147                 Sets.newHashSet(augmentChoice1QName));
148         YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
149                 Sets.newHashSet(augmentChoice2QName));
150         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
151                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
152                 .node(containerQName);
153         final String uri = "instance-identifier-module:cont";
154         mockBodyReader(uri, xmlBodyReader, true);
155         final InputStream inputStream = TestXmlBodyReader.class
156                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
157         final NormalizedNodeContext returnValue = xmlBodyReader
158                 .readFrom(null, null, null, mediaType, null, inputStream);
159         checkNormalizedNodeContext(returnValue);
160         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
161     }
162
163     @Test
164     public void rpcModuleInputTest() throws Exception {
165         final String uri = "invoke-rpc-module:rpc-test";
166         mockBodyReader(uri, xmlBodyReader, true);
167         final InputStream inputStream = TestXmlBodyReader.class
168                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
169         final NormalizedNodeContext returnValue = xmlBodyReader
170                 .readFrom(null, null, null, mediaType, null, inputStream);
171         checkNormalizedNodeContext(returnValue);
172         final ContainerNode contNode = (ContainerNode) returnValue.getData();
173         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
174         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
175                 .getLastPathArgument());
176         assertTrue(contDataNodePotential.isPresent());
177         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
178         final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
179         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
180                 .getLastPathArgument());
181         assertTrue(leafDataNode.isPresent());
182         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
183     }
184
185     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
186             final NormalizedNodeContext nnContext) {
187         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
188     }
189
190     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
191                                                       final NormalizedNodeContext nnContext,
192                                                       final YangInstanceIdentifier dataNodeIdent) {
193         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
194         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
195         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
196     }
197 }