Remove opendaylight directory
[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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.io.File;
18 import java.io.InputStream;
19 import java.util.Collection;
20
21 import javax.ws.rs.core.MediaType;
22
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
37 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40
41 import com.google.common.base.Optional;
42
43 /**
44  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
45  *
46  *
47  *
48  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
49  *
50  *         Created: Mar 9, 2015
51  */
52 public class TestXmlBodyReaderMountPoint extends AbstractBodyReaderTest {
53
54     private final XmlNormalizedNodeBodyReader xmlBodyReader;
55     private static SchemaContext schemaContext;
56
57     public TestXmlBodyReaderMountPoint() throws NoSuchFieldException,
58             SecurityException {
59         super();
60         xmlBodyReader = new XmlNormalizedNodeBodyReader();
61     }
62
63     @Override
64     protected MediaType getMediaType() {
65         return new MediaType(MediaType.APPLICATION_XML, null);
66     }
67
68     @BeforeClass
69     public static void initialization() throws Exception {
70         Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
71         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
72         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
73         schemaContext = TestRestconfUtils.parseYangSources(testFiles);
74
75         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
76         when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
77         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
78         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
79                 .thenReturn(Optional.of(mountInstance));
80
81         ControllerContext.getInstance().setMountService(mockMountService);
82         controllerContext.setSchemas(schemaContext);
83     }
84
85     @Test
86     public void moduleDataTest() throws Exception {
87         final DataSchemaNode dataSchemaNode = schemaContext
88                 .getDataChildByName("cont");
89         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
90         mockBodyReader(uri, xmlBodyReader, false);
91         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
92                 .getResourceAsStream("/instanceidentifier/xml/xmldata.xml");
93         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
94                 null, null, mediaType, null, inputStream);
95         checkMountPointNormalizedNodeContext(returnValue);
96         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
97     }
98
99     @Test
100     public void moduleSubContainerDataPutTest() throws Exception {
101         final DataSchemaNode dataSchemaNode = schemaContext
102                 .getDataChildByName("cont");
103         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
104         mockBodyReader(uri, xmlBodyReader, false);
105         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
106                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
107         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
108                 null, null, mediaType, null, inputStream);
109         checkMountPointNormalizedNodeContext(returnValue);
110         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
111                 "cont1");
112     }
113
114     @Test
115     public void moduleSubContainerDataPostTest() throws Exception {
116         final DataSchemaNode dataSchemaNode = schemaContext
117                 .getDataChildByName("cont");
118         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
119         mockBodyReader(uri, xmlBodyReader, true);
120         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
121                 .getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml");
122         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
123                 null, null, mediaType, null, inputStream);
124         checkMountPointNormalizedNodeContext(returnValue);
125         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
126     }
127
128     @Test
129     public void rpcModuleInputTest() throws Exception {
130         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
131         mockBodyReader(uri, xmlBodyReader, true);
132         final InputStream inputStream = TestXmlBodyReaderMountPoint.class
133                 .getResourceAsStream("/invoke-rpc/xml/rpc-input.xml");
134         final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null,
135                 null, null, mediaType, null, inputStream);
136         checkNormalizedNodeContext(returnValue);
137         final ContainerNode contNode = (ContainerNode) returnValue.getData();
138         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
139         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode.getChild(yangCont
140                 .getLastPathArgument());
141         assertTrue(contDataNodePotential.isPresent());
142         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
143         final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
144         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(yangLeaf
145                 .getLastPathArgument());
146         assertTrue(leafDataNode.isPresent());
147         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
148     }
149
150     private void checkExpectValueNormalizeNodeContext(
151             final DataSchemaNode dataSchemaNode,
152             final NormalizedNodeContext nnContext) {
153         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
154     }
155
156     protected void checkExpectValueNormalizeNodeContext(
157             final DataSchemaNode dataSchemaNode,
158             final NormalizedNodeContext nnContext, final String localQname) {
159         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
160                 .of(dataSchemaNode.getQName());
161         final DOMMountPoint mountPoint = nnContext
162                 .getInstanceIdentifierContext().getMountPoint();
163         final DataSchemaNode mountDataSchemaNode = mountPoint
164                 .getSchemaContext().getDataChildByName(
165                         dataSchemaNode.getQName());
166         assertNotNull(mountDataSchemaNode);
167         if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
168             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
169                     .getDataChildByName(localQname);
170             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
171                     .node(child.getQName()).build();
172             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
173                     .equals(child));
174         } else {
175             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
176         }
177         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
178                 dataNodeIdent));
179     }
180 }