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