879445ab3f618ef49067b60ebf0f3ba136cb6e0f
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonBodyReaderMountPoint.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 com.google.common.base.Optional;
18 import java.io.File;
19 import java.io.InputStream;
20 import java.net.URI;
21 import java.text.ParseException;
22 import java.text.SimpleDateFormat;
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.JsonNormalizedNodeBodyReader;
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.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
40 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44
45 /**
46  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
47  *
48  *
49  *
50  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
51  *
52  *         Created: Mar 11, 2015
53  */
54 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
55
56     private final JsonNormalizedNodeBodyReader jsonBodyReader;
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                     new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
65         } catch (final ParseException e) {
66             throw new Error(e);
67         }
68     }
69
70     public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
71             SecurityException {
72         super();
73         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
74     }
75
76     @Override
77     protected MediaType getMediaType() {
78         return new MediaType(MediaType.APPLICATION_XML, null);
79     }
80
81     @BeforeClass
82     public static void initialization() throws Exception {
83         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
84         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
85         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
86
87         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
88         when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
89         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
90         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
91                 .thenReturn(Optional.of(mountInstance));
92
93         ControllerContext.getInstance().setMountService(mockMountService);
94         controllerContext.setSchemas(schemaContext);
95     }
96
97     @Test
98     public void moduleDataTest() throws Exception {
99         final DataSchemaNode dataSchemaNode = schemaContext
100                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
101         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
102         mockBodyReader(uri, this.jsonBodyReader, false);
103         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
104                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
105         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
106                 null, null, this.mediaType, null, inputStream);
107         checkMountPointNormalizedNodeContext(returnValue);
108         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
109     }
110
111     @Test
112     public void moduleSubContainerDataPutTest() throws Exception {
113         final DataSchemaNode dataSchemaNode = schemaContext
114                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
115         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
116         mockBodyReader(uri, this.jsonBodyReader, false);
117         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
118                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
119         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
120                 null, null, this.mediaType, null, inputStream);
121         checkMountPointNormalizedNodeContext(returnValue);
122         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
123                 QName.create(dataSchemaNode.getQName(), "cont1"));
124     }
125
126     @Test
127     public void moduleSubContainerDataPostTest() throws Exception {
128         final DataSchemaNode dataSchemaNode = schemaContext
129                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
130         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
131         mockBodyReader(uri, this.jsonBodyReader, true);
132         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
133                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
134         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
135                 null, null, this.mediaType, null, inputStream);
136         checkMountPointNormalizedNodeContext(returnValue);
137         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
138     }
139
140     @Test
141     public void rpcModuleInputTest() throws Exception {
142         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
143         mockBodyReader(uri, this.jsonBodyReader, true);
144         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
145                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
146         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
147                 null, null, this.mediaType, null, inputStream);
148         checkNormalizedNodeContext(returnValue);
149         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
150         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
151                 .create(inputNode.getNodeType(), "cont"));
152         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
153                 .getChild(yangCont.getLastPathArgument());
154         assertTrue(contDataNode.isPresent());
155         assertTrue(contDataNode.get() instanceof ContainerNode);
156         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
157                 .create(inputNode.getNodeType(), "lf"));
158         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
159                 .get()).getChild(yangleaf.getLastPathArgument());
160         assertTrue(leafDataNode.isPresent());
161         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
162                 .toString()));
163     }
164
165     private void checkExpectValueNormalizeNodeContext(
166             final DataSchemaNode dataSchemaNode,
167             final NormalizedNodeContext nnContext) {
168         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
169     }
170
171     protected void checkExpectValueNormalizeNodeContext(
172             final DataSchemaNode dataSchemaNode,
173             final NormalizedNodeContext nnContext, final QName qName) {
174         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
175                 .of(dataSchemaNode.getQName());
176         final DOMMountPoint mountPoint = nnContext
177                 .getInstanceIdentifierContext().getMountPoint();
178         final DataSchemaNode mountDataSchemaNode = mountPoint
179                 .getSchemaContext().getDataChildByName(
180                         dataSchemaNode.getQName());
181         assertNotNull(mountDataSchemaNode);
182         if ((qName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
183             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
184                     .getDataChildByName(qName);
185             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
186                     .node(child.getQName()).build();
187             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
188                     .equals(child));
189         } else {
190             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
191         }
192         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
193                 dataNodeIdent));
194     }
195 }