fe257dbd04e938d239c672f7272ae81b8ae5b8cc
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlBodyReaderMountPoint.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 package org.opendaylight.controller.sal.rest.impl.test.providers;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14
15 import java.io.File;
16 import java.io.InputStream;
17 import java.util.Collection;
18 import java.util.Optional;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.Assert;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
25 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
26 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError;
29 import org.opendaylight.yangtools.yang.common.ErrorTag;
30 import org.opendaylight.yangtools.yang.common.ErrorType;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.common.Revision;
34 import org.opendaylight.yangtools.yang.common.XMLNamespace;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
39 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
40 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
41 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
42 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43
44 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
45     private final XmlNormalizedNodeBodyReader xmlBodyReader;
46     private static EffectiveModelContext schemaContext;
47
48     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
49         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
50
51     public TestXmlBodyReaderMountPoint() {
52         super(schemaContext, mock(DOMMountPoint.class));
53         xmlBodyReader = new XmlNormalizedNodeBodyReader(controllerContext);
54     }
55
56     @Override
57     protected MediaType getMediaType() {
58         return new MediaType(MediaType.APPLICATION_XML, null);
59     }
60
61     @BeforeClass
62     public static void initialization() throws Exception {
63         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
64         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
65         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
66     }
67
68     @Test
69     public void moduleDataTest() throws Exception {
70         final DataSchemaNode dataSchemaNode = schemaContext
71                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
72         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
73         mockBodyReader(uri, xmlBodyReader, false);
74         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
75                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
76         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
77                 null, null, mediaType, null, inputStream);
78         checkMountPointNormalizedNodeContext(returnValue);
79         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
80     }
81
82     @Test
83     public void moduleSubContainerDataPutTest() throws Exception {
84         final DataSchemaNode dataSchemaNode = schemaContext
85                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
86         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
87         mockBodyReader(uri, xmlBodyReader, false);
88         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
89                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
90         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
91                 null, null, mediaType, null, inputStream);
92         checkMountPointNormalizedNodeContext(returnValue);
93         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
94                 QName.create(dataSchemaNode.getQName(), "cont1"));
95     }
96
97     @Test
98     public void moduleSubContainerDataPostTest() throws Exception {
99         final DataSchemaNode dataSchemaNode = schemaContext
100                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
101         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
102         mockBodyReader(uri, xmlBodyReader, true);
103         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
104                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
105         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
106                 null, null, mediaType, null, inputStream);
107         checkMountPointNormalizedNodeContext(returnValue);
108         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
109     }
110
111     @Test
112     public void rpcModuleInputTest() throws Exception {
113         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
114         mockBodyReader(uri, xmlBodyReader, true);
115         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
116                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
117         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
118                 null, null, mediaType, null, inputStream);
119         checkNormalizedNodeContextRpc(returnValue);
120         final ContainerNode contNode = (ContainerNode) returnValue.getData();
121         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
122                 QName.create(contNode.getIdentifier().getNodeType(), "cont"));
123         final Optional<DataContainerChild> contDataNodePotential =
124                 contNode.findChildByArg(yangCont.getLastPathArgument());
125         assertTrue(contDataNodePotential.isPresent());
126         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
127         final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(
128                 QName.create(contDataNode.getIdentifier().getNodeType(), "lf"));
129         final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(yangLeaf.getLastPathArgument());
130         assertTrue(leafDataNode.isPresent());
131         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
132     }
133
134     private void checkExpectValueNormalizeNodeContext(
135             final DataSchemaNode dataSchemaNode,
136             final NormalizedNodeContext nnContext) {
137         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
138     }
139
140     protected void checkExpectValueNormalizeNodeContext(
141             final DataSchemaNode dataSchemaNode, final NormalizedNodeContext nnContext, final QName qualifiedName) {
142         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
143         final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
144         final DataSchemaNode mountDataSchemaNode =
145                 modelContext(mountPoint).getDataChildByName(dataSchemaNode.getQName());
146         assertNotNull(mountDataSchemaNode);
147         if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
148             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
149             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
150             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
151         } else {
152             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
153         }
154         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
155     }
156
157     /**
158      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
159      * used to distinguish between them to find correct one. Check if container was found not only according to its name
160      * but also by correct namespace used in payload.
161      */
162     @Test
163     public void findFooContainerUsingNamespaceTest() throws Exception {
164         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
165         final InputStream inputStream = TestXmlBodyReader.class
166                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
167         final NormalizedNodeContext returnValue = xmlBodyReader
168                 .readFrom(null, null, null, mediaType, null, inputStream);
169
170         // check return value
171         checkMountPointNormalizedNodeContext(returnValue);
172         // check if container was found both according to its name and namespace
173         assertEquals("Not correct container found, name was ignored",
174                 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
175         assertEquals("Not correct container found, namespace was ignored",
176                 "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
177     }
178
179     /**
180      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
181      * used to distinguish between them to find correct one. Check if container was found not only according to its name
182      * but also by correct namespace used in payload.
183      */
184     @Test
185     public void findBarContainerUsingNamespaceTest() throws Exception {
186         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
187         final InputStream inputStream = TestXmlBodyReader.class
188                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
189         final NormalizedNodeContext returnValue = xmlBodyReader
190                 .readFrom(null, null, null, mediaType, null, inputStream);
191
192         // check return value
193         checkMountPointNormalizedNodeContext(returnValue);
194         // check if container was found both according to its name and namespace
195         assertEquals("Not correct container found, name was ignored",
196                 "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
197         assertEquals("Not correct container found, namespace was ignored",
198                 "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
199     }
200
201     /**
202      * Test PUT operation when message root element is not the same as the last element in request URI.
203      * PUT operation message should always start with schema node from URI otherwise exception should be
204      * thrown.
205      */
206     @Test
207     public void wrongRootElementTest() throws Exception {
208         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, false);
209         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
210                 "/instanceidentifier/xml/bug7933.xml");
211         try {
212             xmlBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
213             Assert.fail("Test should fail due to malformed PUT operation message");
214         } catch (final RestconfDocumentedException exception) {
215             final RestconfError restconfError = exception.getErrors().get(0);
216             assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
217             assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
218         }
219     }
220 }