Fix YANG patch request for augmented element
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / JsonPatchBodyReaderTest.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 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
9
10 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.InputStream;
16 import java.nio.charset.StandardCharsets;
17 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.common.patch.PatchContext;
22 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
23 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
24 import org.opendaylight.yangtools.yang.common.ErrorTag;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29
30 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
31
32     private final JsonPatchBodyReader jsonToPatchBodyReader;
33     private static EffectiveModelContext schemaContext;
34
35     public JsonPatchBodyReaderTest() throws Exception {
36         super(schemaContext);
37         jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
38     }
39
40     @Override
41     protected MediaType getMediaType() {
42         return new MediaType(APPLICATION_JSON, null);
43     }
44
45     @BeforeClass
46     public static void initialization() {
47         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
48     }
49
50     @Test
51     public void modulePatchDataTest() throws Exception {
52         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
53         mockBodyReader(uri, jsonToPatchBodyReader, false);
54
55         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
56             "/instanceidentifier/json/jsonPATCHdata.json");
57
58         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
59         checkPatchContext(returnValue);
60     }
61
62     /**
63      * Test of successful Patch consisting of create and delete Patch operations.
64      */
65     @Test
66     public void modulePatchCreateAndDeleteTest() throws Exception {
67         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
68         mockBodyReader(uri, jsonToPatchBodyReader, false);
69
70         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
71             "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
72
73         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
74         checkPatchContext(returnValue);
75     }
76
77     /**
78      * Test trying to use Patch create operation which requires value without value. Test should fail with
79      * {@link RestconfDocumentedException} with error code 400.
80      */
81     @Test
82     public void modulePatchValueMissingNegativeTest() throws Exception {
83         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
84         mockBodyReader(uri, jsonToPatchBodyReader, false);
85
86         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
87             "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
88
89         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
90             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
91         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
92     }
93
94     /**
95      * Test trying to use value with Patch delete operation which does not support value. Test should fail with
96      * {@link RestconfDocumentedException} with error code 400.
97      */
98     @Test
99     public void modulePatchValueNotSupportedNegativeTest() throws Exception {
100         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
101         mockBodyReader(uri, jsonToPatchBodyReader, false);
102
103         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
104             "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
105
106         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
107             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
108         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
109     }
110
111     /**
112      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
113      */
114     @Test
115     public void modulePatchCompleteTargetInURITest() throws Exception {
116         final String uri = "instance-identifier-patch-module:patch-cont";
117         mockBodyReader(uri, jsonToPatchBodyReader, false);
118
119         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
120             "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
121
122         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
123         checkPatchContext(returnValue);
124     }
125
126     /**
127      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
128      */
129     @Test
130     public void modulePatchMergeOperationOnListTest() throws Exception {
131         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
132         mockBodyReader(uri, jsonToPatchBodyReader, false);
133
134         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
135             "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
136
137         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
138         checkPatchContext(returnValue);
139     }
140
141     /**
142      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
143      */
144     @Test
145     public void modulePatchMergeOperationOnContainerTest() throws Exception {
146         final String uri = "instance-identifier-patch-module:patch-cont";
147         mockBodyReader(uri, jsonToPatchBodyReader, false);
148
149         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
150             "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
151
152         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
153         checkPatchContext(returnValue);
154     }
155
156     /**
157      * Test reading simple leaf value.
158      */
159     @Test
160     public void modulePatchSimpleLeafValueTest() throws Exception {
161         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
162         mockBodyReader(uri, jsonToPatchBodyReader, false);
163
164         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
165             "/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
166
167         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
168         checkPatchContext(returnValue);
169     }
170
171     /**
172      * Test of Yang Patch on the top-level container with empty URI for data root.
173      */
174     @Test
175     public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
176         final String uri = "";
177         mockBodyReader(uri, jsonToPatchBodyReader, false);
178
179         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
180                 "/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json");
181
182         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
183         checkPatchContext(returnValue);
184     }
185
186     /**
187      * Test of Yang Patch on the top augmented element.
188      */
189     @Test
190     public void modulePatchTargetTopLevelAugmentedContainerTest() throws Exception {
191         mockBodyReader("", jsonToPatchBodyReader, false);
192         final var inputStream = new ByteArrayInputStream(("{\n"
193             + "    \"ietf-yang-patch:yang-patch\": {\n"
194             + "        \"patch-id\": \"test-patch\",\n"
195             + "        \"comment\": \"comment\",\n"
196             + "        \"edit\": [\n"
197             + "            {\n"
198             + "                \"edit-id\": \"edit1\",\n"
199             + "                \"operation\": \"replace\",\n"
200             + "                \"target\": \"/test-m:container-root/test-m:container-lvl1/test-m-aug:container-aug\",\n"
201             + "                \"value\": {\n"
202             + "                    \"container-aug\": {\n"
203             + "                        \"leaf-aug\": \"data\"\n"
204             + "                    }\n"
205             + "                }\n"
206             + "            }\n"
207             + "        ]\n"
208             + "    }\n"
209             + "}").getBytes(StandardCharsets.UTF_8));
210         final var expectedData = Builders.containerBuilder()
211                 .withNodeIdentifier(new NodeIdentifier(CONT_AUG_QNAME))
212                 .withChild(ImmutableNodes.leafNode(LEAF_AUG_QNAME, "data"))
213                 .build();
214         final var returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
215         checkPatchContext(returnValue);
216         final var data = returnValue.getData().get(0).getNode();
217         assertEquals(CONT_AUG_QNAME, data.getIdentifier().getNodeType());
218         assertEquals(expectedData, data);
219     }
220 }