1c3050e33e09cd113030ab0cde2210570c9de643
[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
14 import com.google.common.collect.Sets;
15 import java.io.File;
16 import java.io.FileNotFoundException;
17 import java.io.InputStream;
18 import java.net.URI;
19 import java.util.Collection;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
25 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
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.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
31 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
38
39 public class JsonBodyReaderTest extends AbstractBodyReaderTest {
40
41     private final JsonNormalizedNodeBodyReader jsonBodyReader;
42     private static SchemaContext schemaContext;
43
44     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
45         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
46
47     public JsonBodyReaderTest() throws Exception {
48         super(schemaContext);
49         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(schemaContextHandler, mountPointServiceHandler);
50     }
51
52     @Override
53     protected MediaType getMediaType() {
54         return new MediaType(MediaType.APPLICATION_XML, null);
55     }
56
57     @BeforeClass
58     public static void initialization()
59             throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
60         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
61         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
62         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
63     }
64
65     @Test
66     public void moduleDataTest() throws Exception {
67         final DataSchemaNode dataSchemaNode = schemaContext
68                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
69         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
70         final String uri = "instance-identifier-module:cont";
71         mockBodyReader(uri, this.jsonBodyReader, false);
72         final InputStream inputStream = JsonBodyReaderTest.class
73                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
74         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
75                 inputStream);
76         checkNormalizedNodeContext(returnValue);
77         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, 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, this.jsonBodyReader, false);
89         final InputStream inputStream = JsonBodyReaderTest.class
90                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
91         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
92                 inputStream);
93         checkNormalizedNodeContext(returnValue);
94         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
95     }
96
97     @Test
98     public void moduleSubContainerDataPostTest() throws Exception {
99         final DataSchemaNode dataSchemaNode = schemaContext
100                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
101         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
102         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
103         final String uri = "instance-identifier-module:cont";
104         mockBodyReader(uri, this.jsonBodyReader, true);
105         final InputStream inputStream = JsonBodyReaderTest.class
106                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
107         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
108                 inputStream);
109         checkNormalizedNodeContext(returnValue);
110         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
111     }
112
113     @Test
114     public void moduleSubContainerAugmentDataPostTest() throws Exception {
115         final DataSchemaNode dataSchemaNode = schemaContext
116                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
117         final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
118         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
119         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
120                 Sets.newHashSet(contAugmentQName));
121         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
122                 .node(contAugmentQName);
123         final String uri = "instance-identifier-module:cont";
124         mockBodyReader(uri, this.jsonBodyReader, true);
125         final InputStream inputStream =
126                 XmlBodyReaderTest.class
127                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
128         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
129                 inputStream);
130         checkNormalizedNodeContext(returnValue);
131         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
132     }
133
134     @Test
135     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
136         final DataSchemaNode dataSchemaNode = schemaContext
137                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
138         final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
139         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
140         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
141         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
142         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
143                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
144         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
145                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
146         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
147                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
148         final String uri = "instance-identifier-module:cont";
149         mockBodyReader(uri, this.jsonBodyReader, true);
150         final InputStream inputStream = XmlBodyReaderTest.class
151                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
152         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
153                 inputStream);
154         checkNormalizedNodeContext(returnValue);
155         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
156     }
157
158     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
159             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
160         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
161         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
162         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
163     }
164 }