Remove explicit default super-constructor calls
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonBodyReaderMountPoint.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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import java.io.File;
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.DOMMountPoint;
27 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.QNameModule;
34 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
37 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
38 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
40 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
41 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44
45 public class TestJsonBodyReaderMountPoint extends AbstractBodyReaderTest {
46
47     private final JsonNormalizedNodeBodyReader jsonBodyReader;
48     private static SchemaContext schemaContext;
49
50     private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
51
52     private static QNameModule initializeInstanceIdentifierModule() {
53         try {
54             return QNameModule.create(URI.create("instance:identifier:module"),
55                 SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
56         } catch (final ParseException e) {
57             throw new Error(e);
58         }
59     }
60
61     public TestJsonBodyReaderMountPoint() throws NoSuchFieldException,
62             SecurityException {
63         this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
64     }
65
66     @Override
67     protected MediaType getMediaType() {
68         return new MediaType(MediaType.APPLICATION_XML, null);
69     }
70
71     @BeforeClass
72     public static void initialization() throws Exception {
73         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
74         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
75         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
76
77         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
78         when(mountInstance.getSchemaContext()).thenReturn(schemaContext);
79         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
80         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class)))
81                 .thenReturn(Optional.of(mountInstance));
82
83         ControllerContext.getInstance().setMountService(mockMountService);
84         CONTROLLER_CONTEXT.setSchemas(schemaContext);
85     }
86
87     @Test
88     public void moduleDataTest() throws Exception {
89         final DataSchemaNode dataSchemaNode = schemaContext
90                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
91         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
92         mockBodyReader(uri, this.jsonBodyReader, false);
93         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
94                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
95         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
96                 null, null, this.mediaType, null, inputStream);
97         checkMountPointNormalizedNodeContext(returnValue);
98         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
99     }
100
101     @Test
102     public void moduleSubContainerDataPutTest() throws Exception {
103         final DataSchemaNode dataSchemaNode = schemaContext
104                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
105         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
106         mockBodyReader(uri, this.jsonBodyReader, false);
107         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
108                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
109         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
110                 null, null, this.mediaType, null, inputStream);
111         checkMountPointNormalizedNodeContext(returnValue);
112         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
113                 QName.create(dataSchemaNode.getQName(), "cont1"));
114     }
115
116     @Test
117     public void moduleSubContainerDataPostTest() throws Exception {
118         final DataSchemaNode dataSchemaNode = schemaContext
119                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
120         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
121         mockBodyReader(uri, this.jsonBodyReader, true);
122         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
123                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
124         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
125                 null, null, this.mediaType, null, inputStream);
126         checkMountPointNormalizedNodeContext(returnValue);
127         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
128     }
129
130     @Test
131     public void rpcModuleInputTest() throws Exception {
132         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
133         mockBodyReader(uri, this.jsonBodyReader, true);
134         final InputStream inputStream = TestJsonBodyReaderMountPoint.class
135                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
136         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
137                 null, null, this.mediaType, null, inputStream);
138         checkNormalizedNodeContext(returnValue);
139         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
140         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
141                 .create(inputNode.getNodeType(), "cont"));
142         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
143                 .getChild(yangCont.getLastPathArgument());
144         assertTrue(contDataNode.isPresent());
145         assertTrue(contDataNode.get() instanceof ContainerNode);
146         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
147                 .create(inputNode.getNodeType(), "lf"));
148         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
149                 .get()).getChild(yangleaf.getLastPathArgument());
150         assertTrue(leafDataNode.isPresent());
151         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
152                 .toString()));
153     }
154
155     private void checkExpectValueNormalizeNodeContext(
156             final DataSchemaNode dataSchemaNode,
157             final NormalizedNodeContext nnContext) {
158         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
159     }
160
161     protected void checkExpectValueNormalizeNodeContext(
162             final DataSchemaNode dataSchemaNode,
163             final NormalizedNodeContext nnContext, final QName qualifiedName) {
164         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
165                 .of(dataSchemaNode.getQName());
166         final DOMMountPoint mountPoint = nnContext
167                 .getInstanceIdentifierContext().getMountPoint();
168         final DataSchemaNode mountDataSchemaNode = mountPoint
169                 .getSchemaContext().getDataChildByName(
170                         dataSchemaNode.getQName());
171         assertNotNull(mountDataSchemaNode);
172         if ((qualifiedName != null) && (dataSchemaNode instanceof DataNodeContainer)) {
173             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
174                     .getDataChildByName(qualifiedName);
175             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
176                     .node(child.getQName()).build();
177             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
178                     .equals(child));
179         } else {
180             assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
181         }
182         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
183                 dataNodeIdent));
184     }
185 }