cc29eadd5395767364415fae90982c5dfac90c9d
[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.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;
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         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
46         mockBodyReader(uri, xmlToPatchBodyReader, false);
47
48         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
49             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml"));
50         checkPatchContext(returnValue);
51     }
52
53     /**
54      * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
55      */
56     @Test
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");
62
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());
66     }
67
68     /**
69      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
70      * returned.
71      */
72     @Test
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");
78
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());
82     }
83
84
85     /**
86      * Test of Yang Patch with absolute target path.
87      */
88     @Test
89     public void moduleDataAbsoluteTargetPathTest() throws Exception {
90         final String uri = "";
91         mockBodyReader(uri, xmlToPatchBodyReader, false);
92
93         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
94             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml"));
95         checkPatchContext(returnValue);
96     }
97
98     /**
99      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
100      */
101     @Test
102     public void modulePatchCompleteTargetInURITest() throws Exception {
103         final String uri = "instance-identifier-patch-module:patch-cont";
104         mockBodyReader(uri, xmlToPatchBodyReader, false);
105
106         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
107             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml"));
108         checkPatchContext(returnValue);
109     }
110
111     /**
112      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
113      */
114     @Test
115     public void moduleDataMergeOperationOnListTest() throws Exception {
116         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
117         mockBodyReader(uri, xmlToPatchBodyReader, false);
118
119         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
120             XmlBodyReaderTest.class.getResourceAsStream(
121                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml"));
122         checkPatchContext(returnValue);
123     }
124
125     /**
126      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
127      */
128     @Test
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);
136     }
137 }