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.common.patch.PatchContext;
19 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
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 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
46 mockBodyReader(uri, xmlToPatchBodyReader, false);
48 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
49 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml"));
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 = XmlBodyReaderTest.class.getResourceAsStream(
61 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
63 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
64 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
65 assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
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 = XmlBodyReaderTest.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("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
86 * Test of Yang Patch with absolute target path.
89 public void moduleDataAbsoluteTargetPathTest() throws Exception {
90 final String uri = "";
91 mockBodyReader(uri, xmlToPatchBodyReader, false);
93 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
94 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml"));
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);
106 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
107 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml"));
108 checkPatchContext(returnValue);
112 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
115 public void moduleDataMergeOperationOnListTest() throws Exception {
116 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
117 mockBodyReader(uri, xmlToPatchBodyReader, false);
119 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
120 XmlBodyReaderTest.class.getResourceAsStream(
121 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml"));
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 PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
133 XmlBodyReaderTest.class.getResourceAsStream(
134 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml"));
135 checkPatchContext(returnValue);