2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
15 import java.io.FileNotFoundException;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.common.XMLNamespace;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
26 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
27 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 public class JsonBodyReaderTest extends AbstractBodyReaderTest {
33 private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
34 XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
35 private static final MediaType MEDIA_TYPE = new MediaType(MediaType.APPLICATION_JSON, null);
37 private static EffectiveModelContext schemaContext;
39 private final JsonNormalizedNodeBodyReader jsonBodyReader;
41 public JsonBodyReaderTest() {
43 jsonBodyReader = new JsonNormalizedNodeBodyReader(databindProvider, mountPointService);
47 public static void initialization() throws FileNotFoundException {
48 final var testFiles = loadFiles("/instanceidentifier/yang");
49 testFiles.addAll(loadFiles("/modules"));
50 schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
54 public void moduleSubContainerDataPostTest() throws Exception {
55 final DataSchemaNode dataSchemaNode = schemaContext
56 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
57 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
58 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
59 final String uri = "instance-identifier-module:cont";
60 mockPostBodyReader(uri, jsonBodyReader);
61 final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
62 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_sub_container.json"));
63 checkNormalizedNodePayload(payload);
64 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
68 public void moduleSubContainerDataPostActionTest() throws Exception {
69 final DataSchemaNode dataSchemaNode = schemaContext
70 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
71 final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
72 final QName actionQName = QName.create(dataSchemaNode.getQName(), "reset");
73 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
74 .node(cont1QName).node(actionQName);
75 final String uri = "instance-identifier-module:cont/cont1/reset";
76 mockPostBodyReader(uri, jsonBodyReader);
77 final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
78 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_cont_action.json"));
79 checkNormalizedNodePayload(payload);
80 assertThat(payload.getInstanceIdentifierContext().getSchemaNode(), instanceOf(ActionDefinition.class));
84 public void moduleSubContainerAugmentDataPostTest() throws Exception {
85 final DataSchemaNode dataSchemaNode = schemaContext
86 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
87 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
88 final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
89 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName(), contAugmentQName);
90 final String uri = "instance-identifier-module:cont";
91 mockPostBodyReader(uri, jsonBodyReader);
92 final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
93 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_container.json"));
94 checkNormalizedNodePayload(payload);
95 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
99 public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
100 final DataSchemaNode dataSchemaNode = schemaContext
101 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
102 final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
103 final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
104 final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
105 final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
106 final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
107 .node(augmentChoice1QName).node(augmentChoice2QName).node(containerQName);
108 final String uri = "instance-identifier-module:cont";
109 mockPostBodyReader(uri, jsonBodyReader);
110 final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
111 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json"));
112 checkNormalizedNodePayload(payload);
113 checkExpectValueNormalizeNodeContext(dataSchemaNode, payload, dataII);
116 private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
117 final NormalizedNodePayload nnContext, final YangInstanceIdentifier dataNodeIdent) {
118 assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
119 assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
120 assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));