Do not include duplicate models
[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
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 = initializeInstanceIdentifierModule();
48
49     private static QNameModule initializeInstanceIdentifierModule() {
50         try {
51             return QNameModule.create(URI.create("instance:identifier:module"),
52                     new SimpleDateFormat("yyyy-MM-dd").parse("2014-01-17"));
53         } catch (final ParseException e) {
54             throw new Error(e);
55         }
56     }
57
58     public JsonBodyReaderTest() throws Exception {
59         super();
60         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
61     }
62
63     @Override
64     protected MediaType getMediaType() {
65         return new MediaType(MediaType.APPLICATION_XML, null);
66     }
67
68     @BeforeClass
69     public static void initialization()
70             throws NoSuchFieldException, SecurityException, FileNotFoundException, SourceException, ReactorException {
71         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
72         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
73         schemaContext = TestRestconfUtils.parseYangSources(testFiles);
74         controllerContext.setSchemas(schemaContext);
75         when(mountPointServiceHandler.get()).thenReturn(mock(DOMMountPointService.class));
76     }
77
78     @Test
79     public void moduleDataTest() throws Exception {
80         final DataSchemaNode dataSchemaNode = schemaContext
81                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
82         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
83         final String uri = "instance-identifier-module:cont";
84         mockBodyReader(uri, this.jsonBodyReader, false);
85         final InputStream inputStream = JsonBodyReaderTest.class
86                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
87         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
88                 inputStream);
89         checkNormalizedNodeContext(returnValue);
90         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
91     }
92
93     @Test
94     public void moduleSubContainerDataPutTest() throws Exception {
95         final DataSchemaNode dataSchemaNode = schemaContext
96                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
97         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
98         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
99         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
100         final String uri = "instance-identifier-module:cont/cont1";
101         mockBodyReader(uri, this.jsonBodyReader, false);
102         final InputStream inputStream = JsonBodyReaderTest.class
103                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
104         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
105                 inputStream);
106         checkNormalizedNodeContext(returnValue);
107         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
108     }
109
110     @Test
111     public void moduleSubContainerDataPostTest() throws Exception {
112         final DataSchemaNode dataSchemaNode = schemaContext
113                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
114         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
115         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
116         final String uri = "instance-identifier-module:cont";
117         mockBodyReader(uri, this.jsonBodyReader, true);
118         final InputStream inputStream = JsonBodyReaderTest.class
119                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
120         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
121                 inputStream);
122         checkNormalizedNodeContext(returnValue);
123         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
124     }
125
126     @Test
127     public void moduleSubContainerAugmentDataPostTest() throws Exception {
128         final DataSchemaNode dataSchemaNode = schemaContext
129                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
130         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
131         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
132         final YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
133                 Sets.newHashSet(contAugmentQName));
134         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augII)
135                 .node(contAugmentQName);
136         final String uri = "instance-identifier-module:cont";
137         mockBodyReader(uri, this.jsonBodyReader, true);
138         final InputStream inputStream = TestXmlBodyReader.class
139                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
140         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
141                 inputStream);
142         checkNormalizedNodeContext(returnValue);
143         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
144     }
145
146     @Test
147     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
148         final DataSchemaNode dataSchemaNode = schemaContext
149                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
150         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
151         final QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
152         final QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
153         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
154         final YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
155                 Sets.newHashSet(augmentChoice1QName));
156         final YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
157                 Sets.newHashSet(augmentChoice2QName));
158         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(augChoice1II)
159                 .node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName).node(containerQName);
160         final String uri = "instance-identifier-module:cont";
161         mockBodyReader(uri, this.jsonBodyReader, true);
162         final InputStream inputStream = TestXmlBodyReader.class
163                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
164         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
165                 inputStream);
166         checkNormalizedNodeContext(returnValue);
167         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
168     }
169
170     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
171             final NormalizedNodeContext nnContext, final YangInstanceIdentifier dataNodeIdent) {
172         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
173         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
174         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
175     }
176 }