Add DatabindContext and its wiring
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / test / JsonBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.nb.rfc8040.jersey.providers.test;
10
11 import static org.hamcrest.CoreMatchers.instanceOf;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15
16 import com.google.common.collect.Sets;
17 import java.io.ByteArrayInputStream;
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.InputStream;
21 import java.nio.charset.StandardCharsets;
22 import java.util.Collection;
23 import java.util.Optional;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.Revision;
33 import org.opendaylight.yangtools.yang.common.XMLNamespace;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
36 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
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.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42
43 public class JsonBodyReaderTest extends AbstractBodyReaderTest {
44     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
45         XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
46
47     private static EffectiveModelContext schemaContext;
48
49     private final JsonNormalizedNodeBodyReader jsonBodyReader;
50
51     public JsonBodyReaderTest() throws Exception {
52         super(schemaContext);
53         jsonBodyReader = new JsonNormalizedNodeBodyReader(databindProvider, mountPointService);
54     }
55
56     @Override
57     protected MediaType getMediaType() {
58         return new MediaType(MediaType.APPLICATION_XML, null);
59     }
60
61     @BeforeClass
62     public static void initialization() throws FileNotFoundException {
63         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
64         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
65         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
66     }
67
68     @Test
69     public void moduleDataTest() throws Exception {
70         final DataSchemaNode dataSchemaNode = schemaContext
71                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
72         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
73         final String uri = "instance-identifier-module:cont";
74         mockBodyReader(uri, jsonBodyReader, false);
75         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, mediaType, null,
76             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsondata.json"));
77         checkNormalizedNodePayload(payload);
78         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
79     }
80
81     @Test
82     public void moduleSubContainerDataPutTest() throws Exception {
83         final DataSchemaNode dataSchemaNode = schemaContext
84                 .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, jsonBodyReader, false);
90         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, mediaType, null,
91             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_sub_container.json"));
92         checkNormalizedNodePayload(payload);
93         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, payload, dataII);
94     }
95
96     @Test
97     public void moduleSubContainerDataPostTest() throws Exception {
98         final DataSchemaNode dataSchemaNode = schemaContext
99                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
100         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
101         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
102         final String uri = "instance-identifier-module:cont";
103         mockBodyReader(uri, jsonBodyReader, true);
104         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, mediaType, null,
105             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_sub_container.json"));
106         checkNormalizedNodePayload(payload);
107         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
108     }
109
110     @Test
111     public void moduleSubContainerDataPostActionTest() throws Exception {
112         final Optional<DataSchemaNode> dataSchemaNode = schemaContext
113             .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
114         final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
115         final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
116         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
117             .node(cont1QName).node(actionQName);
118         final String uri = "instance-identifier-module:cont/cont1/reset";
119         mockBodyReader(uri, jsonBodyReader, true);
120         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, mediaType, null,
121             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_cont_action.json"));
122         checkNormalizedNodePayload(payload);
123         assertThat(payload.getInstanceIdentifierContext().getSchemaNode(), instanceOf(ActionDefinition.class));
124     }
125
126     @Test
127     public void moduleSubContainerAugmentDataPostTest() throws Exception {
128         final DataSchemaNode dataSchemaNode = schemaContext
129                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
130         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
131         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
132         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
133                 Sets.newHashSet(contAugmentQName));
134         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
135                 .node(contAugmentQName);
136         final String uri = "instance-identifier-module:cont";
137         mockBodyReader(uri, jsonBodyReader, true);
138         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, mediaType, null,
139             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_container.json"));
140         checkNormalizedNodePayload(payload);
141         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
142     }
143
144     @Test
145     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
146         final DataSchemaNode dataSchemaNode = schemaContext
147                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
148         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
149         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
150         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
151         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
152         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
153                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
154         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
155                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
156         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
157                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
158         final String uri = "instance-identifier-module:cont";
159         mockBodyReader(uri, jsonBodyReader, true);
160         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, mediaType, null,
161             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json"));
162         checkNormalizedNodePayload(payload);
163         checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
164     }
165
166     @Test
167     public void testRangeViolation() throws Exception {
168         mockBodyReader("netconf786:foo", jsonBodyReader, false);
169
170         final InputStream inputStream = new ByteArrayInputStream(("{\n"
171             + "  \"netconf786:foo\": {\n"
172             + "    \"bar\": 100\n"
173             + "  }\n"
174             + "}").getBytes(StandardCharsets.UTF_8));
175
176         assertRangeViolation(() -> jsonBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
177     }
178
179     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
180             final NormalizedNodePayload nnContext, final YangInstanceIdentifier dataNodeIdent) {
181         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
182         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
183         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
184     }
185 }