2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
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;
30 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
32 private final JsonPatchBodyReader jsonToPatchBodyReader;
33 private static EffectiveModelContext schemaContext;
35 public JsonPatchBodyReaderTest() throws Exception {
37 jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
41 protected MediaType getMediaType() {
42 return new MediaType(APPLICATION_JSON, null);
46 public static void initialization() {
47 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
51 public void modulePatchDataTest() throws Exception {
52 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
53 mockBodyReader(uri, jsonToPatchBodyReader, false);
55 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
56 "/instanceidentifier/json/jsonPATCHdata.json");
58 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
59 checkPatchContext(returnValue);
63 * Test of successful Patch consisting of create and delete Patch operations.
66 public void modulePatchCreateAndDeleteTest() throws Exception {
67 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
68 mockBodyReader(uri, jsonToPatchBodyReader, false);
70 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
71 "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
73 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
74 checkPatchContext(returnValue);
78 * Test trying to use Patch create operation which requires value without value. Test should fail with
79 * {@link RestconfDocumentedException} with error code 400.
82 public void modulePatchValueMissingNegativeTest() throws Exception {
83 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
84 mockBodyReader(uri, jsonToPatchBodyReader, false);
86 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
87 "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
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());
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.
99 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
100 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
101 mockBodyReader(uri, jsonToPatchBodyReader, false);
103 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
104 "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
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());
112 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
115 public void modulePatchCompleteTargetInURITest() throws Exception {
116 final String uri = "instance-identifier-patch-module:patch-cont";
117 mockBodyReader(uri, jsonToPatchBodyReader, false);
119 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
120 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
122 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
123 checkPatchContext(returnValue);
127 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
130 public void modulePatchMergeOperationOnListTest() throws Exception {
131 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
132 mockBodyReader(uri, jsonToPatchBodyReader, false);
134 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
135 "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
137 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
138 checkPatchContext(returnValue);
142 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
145 public void modulePatchMergeOperationOnContainerTest() throws Exception {
146 final String uri = "instance-identifier-patch-module:patch-cont";
147 mockBodyReader(uri, jsonToPatchBodyReader, false);
149 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
150 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
152 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
153 checkPatchContext(returnValue);
157 * Test reading simple leaf value.
160 public void modulePatchSimpleLeafValueTest() throws Exception {
161 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
162 mockBodyReader(uri, jsonToPatchBodyReader, false);
164 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
165 "/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
167 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
168 checkPatchContext(returnValue);
172 * Test of Yang Patch on the top-level container with empty URI for data root.
175 public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
176 final String uri = "";
177 mockBodyReader(uri, jsonToPatchBodyReader, false);
179 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
180 "/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json");
182 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
183 checkPatchContext(returnValue);
187 * Test of Yang Patch on the top augmented element.
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"
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"
202 + " \"container-aug\": {\n"
203 + " \"leaf-aug\": \"data\"\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"))
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);