Sal-rest-connector unit tests switch to new yang parser.
[netconf.git] / opendaylight / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonBodyReader.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
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.InputStream;
18 import java.net.URI;
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.rest.common.TestRestconfUtils;
26 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
34 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40
41 import com.google.common.base.Optional;
42 import com.google.common.collect.Sets;
43
44 /**
45  * sal-rest-connector
46  * 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 TestJsonBodyReader extends AbstractBodyReaderTest {
55
56     private final JsonNormalizedNodeBodyReader jsonBodyReader;
57     private static SchemaContext schemaContext;
58
59     public TestJsonBodyReader () throws NoSuchFieldException, SecurityException {
60         super();
61         jsonBodyReader = new JsonNormalizedNodeBodyReader();
62     }
63
64     @Override
65     protected MediaType getMediaType() {
66         return new MediaType(MediaType.APPLICATION_XML, null);
67     }
68
69     @BeforeClass
70     public static void initialization() throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
71         Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
72         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
73         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
74         schemaContext = TestRestconfUtils.parseYangSources(testFiles);
75         controllerContext.setSchemas(schemaContext);
76     }
77
78     @Test
79     public void moduleDataTest() throws Exception {
80         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
81         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
82         final String uri = "instance-identifier-module:cont";
83         mockBodyReader(uri, jsonBodyReader, false);
84         final InputStream inputStream = TestJsonBodyReader.class
85                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
86         final NormalizedNodeContext returnValue = jsonBodyReader
87                 .readFrom(null, null, null, mediaType, null, inputStream);
88         checkNormalizedNodeContext(returnValue);
89         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
90     }
91
92     @Test
93     public void moduleSubContainerDataPutTest() throws Exception {
94         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
95         QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
96         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
97         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
98         final String uri = "instance-identifier-module:cont/cont1";
99         mockBodyReader(uri, jsonBodyReader, false);
100         final InputStream inputStream = TestJsonBodyReader.class
101                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
102         final NormalizedNodeContext returnValue = jsonBodyReader
103                 .readFrom(null, null, null, mediaType, null, inputStream);
104         checkNormalizedNodeContext(returnValue);
105         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
106     }
107
108     @Test
109     public void moduleSubContainerDataPostTest() throws Exception {
110         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
111         QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
112         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
113         final String uri = "instance-identifier-module:cont";
114         mockBodyReader(uri, jsonBodyReader, true);
115         final InputStream inputStream = TestJsonBodyReader.class
116                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
117         final NormalizedNodeContext returnValue = jsonBodyReader
118                 .readFrom(null, null, null, mediaType, null, inputStream);
119         checkNormalizedNodeContext(returnValue);
120         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
121     }
122
123     @Test
124     public void moduleSubContainerAugmentDataPostTest() throws Exception {
125         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
126         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
127         QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
128         YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
129                 Sets.newHashSet(contAugmentQName));
130         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
131                 .node(augII).node(contAugmentQName);
132         final String uri = "instance-identifier-module:cont";
133         mockBodyReader(uri, jsonBodyReader, true);
134         final InputStream inputStream = TestXmlBodyReader.class
135                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
136         final NormalizedNodeContext returnValue = jsonBodyReader
137                 .readFrom(null, null, null, mediaType, null, inputStream);
138         checkNormalizedNodeContext(returnValue);
139         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
140     }
141
142     //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
143     //@Test
144     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
145         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
146         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
147         QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
148         QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
149         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
150         YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
151                 Sets.newHashSet(augmentChoice1QName));
152         YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
153                 Sets.newHashSet(augmentChoice2QName));
154         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
155                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
156                 .node(containerQName);
157         final String uri = "instance-identifier-module:cont";
158         mockBodyReader(uri, jsonBodyReader, true);
159         final InputStream inputStream = TestXmlBodyReader.class
160                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
161         final NormalizedNodeContext returnValue = jsonBodyReader
162                 .readFrom(null, null, null, mediaType, null, inputStream);
163         checkNormalizedNodeContext(returnValue);
164         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
165     }
166
167     @Test
168     public void rpcModuleInputTest() throws Exception {
169         final String uri = "invoke-rpc-module:rpc-test";
170         mockBodyReader(uri, jsonBodyReader, true);
171         final InputStream inputStream = TestJsonBodyReader.class
172                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
173         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
174                 null, null, mediaType, null, inputStream);
175         checkNormalizedNodeContext(returnValue);
176         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
177         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
178                 .create(inputNode.getNodeType(), "cont"));
179         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
180                 .getChild(yangCont.getLastPathArgument());
181         assertTrue(contDataNode.isPresent());
182         assertTrue(contDataNode.get() instanceof ContainerNode);
183         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
184                 .create(inputNode.getNodeType(), "lf"));
185         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
186                 .get()).getChild(yangleaf.getLastPathArgument());
187         assertTrue(leafDataNode.isPresent());
188         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
189                 .toString()));
190     }
191
192     private void checkExpectValueNormalizeNodeContext(
193             final DataSchemaNode dataSchemaNode,
194             final NormalizedNodeContext nnContext) {
195         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
196     }
197
198     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
199                                                       final NormalizedNodeContext nnContext,
200                                                       final YangInstanceIdentifier dataNodeIdent) {
201         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
202         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
203         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
204     }
205 }