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.fail;
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.model.api.EffectiveModelContext;
23 public class TestJsonPatchBodyReader extends AbstractBodyReaderTest {
25 private final JsonToPatchBodyReader jsonToPatchBodyReader;
26 private static EffectiveModelContext schemaContext;
28 public TestJsonPatchBodyReader() {
29 super(schemaContext, null);
30 jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
34 protected MediaType getMediaType() {
35 return new MediaType(APPLICATION_JSON, null);
39 public static void initialization() {
40 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
44 public void modulePatchDataTest() throws Exception {
45 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
46 mockBodyReader(uri, jsonToPatchBodyReader, false);
48 final InputStream inputStream = TestJsonBodyReader.class
49 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
51 final PatchContext returnValue = jsonToPatchBodyReader
52 .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
65 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
67 final PatchContext returnValue = jsonToPatchBodyReader
68 .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 = TestJsonBodyReader.class
82 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
85 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
86 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
87 } catch (final RestconfDocumentedException e) {
88 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
93 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
94 * {@link RestconfDocumentedException} with error code 400.
97 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
98 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
99 mockBodyReader(uri, jsonToPatchBodyReader, false);
101 final InputStream inputStream = TestJsonBodyReader.class
102 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
105 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
106 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
107 } catch (final RestconfDocumentedException e) {
108 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
113 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
116 public void modulePatchCompleteTargetInURITest() throws Exception {
117 final String uri = "instance-identifier-patch-module:patch-cont";
118 mockBodyReader(uri, jsonToPatchBodyReader, false);
120 final InputStream inputStream = TestJsonBodyReader.class
121 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
123 final PatchContext returnValue = jsonToPatchBodyReader
124 .readFrom(null, null, null, mediaType, null, inputStream);
125 checkPatchContext(returnValue);
129 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
132 public void modulePatchMergeOperationOnListTest() throws Exception {
133 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
134 mockBodyReader(uri, jsonToPatchBodyReader, false);
136 final InputStream inputStream = TestJsonBodyReader.class
137 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
139 final PatchContext returnValue = jsonToPatchBodyReader
140 .readFrom(null, null, null, mediaType, null, inputStream);
141 checkPatchContext(returnValue);
145 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
148 public void modulePatchMergeOperationOnContainerTest() throws Exception {
149 final String uri = "instance-identifier-patch-module:patch-cont";
150 mockBodyReader(uri, jsonToPatchBodyReader, false);
152 final InputStream inputStream = TestJsonBodyReader.class
153 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
155 final PatchContext returnValue = jsonToPatchBodyReader
156 .readFrom(null, null, null, mediaType, null, inputStream);
157 checkPatchContext(returnValue);
161 * Test reading simple leaf value.
164 public void modulePatchSimpleLeafValueTest() throws Exception {
165 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
166 mockBodyReader(uri, jsonToPatchBodyReader, false);
168 final InputStream inputStream = TestJsonBodyReader.class
169 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
171 final PatchContext returnValue = jsonToPatchBodyReader
172 .readFrom(null, null, null, mediaType, null, inputStream);
173 checkPatchContext(returnValue);