54f65d7e6785541b18941cbf25e82b6043cca995
[netconf.git] / restconf / restconf-nb-bierman02 / 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 com.google.common.collect.Sets;
16 import java.io.File;
17 import java.io.InputStream;
18 import java.util.Collection;
19 import java.util.Optional;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
25 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.common.QNameModule;
28 import org.opendaylight.yangtools.yang.common.Revision;
29 import org.opendaylight.yangtools.yang.common.XMLNamespace;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 public class TestJsonBodyReader extends AbstractBodyReaderTest {
41
42     private final JsonNormalizedNodeBodyReader jsonBodyReader;
43     private static EffectiveModelContext schemaContext;
44
45     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
46         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
47
48     public TestJsonBodyReader() {
49         super(schemaContext, null);
50         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
51     }
52
53     @Override
54     protected MediaType getMediaType() {
55         return new MediaType(MediaType.APPLICATION_XML, null);
56     }
57
58     @BeforeClass
59     public static void initialization()
60             throws Exception {
61         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
62         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
63         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
64     }
65
66     @Test
67     public void moduleDataTest() throws Exception {
68         final DataSchemaNode dataSchemaNode =
69                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
70         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
71         final String uri = "instance-identifier-module:cont";
72         mockBodyReader(uri, this.jsonBodyReader, false);
73         final InputStream inputStream = TestJsonBodyReader.class
74                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
75         final NormalizedNodeContext returnValue = this.jsonBodyReader
76                 .readFrom(null, null, null, this.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 =
84                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
85         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
86         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
87         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
88         final String uri = "instance-identifier-module:cont/cont1";
89         mockBodyReader(uri, this.jsonBodyReader, false);
90         final InputStream inputStream = TestJsonBodyReader.class
91                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
92         final NormalizedNodeContext returnValue = this.jsonBodyReader
93                 .readFrom(null, null, null, this.mediaType, null, inputStream);
94         checkNormalizedNodeContext(returnValue);
95         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
96     }
97
98     @Test
99     public void moduleSubContainerDataPostTest() throws Exception {
100         final DataSchemaNode dataSchemaNode =
101                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
102         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
103         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
104         final String uri = "instance-identifier-module:cont";
105         mockBodyReader(uri, this.jsonBodyReader, true);
106         final InputStream inputStream = TestJsonBodyReader.class
107                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
108         final NormalizedNodeContext returnValue = this.jsonBodyReader
109                 .readFrom(null, null, null, this.mediaType, null, inputStream);
110         checkNormalizedNodeContext(returnValue);
111         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
112     }
113
114     @Test
115     public void moduleSubContainerAugmentDataPostTest() throws Exception {
116         final DataSchemaNode dataSchemaNode =
117                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
118         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
119         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
120         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
121                 Sets.newHashSet(contAugmentQName));
122         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
123                 .node(augII).node(contAugmentQName);
124         final String uri = "instance-identifier-module:cont";
125         mockBodyReader(uri, this.jsonBodyReader, true);
126         final InputStream inputStream = TestXmlBodyReader.class
127                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
128         final NormalizedNodeContext returnValue = this.jsonBodyReader
129                 .readFrom(null, null, null, this.mediaType, null, inputStream);
130         checkNormalizedNodeContext(returnValue);
131         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
132     }
133
134     //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
135     //@Test
136     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
137         final DataSchemaNode dataSchemaNode =
138                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
139         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
140         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
141         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
142         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
143         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
144                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
145         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
146                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
147         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
148                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
149                 .node(containerQName);
150         final String uri = "instance-identifier-module:cont";
151         mockBodyReader(uri, this.jsonBodyReader, true);
152         final InputStream inputStream = TestXmlBodyReader.class
153                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
154         final NormalizedNodeContext returnValue = this.jsonBodyReader
155                 .readFrom(null, null, null, this.mediaType, null, inputStream);
156         checkNormalizedNodeContext(returnValue);
157         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
158     }
159
160     @Test
161     public void rpcModuleInputTest() throws Exception {
162         final String uri = "invoke-rpc-module:rpc-test";
163         mockBodyReader(uri, this.jsonBodyReader, true);
164         final InputStream inputStream = TestJsonBodyReader.class
165                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
166         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
167                 null, null, this.mediaType, null, inputStream);
168         checkNormalizedNodeContext(returnValue);
169         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
170         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
171             QName.create(inputNode.getIdentifier().getNodeType(), "cont"));
172         final Optional<DataContainerChild> contDataNode = inputNode.findChildByArg(yangCont.getLastPathArgument());
173         assertTrue(contDataNode.isPresent());
174         assertTrue(contDataNode.get() instanceof ContainerNode);
175         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(
176             QName.create(inputNode.getIdentifier().getNodeType(), "lf"));
177         final Optional<DataContainerChild> leafDataNode = ((ContainerNode) contDataNode.get())
178                 .findChildByArg(yangleaf.getLastPathArgument());
179         assertTrue(leafDataNode.isPresent());
180         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
181     }
182
183     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
184             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
185         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
186         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
187         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
188     }
189 }