Clean up revision formatting
[netconf.git] / restconf / sal-rest-connector / 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
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 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.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
28 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
38 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
41 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
42 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45
46 /**
47  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
48  *
49  *
50  *
51  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
52  *
53  *         Created: Mar 9, 2015
54  */
55 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
56     private final XmlNormalizedNodeBodyReader xmlBodyReader;
57     private static SchemaContext schemaContext;
58
59     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
60
61     private static QNameModule initializeInstanceIdentifierModule() {
62         try {
63             return QNameModule.create(URI.create("instance:identifier:module"),
64                 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
65         } catch (final ParseException e) {
66             throw new Error(e);
67         }
68     }
69
70
71     public TestXmlBodyReaderMountPoint() throws NoSuchFieldException,
72             SecurityException {
73         super();
74         this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
75     }
76
77     @Override
78     protected MediaType getMediaType() {
79         return new MediaType(MediaType.APPLICATION_XML, null);
80     }
81
82     @BeforeClass
83     public static void initialization() throws Exception {
84         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
85         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
86         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
87
88         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
89         when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
90         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
91         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
92                 .thenReturn(Optional.of(mountInstance));
93
94         ControllerContext.getInstance().setMountService(mockMountService);
95         controllerContext.setSchemas(schemaContext);
96     }
97
98     @Test
99     public void moduleDataTest() throws Exception {
100         final DataSchemaNode dataSchemaNode = schemaContext
101                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
102         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
103         mockBodyReader(uri, this.xmlBodyReader, false);
104         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
105                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
106         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
107                 null, null, this.mediaType, null, inputStream);
108         checkMountPointNormalizedNodeContext(returnValue);
109         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
110     }
111
112     @Test
113     public void moduleSubContainerDataPutTest() throws Exception {
114         final DataSchemaNode dataSchemaNode = schemaContext
115                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
116         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
117         mockBodyReader(uri, this.xmlBodyReader, false);
118         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
119                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
120         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
121                 null, null, this.mediaType, null, inputStream);
122         checkMountPointNormalizedNodeContext(returnValue);
123         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
124                 QName.create(dataSchemaNode.getQName(), "cont1"));
125     }
126
127     @Test
128     public void moduleSubContainerDataPostTest() throws Exception {
129         final DataSchemaNode dataSchemaNode = schemaContext
130                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
131         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
132         mockBodyReader(uri, this.xmlBodyReader, true);
133         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
134                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
135         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
136                 null, null, this.mediaType, null, inputStream);
137         checkMountPointNormalizedNodeContext(returnValue);
138         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
139     }
140
141     @Test
142     public void rpcModuleInputTest() throws Exception {
143         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
144         mockBodyReader(uri, this.xmlBodyReader, true);
145         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
146                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
147         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
148                 null, null, this.mediaType, null, inputStream);
149         checkNormalizedNodeContext(returnValue);
150         final ContainerNode contNode = (ContainerNode) returnValue.getData();
151         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
152         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
153                 .getLastPathArgument());
154         assertTrue(contDataNodePotential.isPresent());
155         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
156         final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
157         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
158                 .getLastPathArgument());
159         assertTrue(leafDataNode.isPresent());
160         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
161     }
162
163     private void checkExpectValueNormalizeNodeContext(
164             final DataSchemaNode dataSchemaNode,
165             final NormalizedNodeContext nnContext) {
166         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
167     }
168
169     protected void checkExpectValueNormalizeNodeContext(
170             final DataSchemaNode dataSchemaNode,
171             final NormalizedNodeContext nnContext, final QName qName) {
172         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
173                 .of(dataSchemaNode.getQName());
174         final DOMMountPoint mountPoint = nnContext
175                 .getInstanceIdentifierContext().getMountPoint();
176         final DataSchemaNode mountDataSchemaNode = mountPoint
177                 .getSchemaContext().getDataChildByName(
178                         dataSchemaNode.getQName());
179         assertNotNull(mountDataSchemaNode);
180         if ((qName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
181             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
182                     .getDataChildByName(qName);
183             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
184                     .node(child.getQName()).build();
185             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
186                     .equals(child));
187         } else {
188             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
189         }
190         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
191                 dataNodeIdent));
192     }
193
194     /**
195      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
196      * used to distinguish between them to find correct one. Check if container was found not only according to its name
197      * but also by correct namespace used in payload.
198      */
199     @Test
200     public void findFooContainerUsingNamespaceTest() throws Exception {
201         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
202         final InputStream inputStream = TestXmlBodyReader.class
203                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
204         final NormalizedNodeContext returnValue = this.xmlBodyReader
205                 .readFrom(null, null, null, this.mediaType, null, inputStream);
206
207         // check return value
208         checkMountPointNormalizedNodeContext(returnValue);
209         // check if container was found both according to its name and namespace
210         assertEquals("Not correct container found, name was ignored",
211                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
212         assertEquals("Not correct container found, namespace was ignored",
213                 "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
214     }
215
216     /**
217      * Test when container with the same name is placed in two modules (foo-module and bar-module). Namespace must be
218      * used to distinguish between them to find correct one. Check if container was found not only according to its name
219      * but also by correct namespace used in payload.
220      */
221     @Test
222     public void findBarContainerUsingNamespaceTest() throws Exception {
223         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
224         final InputStream inputStream = TestXmlBodyReader.class
225                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
226         final NormalizedNodeContext returnValue = this.xmlBodyReader
227                 .readFrom(null, null, null, this.mediaType, null, inputStream);
228
229         // check return value
230         checkMountPointNormalizedNodeContext(returnValue);
231         // check if container was found both according to its name and namespace
232         assertEquals("Not correct container found, name was ignored",
233                 "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
234         assertEquals("Not correct container found, namespace was ignored",
235                 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
236     }
237 }