import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import javax.ws.rs.core.MediaType;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.patch.PatchContext;
import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
@Test
public void moduleDataTest() throws Exception {
- final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
-
- final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
- XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml"));
- checkPatchContext(returnValue);
+ mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
+ checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
+ XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml")));
}
/**
*/
@Test
public void moduleDataValueMissingNegativeTest() throws Exception {
- final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
+ mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
"/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
-
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
() -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
- assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
+ assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
}
/**
*/
@Test
public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
- final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
+ mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
"/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
-
final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
() -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
- assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
+ assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
}
-
/**
* Test of Yang Patch with absolute target path.
*/
@Test
public void moduleDataAbsoluteTargetPathTest() throws Exception {
- final String uri = "";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
-
- final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
- XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml"));
- checkPatchContext(returnValue);
+ mockBodyReader("", xmlToPatchBodyReader, false);
+ checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
+ XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml")));
}
/**
*/
@Test
public void modulePatchCompleteTargetInURITest() throws Exception {
- final String uri = "instance-identifier-patch-module:patch-cont";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
-
- final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
- XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml"));
- checkPatchContext(returnValue);
+ mockBodyReader("instance-identifier-patch-module:patch-cont", xmlToPatchBodyReader, false);
+ checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
+ XmlBodyReaderTest.class.getResourceAsStream(
+ "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml")));
}
/**
*/
@Test
public void moduleDataMergeOperationOnListTest() throws Exception {
- final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
-
- final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
- XmlBodyReaderTest.class.getResourceAsStream(
- "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml"));
- checkPatchContext(returnValue);
+ mockBodyReader("instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader, false);
+ final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
+ "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
+ checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
}
/**
*/
@Test
public void moduleDataMergeOperationOnContainerTest() throws Exception {
- final String uri = "instance-identifier-patch-module:patch-cont";
- mockBodyReader(uri, xmlToPatchBodyReader, false);
- final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
- XmlBodyReaderTest.class.getResourceAsStream(
- "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml"));
+ mockBodyReader("instance-identifier-patch-module:patch-cont", xmlToPatchBodyReader, false);
+ final InputStream inputStream = XmlBodyReaderTest.class
+ .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
+ checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
+ }
+
+ /**
+ * Test of Yang Patch on the top-level container with empty URI for data root.
+ */
+ @Test
+ public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
+ mockBodyReader("", xmlToPatchBodyReader, false);
+ final InputStream inputStream = XmlBodyReaderTest.class
+ .getResourceAsStream("/instanceidentifier/xml/xmlPATCHTargetTopLevelContainerWithEmptyURI.xml");
+ checkPatchContext(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
+ }
+
+ /**
+ * Test of Yang Patch on the top augmented element.
+ */
+ @Test
+ public void moduleTargetTopLevelAugmentedContainerTest() throws Exception {
+ mockBodyReader("", xmlToPatchBodyReader, false);
+ final var inputStream = new ByteArrayInputStream((
+ "<yang-patch xmlns=\"urn:ietf:params:xml:ns:yang:ietf-yang-patch\">\n"
+ + " <patch-id>test-patch</patch-id>\n"
+ + " <comment>This test patch for augmented element</comment>\n"
+ + " <edit>\n"
+ + " <edit-id>edit1</edit-id>\n"
+ + " <operation>replace</operation>\n"
+ + " <target>/test-m:container-root/test-m:container-lvl1/test-m-aug:container-aug</target>\n"
+ + " <value>\n"
+ + " <container-aug xmlns=\"test-ns-aug\">\n"
+ + " <leaf-aug>data</leaf-aug>\n"
+ + " </container-aug>\n"
+ + " </value>\n"
+ + " </edit>\n"
+ + "</yang-patch>").getBytes(StandardCharsets.UTF_8));
+ final var expectedData = Builders.containerBuilder()
+ .withNodeIdentifier(new NodeIdentifier(CONT_AUG_QNAME))
+ .withChild(ImmutableNodes.leafNode(LEAF_AUG_QNAME, "data"))
+ .build();
+ final var returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
checkPatchContext(returnValue);
+ final var data = returnValue.getData().get(0).getNode();
+ assertEquals(CONT_AUG_QNAME, data.getIdentifier().getNodeType());
+ assertEquals(expectedData, data);
}
}