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