b372915132bfa326a1a7d5c42a69f66ad1f2e9e5
[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.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.collect.Sets;
17 import java.io.File;
18 import java.io.FileNotFoundException;
19 import java.io.InputStream;
20 import java.net.URI;
21 import java.util.Collection;
22 import javax.ws.rs.core.MediaType;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
27 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.JsonNormalizedNodeBodyReader;
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.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
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.Module;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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
44     private final JsonNormalizedNodeBodyReader jsonBodyReader;
45     private static SchemaContext schemaContext;
46
47     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = QNameModule.create(
48         URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
49
50     public JsonBodyReaderTest() throws Exception {
51         super(schemaContext);
52         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(schemaContextHandler);
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         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
67     }
68
69     @Test
70     public void moduleDataTest() throws Exception {
71         final DataSchemaNode dataSchemaNode = schemaContext
72                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
73         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
74         final String uri = "instance-identifier-module:cont";
75         mockBodyReader(uri, this.jsonBodyReader, false);
76         final InputStream inputStream = JsonBodyReaderTest.class
77                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
78         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
79                 inputStream);
80         checkNormalizedNodeContext(returnValue);
81         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
82     }
83
84     @Test
85     public void moduleSubContainerDataPutTest() throws Exception {
86         final DataSchemaNode dataSchemaNode = schemaContext
87                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
88         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
89         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
90         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
91         final String uri = "instance-identifier-module:cont/cont1";
92         mockBodyReader(uri, this.jsonBodyReader, false);
93         final InputStream inputStream = JsonBodyReaderTest.class
94                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
95         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
96                 inputStream);
97         checkNormalizedNodeContext(returnValue);
98         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
99     }
100
101     @Test
102     public void moduleSubContainerDataPostTest() throws Exception {
103         final DataSchemaNode dataSchemaNode = schemaContext
104                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
105         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
106         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
107         final String uri = "instance-identifier-module:cont";
108         mockBodyReader(uri, this.jsonBodyReader, true);
109         final InputStream inputStream = JsonBodyReaderTest.class
110                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
111         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
112                 inputStream);
113         checkNormalizedNodeContext(returnValue);
114         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
115     }
116
117     @Test
118     public void moduleSubContainerAugmentDataPostTest() throws Exception {
119         final DataSchemaNode dataSchemaNode = schemaContext
120                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
121         final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
122         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
123         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
124                 Sets.newHashSet(contAugmentQName));
125         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
126                 .node(contAugmentQName);
127         final String uri = "instance-identifier-module:cont";
128         mockBodyReader(uri, this.jsonBodyReader, true);
129         final InputStream inputStream =
130                 XmlBodyReaderTest.class
131                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
132         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
133                 inputStream);
134         checkNormalizedNodeContext(returnValue);
135         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
136     }
137
138     @Test
139     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
140         final DataSchemaNode dataSchemaNode = schemaContext
141                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
142         final Module augmentModule = schemaContext.findModules(new URI("augment:module")).iterator().next();
143         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
144         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
145         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
146         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
147                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
148         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
149                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
150         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
151                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
152         final String uri = "instance-identifier-module:cont";
153         mockBodyReader(uri, this.jsonBodyReader, true);
154         final InputStream inputStream = XmlBodyReaderTest.class
155                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
156         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
157                 inputStream);
158         checkNormalizedNodeContext(returnValue);
159         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
160     }
161
162     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
163             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
164         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
165         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
166         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
167     }
168 }