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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
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.netconf.sal.rest.impl.JsonToPatchBodyReader;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.common.patch.PatchContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 public class TestJsonPatchBodyReader extends AbstractBodyReaderTest {
26 private final JsonToPatchBodyReader jsonToPatchBodyReader;
27 private static SchemaContext schemaContext;
29 public TestJsonPatchBodyReader() {
30 super(schemaContext, null);
31 jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
35 protected MediaType getMediaType() {
36 return new MediaType(APPLICATION_JSON, null);
40 public static void initialization() {
41 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
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
50 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
52 final PatchContext returnValue = jsonToPatchBodyReader
53 .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 = TestJsonBodyReader.class
66 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
68 final PatchContext returnValue = jsonToPatchBodyReader
69 .readFrom(null, null, null, mediaType, null, inputStream);
70 checkPatchContext(returnValue);
74 * Test trying to use Patch create operation which requires value without value. Test should fail with
75 * {@link RestconfDocumentedException} with error code 400.
78 public void modulePatchValueMissingNegativeTest() throws Exception {
79 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
80 mockBodyReader(uri, jsonToPatchBodyReader, false);
82 final InputStream inputStream = TestJsonBodyReader.class
83 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
86 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
87 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
88 } catch (final RestconfDocumentedException e) {
89 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
94 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
95 * {@link RestconfDocumentedException} with error code 400.
98 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
99 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
100 mockBodyReader(uri, jsonToPatchBodyReader, false);
102 final InputStream inputStream = TestJsonBodyReader.class
103 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
106 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
107 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
108 } catch (final RestconfDocumentedException e) {
109 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
114 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
117 public void modulePatchCompleteTargetInURITest() throws Exception {
118 final String uri = "instance-identifier-patch-module:patch-cont";
119 mockBodyReader(uri, jsonToPatchBodyReader, false);
121 final InputStream inputStream = TestJsonBodyReader.class
122 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
124 final PatchContext returnValue = jsonToPatchBodyReader
125 .readFrom(null, null, null, mediaType, null, inputStream);
126 checkPatchContext(returnValue);
130 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
133 public void modulePatchMergeOperationOnListTest() throws Exception {
134 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
135 mockBodyReader(uri, jsonToPatchBodyReader, false);
137 final InputStream inputStream = TestJsonBodyReader.class
138 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
140 final PatchContext returnValue = jsonToPatchBodyReader
141 .readFrom(null, null, null, mediaType, null, inputStream);
142 checkPatchContext(returnValue);
146 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
149 public void modulePatchMergeOperationOnContainerTest() throws Exception {
150 final String uri = "instance-identifier-patch-module:patch-cont";
151 mockBodyReader(uri, jsonToPatchBodyReader, false);
153 final InputStream inputStream = TestJsonBodyReader.class
154 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
156 final PatchContext returnValue = jsonToPatchBodyReader
157 .readFrom(null, null, null, mediaType, null, inputStream);
158 checkPatchContext(returnValue);
162 * Test reading simple leaf value.
165 public void modulePatchSimpleLeafValueTest() throws Exception {
166 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
167 mockBodyReader(uri, jsonToPatchBodyReader, false);
169 final InputStream inputStream = TestJsonBodyReader.class
170 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
172 final PatchContext returnValue = jsonToPatchBodyReader
173 .readFrom(null, null, null, mediaType, null, inputStream);
174 checkPatchContext(returnValue);