Bug 8533: Not possible to invoke RPC on mount points with new Restconf
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / jersey / providers / JsonPATCHBodyReaderMountPointTest.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 javax.ws.rs.core.MediaType.APPLICATION_JSON;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.base.Optional;
19 import java.io.InputStream;
20 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
25 import org.opendaylight.controller.sal.rest.impl.test.providers.TestJsonBodyReader;
26 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30
31 public class JsonPATCHBodyReaderMountPointTest extends AbstractBodyReaderTest {
32
33     private final JsonToPATCHBodyReader jsonPATCHBodyReader;
34     private static SchemaContext schemaContext;
35     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
36
37     public JsonPATCHBodyReaderMountPointTest() throws Exception {
38         super();
39         jsonPATCHBodyReader = new JsonToPATCHBodyReader();
40     }
41
42     @Override
43     protected MediaType getMediaType() {
44         return new MediaType(APPLICATION_JSON, null);
45     }
46
47     @BeforeClass
48     public static void initialization() {
49         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
50
51         final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
52         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
53
54         when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
55         when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
56         when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
57
58         CONTROLLER_CONTEXT.setSchemas(schemaContext);
59     }
60
61     @Test
62     public void modulePATCHDataTest() throws Exception {
63         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
64         mockBodyReader(uri, jsonPATCHBodyReader, false);
65
66         final InputStream inputStream = TestJsonBodyReader.class
67                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
68
69         final PATCHContext returnValue = jsonPATCHBodyReader
70                 .readFrom(null, null, null, mediaType, null, inputStream);
71         checkPATCHContextMountPoint(returnValue);
72     }
73
74     /**
75      * Test of successful PATCH consisting of create and delete PATCH operations.
76      */
77     @Test
78     public void modulePATCHCreateAndDeleteTest() throws Exception {
79         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
80         mockBodyReader(uri, jsonPATCHBodyReader, false);
81
82         final InputStream inputStream = TestJsonBodyReader.class
83                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
84
85         final PATCHContext returnValue = jsonPATCHBodyReader
86                 .readFrom(null, null, null, mediaType, null, inputStream);
87         checkPATCHContextMountPoint(returnValue);
88     }
89
90     /**
91      * Test trying to use PATCH create operation which requires value without value. Test should fail with
92      * {@link RestconfDocumentedException} with error code 400.
93      */
94     @Test
95     public void modulePATCHValueMissingNegativeTest() throws Exception {
96         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
97         mockBodyReader(uri, jsonPATCHBodyReader, false);
98
99         final InputStream inputStream = TestJsonBodyReader.class
100                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
101
102         try {
103             jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
104             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
105         } catch (final RestconfDocumentedException e) {
106             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
107         }
108     }
109
110     /**
111      * Test trying to use value with PATCH delete operation which does not support value. Test should fail with
112      * {@link RestconfDocumentedException} with error code 400.
113      */
114     @Test
115     public void modulePATCHValueNotSupportedNegativeTest() throws Exception {
116         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
117         mockBodyReader(uri, jsonPATCHBodyReader, false);
118
119         final InputStream inputStream = TestJsonBodyReader.class
120                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
121
122         try {
123             jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
124             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
125         } catch (final RestconfDocumentedException e) {
126             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
127         }
128     }
129
130     /**
131      * Test using PATCH when target is completely specified in request URI and thus target leaf contains only '/' sign.
132      */
133     @Test
134     public void modulePATCHCompleteTargetInURITest() throws Exception {
135         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
136         mockBodyReader(uri, jsonPATCHBodyReader, false);
137
138         final InputStream inputStream = TestJsonBodyReader.class
139                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
140
141         final PATCHContext returnValue = jsonPATCHBodyReader
142                 .readFrom(null, null, null, mediaType, null, inputStream);
143         checkPATCHContextMountPoint(returnValue);
144     }
145
146     /**
147      * Test of Yang PATCH merge operation on list. Test consists of two edit operations - replace and merge.
148      */
149     @Test
150     public void modulePATCHMergeOperationOnListTest() throws Exception {
151         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
152         mockBodyReader(uri, jsonPATCHBodyReader, false);
153
154         final InputStream inputStream = TestJsonBodyReader.class
155                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
156
157         final PATCHContext returnValue = jsonPATCHBodyReader
158                 .readFrom(null, null, null, mediaType, null, inputStream);
159         checkPATCHContextMountPoint(returnValue);
160     }
161
162     /**
163      * Test of Yang PATCH merge operation on container. Test consists of two edit operations - create and merge.
164      */
165     @Test
166     public void modulePATCHMergeOperationOnContainerTest() throws Exception {
167         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
168         mockBodyReader(uri, jsonPATCHBodyReader, false);
169
170         final InputStream inputStream = TestJsonBodyReader.class
171                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
172
173         final PATCHContext returnValue = jsonPATCHBodyReader
174                 .readFrom(null, null, null, mediaType, null, inputStream);
175         checkPATCHContextMountPoint(returnValue);
176     }
177
178     /**
179      * Test reading simple leaf value
180      */
181     @Test
182     public void modulePATCHSimpleLeafValueTest() throws Exception {
183         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
184         mockBodyReader(uri, jsonPATCHBodyReader, false);
185
186         final InputStream inputStream = TestJsonBodyReader.class
187                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
188
189         final PATCHContext returnValue = jsonPATCHBodyReader
190                 .readFrom(null, null, null, mediaType, null, inputStream);
191         checkPATCHContext(returnValue);
192     }
193 }