Clean up revision formatting
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / jersey / providers / XmlBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 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.jersey.providers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
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.util.Collection;
22 import javax.ws.rs.core.MediaType;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
38
39 public class XmlBodyReaderTest extends AbstractBodyReaderTest {
40
41     private final XmlNormalizedNodeBodyReader xmlBodyReader;
42     private static SchemaContext schemaContext;
43     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
44
45     private static QNameModule initializeInstanceIdentifierModule() {
46         try {
47             return QNameModule.create(URI.create("instance:identifier:module"),
48                 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
49         } catch (final ParseException e) {
50             throw new Error(e);
51         }
52     }
53
54     public XmlBodyReaderTest() throws Exception {
55         super();
56         this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
57     }
58
59     @Override
60     protected MediaType getMediaType() {
61         return new MediaType(MediaType.APPLICATION_XML, null);
62     }
63
64     @BeforeClass
65     public static void initialization() throws Exception {
66         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
67         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
68         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
69         controllerContext.setSchemas(schemaContext);
70         when(mountPointServiceHandler.get()).thenReturn(mock(DOMMountPointService.class));
71     }
72
73     @Test
74     public void moduleDataTest() throws Exception {
75         final DataSchemaNode dataSchemaNode = schemaContext
76                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
77         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
78         final String uri = "instance-identifier-module:cont";
79         mockBodyReader(uri, this.xmlBodyReader, false);
80         final InputStream inputStream = XmlBodyReaderTest.class
81                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
82         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
83                 inputStream);
84         checkNormalizedNodeContext(returnValue);
85         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
86     }
87
88     @Test
89     public void moduleSubContainerDataPutTest() throws Exception {
90         final DataSchemaNode dataSchemaNode = schemaContext
91                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
92         final 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, this.xmlBodyReader, false);
97         final InputStream inputStream = XmlBodyReaderTest.class
98                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
99         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
100                 inputStream);
101         checkNormalizedNodeContext(returnValue);
102         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
103     }
104
105     @Test
106     public void moduleSubContainerDataPostTest() throws Exception {
107         final DataSchemaNode dataSchemaNode = schemaContext
108                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
109         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
110         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
111         final String uri = "instance-identifier-module:cont";
112         mockBodyReader(uri, this.xmlBodyReader, true);
113         final InputStream inputStream = XmlBodyReaderTest.class
114                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
115         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
116                 inputStream);
117         checkNormalizedNodeContext(returnValue);
118         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
119     }
120
121     @Test
122     public void moduleSubContainerAugmentDataPostTest() throws Exception {
123         final DataSchemaNode dataSchemaNode = schemaContext
124                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
125         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
126         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
127         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
128                 Sets.newHashSet(contAugmentQName));
129         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
130                 .node(contAugmentQName);
131         final String uri = "instance-identifier-module:cont";
132         mockBodyReader(uri, this.xmlBodyReader, true);
133         final InputStream inputStream = XmlBodyReaderTest.class
134                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml");
135         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
136                 inputStream);
137         checkNormalizedNodeContext(returnValue);
138         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
139     }
140
141     @Test
142     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
143         final DataSchemaNode dataSchemaNode = schemaContext
144                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
145         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
146         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
147         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
148         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
149         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
150                 Sets.newHashSet(augmentChoice1QName));
151         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
152                 Sets.newHashSet(augmentChoice2QName));
153         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
154                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
155         final String uri = "instance-identifier-module:cont";
156         mockBodyReader(uri, this.xmlBodyReader, true);
157         final InputStream inputStream = XmlBodyReaderTest.class
158                 .getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml");
159         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
160                 inputStream);
161         checkNormalizedNodeContext(returnValue);
162         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
163     }
164
165     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
166             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
167         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
168         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
169         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
170     }
171
172     /**
173      * Test when container with the same name is placed in two modules
174      * (foo-module and bar-module). Namespace must be used to distinguish
175      * between them to find correct one. Check if container was found not only
176      * according to its name but also by correct namespace used in payload.
177      */
178     @Test
179     public void findFooContainerUsingNamespaceTest() throws Exception {
180         mockBodyReader("", this.xmlBodyReader, true);
181         final InputStream inputStream = XmlBodyReaderTest.class
182                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml");
183         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
184                 inputStream);
185
186         // check return value
187         checkNormalizedNodeContext(returnValue);
188         // check if container was found both according to its name and namespace
189         assertEquals("Not correct container found, name was ignored", "foo-bar-container",
190                 returnValue.getData().getNodeType().getLocalName());
191         assertEquals("Not correct container found, namespace was ignored", "foo:module",
192                 returnValue.getData().getNodeType().getNamespace().toString());
193     }
194
195     /**
196      * Test when container with the same name is placed in two modules
197      * (foo-module and bar-module). Namespace must be used to distinguish
198      * between them to find correct one. Check if container was found not only
199      * according to its name but also by correct namespace used in payload.
200      */
201     @Test
202     public void findBarContainerUsingNamespaceTest() throws Exception {
203         mockBodyReader("", this.xmlBodyReader, true);
204         final InputStream inputStream = XmlBodyReaderTest.class
205                 .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
206         final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
207                 inputStream);
208
209         // check return value
210         checkNormalizedNodeContext(returnValue);
211         // check if container was found both according to its name and namespace
212         assertEquals("Not correct container found, name was ignored", "foo-bar-container",
213                 returnValue.getData().getNodeType().getLocalName());
214         assertEquals("Not correct container found, namespace was ignored", "bar:module",
215                 returnValue.getData().getNodeType().getNamespace().toString());
216     }
217 }