0a437cb80da7fc4b28211b056abc21c13f23a003
[netconf.git] / restconf / restconf-nb-bierman02 / 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.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.dom.api.DOMMountPointService;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
29 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.QNameModule;
32 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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                 SimpleDateFormatUtil.getRevisionFormat().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         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
75         CONTROLLER_CONTEXT.setSchemas(schemaContext);
76         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
77     }
78
79     @Test
80     public void moduleDataTest() throws Exception {
81         final DataSchemaNode dataSchemaNode = schemaContext
82                 .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 = JsonBodyReaderTest.class
87                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
88         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
89                 inputStream);
90         checkNormalizedNodeContext(returnValue);
91         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
92     }
93
94     @Test
95     public void moduleSubContainerDataPutTest() throws Exception {
96         final DataSchemaNode dataSchemaNode = schemaContext
97                 .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 = JsonBodyReaderTest.class
104                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
105         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
106                 inputStream);
107         checkNormalizedNodeContext(returnValue);
108         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
109     }
110
111     @Test
112     public void moduleSubContainerDataPostTest() throws Exception {
113         final DataSchemaNode dataSchemaNode = schemaContext
114                 .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 = JsonBodyReaderTest.class
120                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
121         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
122                 inputStream);
123         checkNormalizedNodeContext(returnValue);
124         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
125     }
126
127     @Test
128     public void moduleSubContainerAugmentDataPostTest() throws Exception {
129         final DataSchemaNode dataSchemaNode = schemaContext
130                 .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()).node(augII)
136                 .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.readFrom(null, null, null, this.mediaType, null,
142                 inputStream);
143         checkNormalizedNodeContext(returnValue);
144         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
145     }
146
147     @Test
148     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
149         final DataSchemaNode dataSchemaNode = schemaContext
150                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
151         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
152         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
153         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
154         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
155         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II =
156                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice1QName));
157         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II =
158                 new YangInstanceIdentifier.AugmentationIdentifier(Sets.newHashSet(augmentChoice2QName));
159         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
160                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
161         final String uri = "instance-identifier-module:cont";
162         mockBodyReader(uri, this.jsonBodyReader, true);
163         final InputStream inputStream = TestXmlBodyReader.class
164                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
165         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
166                 inputStream);
167         checkNormalizedNodeContext(returnValue);
168         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
169     }
170
171     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
172             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
173         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
174         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
175         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
176     }
177 }