Bug 3104 - Sal Rest Connector: Data already exists for path when adding
[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.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import com.google.common.base.Optional;
15 import com.google.common.collect.Sets;
16 import java.io.InputStream;
17 import java.net.URI;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
22 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33
34 /**
35  * sal-rest-connector
36  * org.opendaylight.controller.sal.rest.impl.test.providers
37  *
38  *
39  *
40  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41  *
42  * Created: Mar 11, 2015
43  */
44 public class TestJsonBodyReader extends AbstractBodyReaderTest {
45
46     private final JsonNormalizedNodeBodyReader jsonBodyReader;
47     private static SchemaContext schemaContext;
48
49     public TestJsonBodyReader () throws NoSuchFieldException, SecurityException {
50         super();
51         jsonBodyReader = new JsonNormalizedNodeBodyReader();
52     }
53
54     @Override
55     protected MediaType getMediaType() {
56         return new MediaType(MediaType.APPLICATION_XML, null);
57     }
58
59     @BeforeClass
60     public static void initialization() throws NoSuchFieldException, SecurityException {
61         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
62         schemaContext = schemaContextLoader("/modules", schemaContext);
63         schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
64         controllerContext.setSchemas(schemaContext);
65     }
66
67     @Test
68     public void moduleDataTest() throws Exception {
69         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
70         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
71         final String uri = "instance-identifier-module:cont";
72         mockBodyReader(uri, jsonBodyReader, false);
73         final InputStream inputStream = TestJsonBodyReader.class
74                 .getResourceAsStream("/instanceidentifier/json/jsondata.json");
75         final NormalizedNodeContext returnValue = jsonBodyReader
76                 .readFrom(null, null, null, mediaType, null, inputStream);
77         checkNormalizedNodeContext(returnValue);
78         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
79     }
80
81     @Test
82     public void moduleSubContainerDataPutTest() throws Exception {
83         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
84         QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
85         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
86         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
87         final String uri = "instance-identifier-module:cont/cont1";
88         mockBodyReader(uri, jsonBodyReader, false);
89         final InputStream inputStream = TestJsonBodyReader.class
90                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
91         final NormalizedNodeContext returnValue = jsonBodyReader
92                 .readFrom(null, null, null, mediaType, null, inputStream);
93         checkNormalizedNodeContext(returnValue);
94         checkExpectValueNormalizeNodeContext(dataSchemaNodeOnPath, returnValue, dataII);
95     }
96
97     @Test
98     public void moduleSubContainerDataPostTest() throws Exception {
99         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
100         QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
101         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
102         final String uri = "instance-identifier-module:cont";
103         mockBodyReader(uri, jsonBodyReader, true);
104         final InputStream inputStream = TestJsonBodyReader.class
105                 .getResourceAsStream("/instanceidentifier/json/json_sub_container.json");
106         final NormalizedNodeContext returnValue = jsonBodyReader
107                 .readFrom(null, null, null, mediaType, null, inputStream);
108         checkNormalizedNodeContext(returnValue);
109         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
110     }
111
112     @Test
113     public void moduleSubContainerAugmentDataPostTest() throws Exception {
114         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
115         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
116         QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
117         YangInstanceIdentifier.AugmentationIdentifier augII = new YangInstanceIdentifier.AugmentationIdentifier(
118                 Sets.newHashSet(contAugmentQName));
119         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
120                 .node(augII).node(contAugmentQName);
121         final String uri = "instance-identifier-module:cont";
122         mockBodyReader(uri, jsonBodyReader, true);
123         final InputStream inputStream = TestXmlBodyReader.class
124                 .getResourceAsStream("/instanceidentifier/json/json_augment_container.json");
125         final NormalizedNodeContext returnValue = jsonBodyReader
126                 .readFrom(null, null, null, mediaType, null, inputStream);
127         checkNormalizedNodeContext(returnValue);
128         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
129     }
130
131     //FIXME: Uncomment this when JsonParserStream works correctly with case augmentation with choice
132     //@Test
133     public void moduleSubContainerChoiceAugmentDataPostTest() throws Exception {
134         final DataSchemaNode dataSchemaNode = schemaContext.getDataChildByName("cont");
135         final Module augmentModule = schemaContext.findModuleByNamespace(new URI("augment:module")).iterator().next();
136         QName augmentChoice1QName = QName.create(augmentModule.getQNameModule(), "augment-choice1");
137         QName augmentChoice2QName = QName.create(augmentChoice1QName, "augment-choice2");
138         final QName containerQName = QName.create(augmentChoice1QName, "case-choice-case-container1");
139         YangInstanceIdentifier.AugmentationIdentifier augChoice1II = new YangInstanceIdentifier.AugmentationIdentifier(
140                 Sets.newHashSet(augmentChoice1QName));
141         YangInstanceIdentifier.AugmentationIdentifier augChoice2II = new YangInstanceIdentifier.AugmentationIdentifier(
142                 Sets.newHashSet(augmentChoice2QName));
143         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
144                 .node(augChoice1II).node(augmentChoice1QName).node(augChoice2II).node(augmentChoice2QName)
145                 .node(containerQName);
146         final String uri = "instance-identifier-module:cont";
147         mockBodyReader(uri, jsonBodyReader, true);
148         final InputStream inputStream = TestXmlBodyReader.class
149                 .getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json");
150         final NormalizedNodeContext returnValue = jsonBodyReader
151                 .readFrom(null, null, null, mediaType, null, inputStream);
152         checkNormalizedNodeContext(returnValue);
153         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue, dataII);
154     }
155
156     @Test
157     public void rpcModuleInputTest() throws Exception {
158         final String uri = "invoke-rpc-module:rpc-test";
159         mockBodyReader(uri, jsonBodyReader, true);
160         final InputStream inputStream = TestJsonBodyReader.class
161                 .getResourceAsStream("/invoke-rpc/json/rpc-input.json");
162         final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
163                 null, null, mediaType, null, inputStream);
164         checkNormalizedNodeContext(returnValue);
165         final ContainerNode inputNode = (ContainerNode) returnValue.getData();
166         final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName
167                 .create(inputNode.getNodeType(), "cont"));
168         final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNode = inputNode
169                 .getChild(yangCont.getLastPathArgument());
170         assertTrue(contDataNode.isPresent());
171         assertTrue(contDataNode.get() instanceof ContainerNode);
172         final YangInstanceIdentifier yangleaf = YangInstanceIdentifier.of(QName
173                 .create(inputNode.getNodeType(), "lf"));
174         final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = ((ContainerNode) contDataNode
175                 .get()).getChild(yangleaf.getLastPathArgument());
176         assertTrue(leafDataNode.isPresent());
177         assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue()
178                 .toString()));
179     }
180
181     private void checkExpectValueNormalizeNodeContext(
182             final DataSchemaNode dataSchemaNode,
183             final NormalizedNodeContext nnContext) {
184         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
185     }
186
187     private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
188                                                       final NormalizedNodeContext nnContext,
189                                                       final YangInstanceIdentifier dataNodeIdent) {
190         assertEquals(dataSchemaNode, nnContext.getInstanceIdentifierContext().getSchemaNode());
191         assertEquals(dataNodeIdent, nnContext.getInstanceIdentifierContext().getInstanceIdentifier());
192         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
193     }
194 }