2 * Copyright (c) 2016 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.restconf.nb.rfc8040.jersey.providers.patch;
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.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
19 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
24 private static EffectiveModelContext schemaContext;
26 private final XmlPatchBodyReader xmlToPatchBodyReader;
28 public XmlPatchBodyReaderTest() throws Exception {
30 xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
34 protected MediaType getMediaType() {
35 return new MediaType(MediaType.APPLICATION_XML, null);
39 public static void initialization() {
40 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
44 public void moduleDataTest() throws Exception {
45 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
46 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
47 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml")));
51 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
54 public void moduleDataValueMissingNegativeTest() throws Exception {
55 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
56 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
57 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
58 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
59 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
60 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
64 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
68 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
69 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
70 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
71 "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
72 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
73 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
74 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
78 * Test of Yang Patch with absolute target path.
81 public void moduleDataAbsoluteTargetPathTest() throws Exception {
82 mockBodyReader("", xmlToPatchBodyReader, false);
83 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
84 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml")));
88 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
91 public void modulePatchCompleteTargetInURITest() throws Exception {
92 mockBodyReader("instance-identifier-patch-module:patch-cont", xmlToPatchBodyReader, false);
93 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
94 XmlBodyReaderTest.class.getResourceAsStream(
95 "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml")));
99 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
102 public void moduleDataMergeOperationOnListTest() throws Exception {
103 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
104 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
105 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
106 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
110 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
113 public void moduleDataMergeOperationOnContainerTest() throws Exception {
114 mockBodyReader("instance-identifier-patch-module:patch-cont", xmlToPatchBodyReader, false);
115 final InputStream inputStream = XmlBodyReaderTest.class
116 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
117 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
121 * Test of Yang Patch on the top-level container with empty URI for data root.
124 public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
125 mockBodyReader("", xmlToPatchBodyReader, false);
126 final InputStream inputStream = XmlBodyReaderTest.class
127 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml");
128 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));