Bug 7433 - Remove use of YangSchemaSourceImpl from restconf tests
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / jersey / providers / 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.jersey.providers;
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.text.ParseException;
22 import java.text.SimpleDateFormat;
23 import java.util.Collection;
24 import javax.ws.rs.core.MediaType;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
30 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.common.QNameModule;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
35 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42
43 public class JsonBodyReaderTest 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                     new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
54         } catch (final ParseException e) {
55             throw new Error(e);
56         }
57     }
58
59     public JsonBodyReaderTest() throws Exception {
60         super();
61         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
62     }
63
64     @Override
65     protected MediaType getMediaType() {
66         return new MediaType(MediaType.APPLICATION_XML, null);
67     }
68
69     @BeforeClass
70     public static void initialization()
71             throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
72         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
73         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
74         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
75         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
76         controllerContext.setSchemas(schemaContext);
77         when(mountPointServiceHandler.get()).thenReturn(mock(DOMMountPointService.class));
78     }
79
80     @Test
81     public void moduleDataTest() throws Exception {
82         final DataSchemaNode dataSchemaNode = schemaContext
83                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
84         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
85         final String uri = "instance-identifier-module:cont";
86         mockBodyReader(uri, this.jsonBodyReader, false);
87         final InputStream inputStream = JsonBodyReaderTest.class
88                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
89         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
90                 inputStream);
91         checkNormalizedNodeContext(returnValue);
92         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
93     }
94
95     @Test
96     public void moduleSubContainerDataPutTest() throws Exception {
97         final DataSchemaNode dataSchemaNode = schemaContext
98                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
99         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
100         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
101         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
102         final String uri = "instance-identifier-module:cont/cont1";
103         mockBodyReader(uri, this.jsonBodyReader, false);
104         final InputStream inputStream = JsonBodyReaderTest.class
105                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
106         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
107                 inputStream);
108         checkNormalizedNodeContext(returnValue);
109         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
110     }
111
112     @Test
113     public void moduleSubContainerDataPostTest() throws Exception {
114         final DataSchemaNode dataSchemaNode = schemaContext
115                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
116         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
117         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
118         final String uri = "instance-identifier-module:cont";
119         mockBodyReader(uri, this.jsonBodyReader, true);
120         final InputStream inputStream = JsonBodyReaderTest.class
121                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
122         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
123                 inputStream);
124         checkNormalizedNodeContext(returnValue);
125         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
126     }
127
128     @Test
129     public void moduleSubContainerAugmentDataPostTest() throws Exception {
130         final DataSchemaNode dataSchemaNode = schemaContext
131                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
132         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
133         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
134         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
135                 Sets.newHashSet(contAugmentQName));
136         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
137                 .node(contAugmentQName);
138         final String uri = "instance-identifier-module:cont";
139         mockBodyReader(uri, this.jsonBodyReader, true);
140         final InputStream inputStream = TestXmlBodyReader.class
141                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
142         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
143                 inputStream);
144         checkNormalizedNodeContext(returnValue);
145         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
146     }
147
148     @Test
149     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
150         final DataSchemaNode dataSchemaNode = schemaContext
151                 .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 = new YangInstanceIdentifier.AugmentationIdentifier(
157                 Sets.newHashSet(augmentChoice1QName));
158         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
159                 Sets.newHashSet(augmentChoice2QName));
160         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
161                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
162         final String uri = "instance-identifier-module:cont";
163         mockBodyReader(uri, this.jsonBodyReader, true);
164         final InputStream inputStream = TestXmlBodyReader.class
165                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
166         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
167                 inputStream);
168         checkNormalizedNodeContext(returnValue);
169         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
170     }
171
172     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
173             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
174         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
175         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
176         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
177     }
178 }