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