Fix patch target parsing
[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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13
14 import java.io.InputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
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.model.api.EffectiveModelContext;
23
24 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
25
26     private final XmlToPatchBodyReader xmlToPatchBodyReader;
27     private static EffectiveModelContext schemaContext;
28
29     public XmlPatchBodyReaderTest() throws Exception {
30         super(schemaContext);
31         xmlToPatchBodyReader = new XmlToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
32     }
33
34     @Override
35     protected MediaType getMediaType() {
36         return new MediaType(MediaType.APPLICATION_XML, null);
37     }
38
39     @BeforeClass
40     public static void initialization() {
41         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
42     }
43
44     @Test
45     public void moduleDataTest() throws Exception {
46         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
47         mockBodyReader(uri, xmlToPatchBodyReader, false);
48
49         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
50             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml"));
51         checkPatchContext(returnValue);
52     }
53
54     /**
55      * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
56      */
57     @Test
58     public void moduleDataValueMissingNegativeTest() throws Exception {
59         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
60         mockBodyReader(uri, xmlToPatchBodyReader, false);
61         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
62             "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
63
64         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
65             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
66         assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
67     }
68
69     /**
70      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
71      * returned.
72      */
73     @Test
74     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
75         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
76         mockBodyReader(uri, xmlToPatchBodyReader, false);
77         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
78             "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
79
80         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
81             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
82         assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
83     }
84
85
86     /**
87      * Test of Yang Patch with absolute target path.
88      */
89     @Test
90     public void moduleDataAbsoluteTargetPathTest() throws Exception {
91         final String uri = "";
92         mockBodyReader(uri, xmlToPatchBodyReader, false);
93
94         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
95             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml"));
96         checkPatchContext(returnValue);
97     }
98
99     /**
100      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
101      */
102     @Test
103     public void modulePatchCompleteTargetInURITest() throws Exception {
104         final String uri = "instance-identifier-patch-module:patch-cont";
105         mockBodyReader(uri, xmlToPatchBodyReader, false);
106
107         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
108             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml"));
109         checkPatchContext(returnValue);
110     }
111
112     /**
113      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
114      */
115     @Test
116     public void moduleDataMergeOperationOnListTest() throws Exception {
117         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
118         mockBodyReader(uri, xmlToPatchBodyReader, false);
119
120         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
121             XmlBodyReaderTest.class.getResourceAsStream(
122                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml"));
123         checkPatchContext(returnValue);
124     }
125
126     /**
127      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
128      */
129     @Test
130     public void moduleDataMergeOperationOnContainerTest() throws Exception {
131         final String uri = "instance-identifier-patch-module:patch-cont";
132         mockBodyReader(uri, xmlToPatchBodyReader, false);
133         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
134             XmlBodyReaderTest.class.getResourceAsStream(
135                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml"));
136         checkPatchContext(returnValue);
137     }
138 }