Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.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.FileNotFoundException;
18 import java.io.InputStream;
19 import java.util.Collection;
20 import java.util.Optional;
21 import javax.ws.rs.core.MediaType;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
25 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.QNameModule;
29 import org.opendaylight.yangtools.yang.common.Revision;
30 import org.opendaylight.yangtools.yang.common.XMLNamespace;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
33 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
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.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
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         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(schemaContextHandler, 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()
62             throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
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, this.jsonBodyReader, false);
75         final InputStream inputStream = JsonBodyReaderTest.class
76                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
77         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
78                 inputStream);
79         checkNormalizedNodeContext(returnValue);
80         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
81     }
82
83     @Test
84     public void moduleSubContainerDataPutTest() throws Exception {
85         final DataSchemaNode dataSchemaNode = schemaContext
86                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
87         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
88         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
89         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
90         final String uri = "instance-identifier-module:cont/cont1";
91         mockBodyReader(uri, this.jsonBodyReader, false);
92         final InputStream inputStream = JsonBodyReaderTest.class
93                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
94         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
95                 inputStream);
96         checkNormalizedNodeContext(returnValue);
97         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
98     }
99
100     @Test
101     public void moduleSubContainerDataPostTest() throws Exception {
102         final DataSchemaNode dataSchemaNode = schemaContext
103                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
104         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
105         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
106         final String uri = "instance-identifier-module:cont";
107         mockBodyReader(uri, this.jsonBodyReader, true);
108         final InputStream inputStream = JsonBodyReaderTest.class
109                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
110         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
111                 inputStream);
112         checkNormalizedNodeContext(returnValue);
113         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
114     }
115
116     @Test
117     public void moduleSubContainerDataPostActionTest() throws Exception {
118         final Optional<DataSchemaNode> dataSchemaNode = schemaContext
119             .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
120         final QName cont1QName = QName.create(dataSchemaNode.get().getQName(), "cont1");
121         final QName actionQName = QName.create(dataSchemaNode.get().getQName(), "reset");
122         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.get().getQName())
123             .node(cont1QName).node(actionQName);
124         final String uri = "instance-identifier-module:cont/cont1/reset";
125         mockBodyReader(uri, this.jsonBodyReader, true);
126         final InputStream inputStream = JsonBodyReaderTest.class
127             .getResourceAsStream("/instanceidentifier/json/json_cont_action.json");
128         final NormalizedNodeContext returnValue = this.jsonBodyReader
129             .readFrom(null, null, null, this.mediaType, null, inputStream);
130         checkNormalizedNodeContext(returnValue);
131         assertTrue(returnValue.getInstanceIdentifierContext().getSchemaNode() instanceof ActionDefinition);
132     }
133
134     @Test
135     public void moduleSubContainerAugmentDataPostTest() throws Exception {
136         final DataSchemaNode dataSchemaNode = schemaContext
137                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
138         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
139         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
140         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
141                 Sets.newHashSet(contAugmentQName));
142         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
143                 .node(contAugmentQName);
144         final String uri = "instance-identifier-module:cont";
145         mockBodyReader(uri, this.jsonBodyReader, true);
146         final InputStream inputStream =
147                 XmlBodyReaderTest.class
148                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
149         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
150                 inputStream);
151         checkNormalizedNodeContext(returnValue);
152         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
153     }
154
155     @Test
156     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
157         final DataSchemaNode dataSchemaNode = schemaContext
158                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
159         final Module augmentModule = schemaContext.findModules(XMLNamespace.of("augment:module")).iterator().next();
160         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
161         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
162         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
163         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
164                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
165         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
166                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
167         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
168                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
169         final String uri = "instance-identifier-module:cont";
170         mockBodyReader(uri, this.jsonBodyReader, true);
171         final InputStream inputStream = XmlBodyReaderTest.class
172                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
173         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
174                 inputStream);
175         checkNormalizedNodeContext(returnValue);
176         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
177     }
178
179     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
180             final NormalizedNodeContext 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 }