36ce29b893bc30bea5ba93ee769908156687826f
[controller.git] / opendaylight / md-sal / 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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.InputStream;
15
16 import javax.ws.rs.core.MediaType;
17
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
21 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31
32 import com.google.common.base.Optional;
33
34 /**
35  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
36  *
37  *
38  *
39  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
40  *
41  *         Created: Mar 11, 2015
42  */
43 public class TestJsonBodyReader extends AbstractBodyReaderTest {
44
45     private final JsonNormalizedNodeBodyReader jsonBodyReader;
46     private static SchemaContext schemaContext;
47
48     public TestJsonBodyReader() throws NoSuchFieldException, SecurityException {
49         super();
50         jsonBodyReader = new JsonNormalizedNodeBodyReader();
51     }
52
53     @Override
54     protected MediaType getMediaType() {
55         return new MediaType(MediaType.APPLICATION_XML, null);
56     }
57
58     @BeforeClass
59     public static void initialization() throws NoSuchFieldException,
60             SecurityException {
61         schemaContext = schemaContextLoader("/instanceidentifier/yang",
62                 schemaContext);
63         schemaContext = schemaContextLoader("/modules", schemaContext);
64         schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
65         controllerContext.setSchemas(schemaContext);
66     }
67
68     @Test
69     public void moduleDataTest() throws Exception {
70         final DataSchemaNode dataSchemaNode = schemaContext
71                 .getDataChildByName("cont");
72         final String uri = "instance-identifier-module:cont";
73         mockBodyReader(uri, jsonBodyReader, false);
74         final InputStream inputStream = TestJsonBodyReader.class
75                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
76         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
77                 null, null, mediaType, null, inputStream);
78         checkNormalizedNodeContext(returnValue);
79         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
80     }
81
82     @Test
83     public void moduleSubContainerDataPutTest() throws Exception {
84         final DataSchemaNode dataSchemaNode = schemaContext
85                 .getDataChildByName("cont");
86         final String uri = "instance-identifier-module:cont/cont1";
87         mockBodyReader(uri, jsonBodyReader, false);
88         final InputStream inputStream = TestJsonBodyReader.class
89                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
90         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
91                 null, null, mediaType, null, inputStream);
92         checkNormalizedNodeContext(returnValue);
93         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue,
94                 "cont1");
95     }
96
97     @Test
98     public void moduleSubContainerDataPostTest() throws Exception {
99         final DataSchemaNode dataSchemaNode = schemaContext
100                 .getDataChildByName("cont");
101         final String uri = "instance-identifier-module:cont";
102         mockBodyReader(uri, jsonBodyReader, true);
103         final InputStream inputStream = TestJsonBodyReader.class
104                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
105         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
106                 null, null, mediaType, null, inputStream);
107         checkNormalizedNodeContext(returnValue);
108         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
109     }
110
111     @Test
112     public void rpcModuleInputTest() throws Exception {
113         final String uri = "invoke-rpc-module:rpc-test";
114         mockBodyReader(uri, jsonBodyReader, true);
115         final InputStream inputStream = TestJsonBodyReader.class
116                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
117         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
118                 null, null, mediaType, null, inputStream);
119         checkNormalizedNodeContext(returnValue);
120         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
121         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
122                 .create(inputNode.getNodeType(), "cont"));
123         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
124                 .getChild(yangCont.getLastPathArgument());
125         assertTrue(contDataNode.isPresent());
126         assertTrue(contDataNode.get() instanceof ContainerNode);
127         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
128                 .create(inputNode.getNodeType(), "lf"));
129         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
130                 .get()).getChild(yangleaf.getLastPathArgument());
131         assertTrue(leafDataNode.isPresent());
132         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
133                 .toString()));
134     }
135
136     private void checkExpectValueNormalizeNodeContext(
137             final DataSchemaNode dataSchemaNode,
138             final NormalizedNodeContext nnContext) {
139         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
140     }
141
142     private void checkExpectValueNormalizeNodeContext(
143             final DataSchemaNode dataSchemaNode,
144             final NormalizedNodeContext nnContext, final String localQname) {
145         YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
146                 .of(dataSchemaNode.getQName());
147
148         if (localQname != null && dataSchemaNode instanceof DataNodeContainer) {
149             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode)
150                     .getDataChildByName(localQname);
151             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent)
152                     .node(child.getQName()).build();
153             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
154                     .equals(child));
155         } else {
156             assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode()
157                     .equals(dataSchemaNode));
158         }
159         assertTrue(nnContext.getInstanceIdentifierContext()
160                 .getInstanceIdentifier().equals(dataNodeIdent));
161         assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
162                 dataNodeIdent));
163     }
164 }