2 * Copyright (c) 2015 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.controller.sal.rest.impl.test.providers;
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.netconf.sal.rest.impl.JsonToPatchBodyReader;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.patch.PatchContext;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 public class TestJsonPatchBodyReader extends AbstractBodyReaderTest {
25 private static EffectiveModelContext schemaContext;
27 private final JsonToPatchBodyReader jsonToPatchBodyReader;
29 public TestJsonPatchBodyReader() {
30 super(schemaContext, null);
31 jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
35 public static void initialization() {
36 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
40 protected MediaType getMediaType() {
41 return new MediaType(APPLICATION_JSON, null);
45 public void modulePatchDataTest() throws Exception {
46 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
47 mockBodyReader(uri, jsonToPatchBodyReader, false);
49 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
50 "/instanceidentifier/json/jsonPATCHdata.json");
52 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
53 checkPatchContext(returnValue);
57 * Test of successful Patch consisting of create and delete Patch operations.
60 public void modulePatchCreateAndDeleteTest() throws Exception {
61 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
62 mockBodyReader(uri, jsonToPatchBodyReader, false);
64 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
65 "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
67 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
68 checkPatchContext(returnValue);
72 * Test trying to use Patch create operation which requires value without value. Test should fail with
73 * {@link RestconfDocumentedException} with error code 400.
76 public void modulePatchValueMissingNegativeTest() throws Exception {
77 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
78 mockBodyReader(uri, jsonToPatchBodyReader, false);
80 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
81 "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
83 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
84 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
85 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
89 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
90 * {@link RestconfDocumentedException} with error code 400.
93 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
94 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
95 mockBodyReader(uri, jsonToPatchBodyReader, false);
97 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
98 "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
100 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
101 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
102 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
106 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
109 public void modulePatchCompleteTargetInURITest() throws Exception {
110 final String uri = "instance-identifier-patch-module:patch-cont";
111 mockBodyReader(uri, jsonToPatchBodyReader, false);
113 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
114 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
116 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
117 checkPatchContext(returnValue);
121 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
124 public void modulePatchMergeOperationOnListTest() throws Exception {
125 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
126 mockBodyReader(uri, jsonToPatchBodyReader, false);
128 final InputStream inputStream = TestJsonBodyReader.class
129 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
131 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
132 checkPatchContext(returnValue);
136 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
139 public void modulePatchMergeOperationOnContainerTest() throws Exception {
140 final String uri = "instance-identifier-patch-module:patch-cont";
141 mockBodyReader(uri, jsonToPatchBodyReader, false);
143 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
144 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
146 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
147 checkPatchContext(returnValue);
151 * Test reading simple leaf value.
154 public void modulePatchSimpleLeafValueTest() throws Exception {
155 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
156 mockBodyReader(uri, jsonToPatchBodyReader, false);
158 final InputStream inputStream = TestJsonBodyReader.class
159 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
161 final PatchContext returnValue = jsonToPatchBodyReader
162 .readFrom(null, null, null, mediaType, null, inputStream);
163 checkPatchContext(returnValue);