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