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 org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
13 import java.io.InputStream;
14 import javax.ws.rs.core.MediaType;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.netconf.sal.rest.impl.XmlToPatchBodyReader;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 public class TestXmlPatchBodyReader extends AbstractBodyReaderTest {
25 private final XmlToPatchBodyReader xmlToPatchBodyReader;
26 private static EffectiveModelContext schemaContext;
28 public TestXmlPatchBodyReader() {
29 super(schemaContext, null);
30 xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext);
34 protected MediaType getMediaType() {
35 return new MediaType(MediaType.APPLICATION_XML, null);
39 public static void initialization() throws NoSuchFieldException, SecurityException {
40 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
44 public void moduleDataTest() throws Exception {
45 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
46 mockBodyReader(uri, xmlToPatchBodyReader, false);
47 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
48 "/instanceidentifier/xml/xmlPATCHdata.xml");
49 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
50 checkPatchContext(returnValue);
54 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
57 public void moduleDataValueMissingNegativeTest() throws Exception {
58 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
59 mockBodyReader(uri, xmlToPatchBodyReader, false);
60 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
61 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
62 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
63 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
64 assertEquals(1, ex.getErrors().size());
65 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
69 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
73 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
74 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
75 mockBodyReader(uri, xmlToPatchBodyReader, false);
76 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
77 "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
79 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81 assertEquals(1, ex.getErrors().size());
82 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
86 * Test of Yang Patch with absolute target path.
89 public void moduleDataAbsoluteTargetPathTest() throws Exception {
90 final String uri = "";
91 mockBodyReader(uri, xmlToPatchBodyReader, false);
92 final InputStream inputStream = TestXmlBodyReader.class
93 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
94 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
95 checkPatchContext(returnValue);
99 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
102 public void modulePatchCompleteTargetInURITest() throws Exception {
103 final String uri = "instance-identifier-patch-module:patch-cont";
104 mockBodyReader(uri, xmlToPatchBodyReader, false);
105 final InputStream inputStream = TestXmlBodyReader.class
106 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
107 final PatchContext returnValue = xmlToPatchBodyReader
108 .readFrom(null, null, null, mediaType, null, inputStream);
109 checkPatchContext(returnValue);
113 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
116 public void moduleDataMergeOperationOnListTest() throws Exception {
117 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
118 mockBodyReader(uri, xmlToPatchBodyReader, false);
119 final InputStream inputStream = TestXmlBodyReader.class
120 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
121 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
122 checkPatchContext(returnValue);
126 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
129 public void moduleDataMergeOperationOnContainerTest() throws Exception {
130 final String uri = "instance-identifier-patch-module:patch-cont";
131 mockBodyReader(uri, xmlToPatchBodyReader, false);
132 final InputStream inputStream = TestXmlBodyReader.class
133 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
134 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
135 checkPatchContext(returnValue);