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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
11 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
15 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.patch.PatchContext;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
22 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
27 private final JsonToPatchBodyReader jsonToPatchBodyReader;
28 private static SchemaContext schemaContext;
30 public JsonPatchBodyReaderTest() throws Exception {
32 jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
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
51 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
53 final PatchContext returnValue = jsonToPatchBodyReader
54 .readFrom(null, null, null, mediaType, null, inputStream);
55 checkPatchContext(returnValue);
59 * Test of successful Patch consisting of create and delete Patch operations.
62 public void modulePatchCreateAndDeleteTest() throws Exception {
63 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
64 mockBodyReader(uri, jsonToPatchBodyReader, false);
66 final InputStream inputStream = JsonBodyReaderTest.class
67 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
69 final PatchContext returnValue = jsonToPatchBodyReader
70 .readFrom(null, null, null, mediaType, null, inputStream);
71 checkPatchContext(returnValue);
75 * Test trying to use Patch create operation which requires value without value. Test should fail with
76 * {@link RestconfDocumentedException} with error code 400.
79 public void modulePatchValueMissingNegativeTest() throws Exception {
80 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
81 mockBodyReader(uri, jsonToPatchBodyReader, false);
83 final InputStream inputStream = JsonBodyReaderTest.class
84 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
87 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
88 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
89 } catch (final RestconfDocumentedException e) {
90 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
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
104 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
107 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
108 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
109 } catch (final RestconfDocumentedException e) {
110 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
115 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
118 public void modulePatchCompleteTargetInURITest() throws Exception {
119 final String uri = "instance-identifier-patch-module:patch-cont";
120 mockBodyReader(uri, jsonToPatchBodyReader, false);
122 final InputStream inputStream = JsonBodyReaderTest.class
123 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
125 final PatchContext returnValue = jsonToPatchBodyReader
126 .readFrom(null, null, null, mediaType, null, inputStream);
127 checkPatchContext(returnValue);
131 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
134 public void modulePatchMergeOperationOnListTest() throws Exception {
135 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
136 mockBodyReader(uri, jsonToPatchBodyReader, false);
138 final InputStream inputStream = JsonBodyReaderTest.class
139 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
141 final PatchContext returnValue = jsonToPatchBodyReader
142 .readFrom(null, null, null, mediaType, null, inputStream);
143 checkPatchContext(returnValue);
147 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
150 public void modulePatchMergeOperationOnContainerTest() throws Exception {
151 final String uri = "instance-identifier-patch-module:patch-cont";
152 mockBodyReader(uri, jsonToPatchBodyReader, false);
154 final InputStream inputStream = JsonBodyReaderTest.class
155 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
157 final PatchContext returnValue = jsonToPatchBodyReader
158 .readFrom(null, null, null, mediaType, null, inputStream);
159 checkPatchContext(returnValue);
163 * Test reading simple leaf value.
166 public void modulePatchSimpleLeafValueTest() throws Exception {
167 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
168 mockBodyReader(uri, jsonToPatchBodyReader, false);
170 final InputStream inputStream =
171 JsonBodyReaderTest.class
172 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
174 final PatchContext returnValue = jsonToPatchBodyReader
175 .readFrom(null, null, null, mediaType, null, inputStream);
176 checkPatchContext(returnValue);