Merge "Reduce/enhance logging in AbstractLeader"
[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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import com.google.common.base.Optional;
14 import java.io.InputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
19 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 /**
31  * sal-rest-connector
32  * org.opendaylight.controller.sal.rest.impl.test.providers
33  *
34  *
35  *
36  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
37  *
38  * Created: Mar 7, 2015
39  */
40 public class TestXmlBodyReader extends AbstractBodyReaderTest {
41
42     private final XmlNormalizedNodeBodyReader xmlBodyReader;
43     private static SchemaContext schemaContext;
44
45     public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
46         super();
47         xmlBodyReader = new XmlNormalizedNodeBodyReader();
48     }
49
50     @Override
51     MediaType getMediaType() {
52         return new MediaType(MediaType.APPLICATION_XML, null);
53     }
54
55     @BeforeClass
56     public static void initialization() throws NoSuchFieldException, SecurityException {
57         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
58         schemaContext = schemaContextLoader("/modules", schemaContext);
59         schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
60         controllerContext.setSchemas(schemaContext);
61     }
62
63     @Test
64     public void moduleDataTest() throws Exception {
65         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
66         final String uri = "instance-identifier-module:cont";
67         mockBodyReader(uri, xmlBodyReader, false);
68         final InputStream inputStream = TestXmlBodyReader.class
69                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
70         final NormalizedNodeContext returnValue = xmlBodyReader
71                 .readFrom(null, null, null, mediaType, null, inputStream);
72         checkNormalizedNodeContext(returnValue);
73         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
74     }
75
76     @Test
77     public void moduleSubContainerDataPutTest() throws Exception {
78         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
79         final String uri = "instance-identifier-module:cont/cont1";
80         mockBodyReader(uri, xmlBodyReader, false);
81         final InputStream inputStream = TestXmlBodyReader.class
82                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
83         final NormalizedNodeContext returnValue = xmlBodyReader
84                 .readFrom(null, null, null, mediaType, null, inputStream);
85         checkNormalizedNodeContext(returnValue);
86         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, "cont1");
87     }
88
89     @Test
90     public void moduleSubContainerDataPostTest() throws Exception {
91         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
92         final String uri = "instance-identifier-module:cont";
93         mockBodyReader(uri, xmlBodyReader, true);
94         final InputStream inputStream = TestXmlBodyReader.class
95                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
96         final NormalizedNodeContext returnValue = xmlBodyReader
97                 .readFrom(null, null, null, mediaType, null, inputStream);
98         checkNormalizedNodeContext(returnValue);
99         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
100     }
101
102     @Test
103     public void rpcModuleInputTest() throws Exception {
104         final String uri = "invoke-rpc-module:rpc-test";
105         mockBodyReader(uri, xmlBodyReader, true);
106         final InputStream inputStream = TestXmlBodyReader.class
107                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
108         final NormalizedNodeContext returnValue = xmlBodyReader
109                 .readFrom(null, null, null, mediaType, null, inputStream);
110         checkNormalizedNodeContext(returnValue);
111         final ContainerNode contNode = (ContainerNode) returnValue.getData();
112         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "lf"));
113         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contNode.getChild(yangleaf.getLastPathArgument());
114         assertTrue(leafDataNode.isPresent());
115         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
116     }
117
118     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
119             final NormalizedNodeContext nnContext) {
120         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
121     }
122
123     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
124             final NormalizedNodeContext nnContext, final String localQname) {
125         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
126
127         if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
128             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(localQname);
129             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
130             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
131         } else {
132             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(dataSchemaNode));
133         }
134         assertTrue(nnContext.getInstanceIdentifierContext().getInstanceIdentifier().equals(dataNodeIdent));
135         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
136     }
137 }