6b995bbee6dbf98281f2eec129611255db43c1e1
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchBodyReaderMountPointTest.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.Test;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.yangtools.yang.common.ErrorTag;
18
19 public class XmlPatchBodyReaderMountPointTest extends AbstractPatchBodyReaderTest {
20     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
21
22     private final XmlPatchBodyReader xmlToPatchBodyReader;
23
24     public XmlPatchBodyReaderMountPointTest() {
25         xmlToPatchBodyReader = new XmlPatchBodyReader(databindProvider, mountPointService);
26     }
27
28     @Override
29     protected MediaType getMediaType() {
30         return new MediaType(MediaType.APPLICATION_XML, null);
31     }
32
33     @Test
34     public void moduleDataTest() throws Exception {
35         mockBodyReader(MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1", xmlToPatchBodyReader,
36             false);
37         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
38             XmlPatchBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml")));
39     }
40
41     /**
42      * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
43      */
44     @Test
45     public void moduleDataValueMissingNegativeTest() throws Exception {
46         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
47         mockBodyReader(uri, xmlToPatchBodyReader, false);
48         final InputStream inputStream = XmlPatchBodyReaderMountPointTest.class.getResourceAsStream(
49             "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
50
51         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
52             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
53         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
54     }
55
56     /**
57      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
58      * returned.
59      */
60     @Test
61     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
62         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
63         mockBodyReader(uri, xmlToPatchBodyReader, false);
64         final InputStream inputStream = XmlPatchBodyReaderMountPointTest.class.getResourceAsStream(
65             "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
66
67         RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
68             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
69         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
70     }
71
72     /**
73      * Test of Yang Patch with absolute target path.
74      */
75     @Test
76     public void moduleDataAbsoluteTargetPathTest() throws Exception {
77         final String uri = MOUNT_POINT;
78         mockBodyReader(uri, xmlToPatchBodyReader, false);
79
80         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
81             XmlPatchBodyReaderMountPointTest.class.getResourceAsStream(
82                 "/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml")));
83     }
84
85     /**
86      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
87      */
88     @Test
89     public void modulePatchCompleteTargetInURITest() throws Exception {
90         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
91         mockBodyReader(uri, xmlToPatchBodyReader, false);
92
93         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
94             XmlPatchBodyReaderMountPointTest.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         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
104         mockBodyReader(uri, xmlToPatchBodyReader, false);
105
106         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
107             XmlPatchBodyReaderMountPointTest.class.getResourceAsStream(
108                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml")));
109     }
110
111     /**
112      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
113      */
114     @Test
115     public void moduleDataMergeOperationOnContainerTest() throws Exception {
116         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
117         mockBodyReader(uri, xmlToPatchBodyReader, false);
118
119         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
120             XmlPatchBodyReaderMountPointTest.class.getResourceAsStream(
121                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml")));
122     }
123 }