953880916aaae5aae6caf9c247a473e1991d99e8
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonBodyReader.java
1 /*
2  * Copyright (c) 2015 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.controller.sal.rest.impl.test.providers;
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.base.Optional;
16 import com.google.common.collect.Sets;
17 import java.io.File;
18 import java.io.InputStream;
19 import java.net.URI;
20 import java.text.ParseException;
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.rest.common.TestRestconfUtils;
26 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
27 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.QNameModule;
30 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
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.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41
42
43 public class TestJsonBodyReader extends AbstractBodyReaderTest {
44
45     private final JsonNormalizedNodeBodyReader jsonBodyReader;
46     private static SchemaContext schemaContext;
47
48     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
49
50     private static QNameModule initializeInstanceIdentifierModule() {
51         try {
52             return QNameModule.create(URI.create("instance:identifier:module"),
53                 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
54         } catch (final ParseException e) {
55             throw new Error(e);
56         }
57     }
58
59
60     public TestJsonBodyReader() throws NoSuchFieldException, SecurityException {
61         super();
62         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
63     }
64
65     @Override
66     protected MediaType getMediaType() {
67         return new MediaType(MediaType.APPLICATION_XML, null);
68     }
69
70     @BeforeClass
71     public static void initialization()
72             throws Exception {
73         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
74         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
75         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
76         CONTROLLER_CONTEXT.setSchemas(schemaContext);
77     }
78
79     @Test
80     public void moduleDataTest() throws Exception {
81         final DataSchemaNode dataSchemaNode =
82                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
83         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
84         final String uri = "instance-identifier-module:cont";
85         mockBodyReader(uri, this.jsonBodyReader, false);
86         final InputStream inputStream = TestJsonBodyReader.class
87                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
88         final NormalizedNodeContext returnValue = this.jsonBodyReader
89                 .readFrom(null, null, null, this.mediaType, null, inputStream);
90         checkNormalizedNodeContext(returnValue);
91         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
92     }
93
94     @Test
95     public void moduleSubContainerDataPutTest() throws Exception {
96         final DataSchemaNode dataSchemaNode =
97                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
98         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
99         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
100         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
101         final String uri = "instance-identifier-module:cont/cont1";
102         mockBodyReader(uri, this.jsonBodyReader, false);
103         final InputStream inputStream = TestJsonBodyReader.class
104                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
105         final NormalizedNodeContext returnValue = this.jsonBodyReader
106                 .readFrom(null, null, null, this.mediaType, null, inputStream);
107         checkNormalizedNodeContext(returnValue);
108         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
109     }
110
111     @Test
112     public void moduleSubContainerDataPostTest() throws Exception {
113         final DataSchemaNode dataSchemaNode =
114                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
115         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
116         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
117         final String uri = "instance-identifier-module:cont";
118         mockBodyReader(uri, this.jsonBodyReader, true);
119         final InputStream inputStream = TestJsonBodyReader.class
120                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
121         final NormalizedNodeContext returnValue = this.jsonBodyReader
122                 .readFrom(null, null, null, this.mediaType, null, inputStream);
123         checkNormalizedNodeContext(returnValue);
124         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
125     }
126
127     @Test
128     public void moduleSubContainerAugmentDataPostTest() throws Exception {
129         final DataSchemaNode dataSchemaNode =
130                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
131         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
132         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
133         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
134                 Sets.newHashSet(contAugmentQName));
135         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
136                 .node(augII).node(contAugmentQName);
137         final String uri = "instance-identifier-module:cont";
138         mockBodyReader(uri, this.jsonBodyReader, true);
139         final InputStream inputStream = TestXmlBodyReader.class
140                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
141         final NormalizedNodeContext returnValue = this.jsonBodyReader
142                 .readFrom(null, null, null, this.mediaType, null, inputStream);
143         checkNormalizedNodeContext(returnValue);
144         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
145     }
146
147     //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
148     //@Test
149     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
150         final DataSchemaNode dataSchemaNode =
151                 schemaContext.getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
152         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
153         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
154         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
155         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
156         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
157                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
158         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
159                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
160         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
161                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
162                 .node(containerQName);
163         final String uri = "instance-identifier-module:cont";
164         mockBodyReader(uri, this.jsonBodyReader, true);
165         final InputStream inputStream = TestXmlBodyReader.class
166                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
167         final NormalizedNodeContext returnValue = this.jsonBodyReader
168                 .readFrom(null, null, null, this.mediaType, null, inputStream);
169         checkNormalizedNodeContext(returnValue);
170         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
171     }
172
173     @Test
174     public void rpcModuleInputTest() throws Exception {
175         final String uri = "invoke-rpc-module:rpc-test";
176         mockBodyReader(uri, this.jsonBodyReader, true);
177         final InputStream inputStream = TestJsonBodyReader.class
178                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
179         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
180                 null, null, this.mediaType, null, inputStream);
181         checkNormalizedNodeContext(returnValue);
182         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
183         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
184                 .create(inputNode.getNodeType(), "cont"));
185         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
186                 .getChild(yangCont.getLastPathArgument());
187         assertTrue(contDataNode.isPresent());
188         assertTrue(contDataNode.get() instanceof ContainerNode);
189         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
190                 .create(inputNode.getNodeType(), "lf"));
191         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
192                 .get()).getChild(yangleaf.getLastPathArgument());
193         assertTrue(leafDataNode.isPresent());
194         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
195                 .toString()));
196     }
197
198     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
199             final NormalizedNodeContext nnContext) {
200         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
201     }
202
203     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
204             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
205         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
206         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
207         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
208     }
209 }