fe50c687e7fc3e8d3c1559cdb2d3f8e85b503ce7
[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 java.io.File;
16 import java.io.InputStream;
17 import java.util.Collection;
18 import java.util.Optional;
19 import java.util.Set;
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.netconf.sal.rest.impl.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.YangInstanceIdentifier.AugmentationIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40
41 public class TestJsonBodyReader extends AbstractBodyReaderTest {
42
43     private final JsonNormalizedNodeBodyReader jsonBodyReader;
44     private static EffectiveModelContext schemaContext;
45
46     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
47         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
48
49     public TestJsonBodyReader() {
50         super(schemaContext, null);
51         jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
52     }
53
54     @Override
55     protected MediaType getMediaType() {
56         return new MediaType(MediaType.APPLICATION_XML, null);
57     }
58
59     @BeforeClass
60     public static void initialization()
61             throws Exception {
62         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
63         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
64         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
65     }
66
67     @Test
68     public void moduleDataTest() throws Exception {
69         final DataSchemaNode dataSchemaNode =
70                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
71         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
72         final String uri = "instance-identifier-module:cont";
73         mockBodyReader(uri, jsonBodyReader, false);
74         final InputStream inputStream = TestJsonBodyReader.class
75                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
76         final NormalizedNodeContext returnValue = jsonBodyReader
77                 .readFrom(null, null, null, mediaType, null, inputStream);
78         checkNormalizedNodeContext(returnValue);
79         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
80     }
81
82     @Test
83     public void moduleSubContainerDataPutTest() throws Exception {
84         final DataSchemaNode dataSchemaNode =
85                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
86         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
87         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
88         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
89         final String uri = "instance-identifier-module:cont/cont1";
90         mockBodyReader(uri, jsonBodyReader, false);
91         final InputStream inputStream = TestJsonBodyReader.class
92                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
93         final NormalizedNodeContext returnValue = jsonBodyReader
94                 .readFrom(null, null, null, mediaType, null, inputStream);
95         checkNormalizedNodeContext(returnValue);
96         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
97     }
98
99     @Test
100     public void moduleSubContainerDataPostTest() throws Exception {
101         final DataSchemaNode dataSchemaNode =
102                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
103         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
104         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
105         final String uri = "instance-identifier-module:cont";
106         mockBodyReader(uri, jsonBodyReader, true);
107         final InputStream inputStream = TestJsonBodyReader.class
108                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
109         final NormalizedNodeContext returnValue = jsonBodyReader
110                 .readFrom(null, null, null, mediaType, null, inputStream);
111         checkNormalizedNodeContext(returnValue);
112         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
113     }
114
115     @Test
116     public void moduleSubContainerAugmentDataPostTest() throws Exception {
117         final DataSchemaNode dataSchemaNode =
118                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
119         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
120         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
121         final AugmentationIdentifier augII = new AugmentationIdentifier(Set.of(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, jsonBodyReader, true);
126         final InputStream inputStream = TestXmlBodyReader.class
127                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
128         final NormalizedNodeContext returnValue = jsonBodyReader
129                 .readFrom(null, null, null, 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 AugmentationIdentifier augChoice1II = new AugmentationIdentifier(Set.of(augmentChoice1QName));
144         final AugmentationIdentifier augChoice2II = new AugmentationIdentifier(Set.of(augmentChoice2QName));
145         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
146                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
147                 .node(containerQName);
148         final String uri = "instance-identifier-module:cont";
149         mockBodyReader(uri, jsonBodyReader, true);
150         final InputStream inputStream = TestXmlBodyReader.class
151                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
152         final NormalizedNodeContext returnValue = jsonBodyReader
153                 .readFrom(null, null, null, mediaType, null, inputStream);
154         checkNormalizedNodeContext(returnValue);
155         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
156     }
157
158     @Test
159     public void rpcModuleInputTest() throws Exception {
160         final String uri = "invoke-rpc-module:rpc-test";
161         mockBodyReader(uri, jsonBodyReader, true);
162         final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream("/invoke-rpc/json/rpc-input.json");
163         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null, null, null, mediaType, null,
164             inputStream);
165         checkNormalizedNodeContextRpc(returnValue);
166         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
167         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
168             QName.create(inputNode.getIdentifier().getNodeType(), "cont"));
169         final Optional<DataContainerChild> contDataNode = inputNode.findChildByArg(yangCont.getLastPathArgument());
170         assertTrue(contDataNode.isPresent());
171         assertTrue(contDataNode.get() instanceof ContainerNode);
172         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(
173             QName.create(inputNode.getIdentifier().getNodeType(), "lf"));
174         final Optional<DataContainerChild> leafDataNode = ((ContainerNode) contDataNode.get())
175                 .findChildByArg(yangleaf.getLastPathArgument());
176         assertTrue(leafDataNode.isPresent());
177         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
178     }
179
180     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
181             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
182         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
183         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
184         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
185     }
186 }