8b37ba4ee50d03ca63ec3042d0a5f42d48ea1216
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlPatchBodyReader.java
1 /*
2  * Copyright (c) 2015 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.controller.sal.rest.impl.test.providers;
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.netconf.sal.rest.impl.XmlToPatchBodyReader;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
20 import org.opendaylight.yangtools.yang.common.ErrorTag;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22
23 public class TestXmlPatchBodyReader extends AbstractBodyReaderTest {
24
25     private final XmlToPatchBodyReader xmlToPatchBodyReader;
26     private static EffectiveModelContext schemaContext;
27
28     public TestXmlPatchBodyReader() {
29         super(schemaContext, null);
30         xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext);
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() throws NoSuchFieldException, SecurityException {
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         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
48             "/instanceidentifier/xml/xmlPATCHdata.xml");
49         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
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 = TestXmlBodyReader.class.getResourceAsStream(
61             "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
62         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
63             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
64         assertEquals(1, ex.getErrors().size());
65         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
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 = TestXmlBodyReader.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(1, ex.getErrors().size());
82         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
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         final InputStream inputStream = TestXmlBodyReader.class
93                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
94         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
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         final InputStream inputStream = TestXmlBodyReader.class
106                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
107         final PatchContext returnValue = xmlToPatchBodyReader
108                 .readFrom(null, null, null, mediaType, null, inputStream);
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         final InputStream inputStream = TestXmlBodyReader.class
120                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
121         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
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 InputStream inputStream = TestXmlBodyReader.class
133                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
134         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
135         checkPatchContext(returnValue);
136     }
137 }