96e16d71ebbffa2df60f84ad5975d38d8acc6bef
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12
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;
22
23 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
24     private static EffectiveModelContext schemaContext;
25
26     private final XmlPatchBodyReader xmlToPatchBodyReader;
27
28     public XmlPatchBodyReaderTest() throws Exception {
29         super(schemaContext);
30         xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
31     }
32
33     @Override
34     protected MediaType getMediaType() {
35         return new MediaType(MediaType.APPLICATION_XML, null);
36     }
37
38     @BeforeClass
39     public static void initialization() {
40         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
41     }
42
43     @Test
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")));
48     }
49
50     /**
51      * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
52      */
53     @Test
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());
61     }
62
63     /**
64      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
65      * returned.
66      */
67     @Test
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());
75     }
76
77     /**
78      * Test of Yang Patch with absolute target path.
79      */
80     @Test
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")));
85     }
86
87     /**
88      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
89      */
90     @Test
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")));
96     }
97
98     /**
99      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
100      */
101     @Test
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));
107     }
108
109     /**
110      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
111      */
112     @Test
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));
118     }
119
120     /**
121      * Test of Yang Patch on the top-level container with empty URI for data root.
122      */
123     @Test
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));
129     }
130 }