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