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.ByteArrayInputStream;
14 import java.io.InputStream;
15 import java.nio.charset.StandardCharsets;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
29 private static EffectiveModelContext schemaContext;
31 private final XmlPatchBodyReader xmlToPatchBodyReader;
33 public XmlPatchBodyReaderTest() throws Exception {
35 xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
39 protected MediaType getMediaType() {
40 return new MediaType(MediaType.APPLICATION_XML, null);
44 public static void initialization() {
45 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
49 public void moduleDataTest() throws Exception {
50 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
51 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
52 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml")));
56 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
59 public void moduleDataValueMissingNegativeTest() throws Exception {
60 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
61 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
62 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
63 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
64 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
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 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
75 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
76 "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
77 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
78 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
79 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
83 * Test of Yang Patch with absolute target path.
86 public void moduleDataAbsoluteTargetPathTest() throws Exception {
87 mockBodyReader("", xmlToPatchBodyReader, false);
88 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
89 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml")));
93 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
96 public void modulePatchCompleteTargetInURITest() throws Exception {
97 mockBodyReader("instance-identifier-patch-module:patch-cont", xmlToPatchBodyReader, false);
98 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
99 XmlBodyReaderTest.class.getResourceAsStream(
100 "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml")));
104 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
107 public void moduleDataMergeOperationOnListTest() throws Exception {
108 mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
109 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
110 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
111 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
115 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
118 public void moduleDataMergeOperationOnContainerTest() throws Exception {
119 mockBodyReader("instance-identifier-patch-module:patch-cont", xmlToPatchBodyReader, false);
120 final InputStream inputStream = XmlBodyReaderTest.class
121 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
122 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
126 * Test of Yang Patch on the top-level container with empty URI for data root.
129 public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
130 mockBodyReader("", xmlToPatchBodyReader, false);
131 final InputStream inputStream = XmlBodyReaderTest.class
132 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml");
133 checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
137 * Test of Yang Patch on the top augmented element.
140 public void moduleTargetTopLevelAugmentedContainerTest() throws Exception {
141 mockBodyReader("", xmlToPatchBodyReader, false);
142 final var inputStream = new ByteArrayInputStream((
143 "<yang-patch xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-patch\">\n"
144 + " <patch-id>test-patch</patch-id>\n"
145 + " <comment>This test patch for augmented element</comment>\n"
147 + " <edit-id>edit1</edit-id>\n"
148 + " <operation>replace</operation>\n"
149 + " <target>/test-m:container-root/test-m:container-lvl1/test-m-aug:container-aug</target>\n"
151 + " <container-aug xmlns=\"test-ns-aug\">\n"
152 + " <leaf-aug>data</leaf-aug>\n"
153 + " </container-aug>\n"
156 + "</yang-patch>").getBytes(StandardCharsets.UTF_8));
157 final var expectedData = Builders.containerBuilder()
158 .withNodeIdentifier(new NodeIdentifier(CONT_AUG_QNAME))
159 .withChild(ImmutableNodes.leafNode(LEAF_AUG_QNAME, "data"))
161 final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
162 checkPatchContext(returnValue);
163 final var data = returnValue.getData().get(0).getNode();
164 assertEquals(CONT_AUG_QNAME, data.getIdentifier().getNodeType());
165 assertEquals(expectedData, data);