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.netconf.sal.restconf.impl.PATCHContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 public class TestJsonPATCHBodyReader extends AbstractBodyReaderTest {
26 private final JsonToPATCHBodyReader jsonPATCHBodyReader;
27 private static SchemaContext schemaContext;
29 public TestJsonPATCHBodyReader() throws NoSuchFieldException, SecurityException {
31 jsonPATCHBodyReader = new JsonToPATCHBodyReader();
35 protected MediaType getMediaType() {
36 return new MediaType(APPLICATION_JSON, null);
40 public static void initialization() {
41 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
42 controllerContext.setSchemas(schemaContext);
46 public void modulePATCHDataTest() throws Exception {
47 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
48 mockBodyReader(uri, jsonPATCHBodyReader, false);
50 final InputStream inputStream = TestJsonBodyReader.class
51 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
53 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
66 final InputStream inputStream = TestJsonBodyReader.class
67 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
69 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
83 final InputStream inputStream = TestJsonBodyReader.class
84 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
87 jsonPATCHBodyReader.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, jsonPATCHBodyReader, false);
103 final InputStream inputStream = TestJsonBodyReader.class
104 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
107 jsonPATCHBodyReader.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, jsonPATCHBodyReader, false);
122 final InputStream inputStream = TestJsonBodyReader.class
123 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
125 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
138 final InputStream inputStream = TestJsonBodyReader.class
139 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
141 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
154 final InputStream inputStream = TestJsonBodyReader.class
155 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
157 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
170 final InputStream inputStream = TestJsonBodyReader.class
171 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
173 final PATCHContext returnValue = jsonPATCHBodyReader
174 .readFrom(null, null, null, mediaType, null, inputStream);
175 checkPATCHContext(returnValue);