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.InputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
27 private final JsonPatchBodyReader jsonToPatchBodyReader;
28 private static EffectiveModelContext schemaContext;
30 public JsonPatchBodyReaderTest() throws Exception {
32 jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
36 protected MediaType getMediaType() {
37 return new MediaType(APPLICATION_JSON, null);
41 public static void initialization() {
42 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
46 public void modulePatchDataTest() throws Exception {
47 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
48 mockBodyReader(uri, jsonToPatchBodyReader, false);
50 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
51 "/instanceidentifier/json/jsonPATCHdata.json");
53 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
54 checkPatchContext(returnValue);
58 * Test of successful Patch consisting of create and delete Patch operations.
61 public void modulePatchCreateAndDeleteTest() throws Exception {
62 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
63 mockBodyReader(uri, jsonToPatchBodyReader, false);
65 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
66 "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
68 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
69 checkPatchContext(returnValue);
73 * Test trying to use Patch create operation which requires value without value. Test should fail with
74 * {@link RestconfDocumentedException} with error code 400.
77 public void modulePatchValueMissingNegativeTest() throws Exception {
78 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
79 mockBodyReader(uri, jsonToPatchBodyReader, false);
81 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
82 "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
84 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
85 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
86 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
90 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
91 * {@link RestconfDocumentedException} with error code 400.
94 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
95 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
96 mockBodyReader(uri, jsonToPatchBodyReader, false);
98 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
99 "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
101 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
102 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
103 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
107 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
110 public void modulePatchCompleteTargetInURITest() throws Exception {
111 final String uri = "instance-identifier-patch-module:patch-cont";
112 mockBodyReader(uri, jsonToPatchBodyReader, false);
114 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
115 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
117 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
118 checkPatchContext(returnValue);
122 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
125 public void modulePatchMergeOperationOnListTest() throws Exception {
126 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
127 mockBodyReader(uri, jsonToPatchBodyReader, false);
129 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
130 "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
132 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
133 checkPatchContext(returnValue);
137 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
140 public void modulePatchMergeOperationOnContainerTest() throws Exception {
141 final String uri = "instance-identifier-patch-module:patch-cont";
142 mockBodyReader(uri, jsonToPatchBodyReader, false);
144 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
145 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
147 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
148 checkPatchContext(returnValue);
152 * Test reading simple leaf value.
155 public void modulePatchSimpleLeafValueTest() throws Exception {
156 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
157 mockBodyReader(uri, jsonToPatchBodyReader, false);
159 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
160 "/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
162 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
163 checkPatchContext(returnValue);
167 * Test of Yang Patch on the top-level container with empty URI for data root.
170 public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
171 final String uri = "";
172 mockBodyReader(uri, jsonToPatchBodyReader, false);
174 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
175 "/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json");
177 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
178 checkPatchContext(returnValue);