9e3f54cd824c49f82a6b3f175b082938bbb425b0
[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.fail;
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.model.api.EffectiveModelContext;
21
22 public class TestXmlPatchBodyReader extends AbstractBodyReaderTest {
23
24     private final XmlToPatchBodyReader xmlToPatchBodyReader;
25     private static EffectiveModelContext schemaContext;
26
27     public TestXmlPatchBodyReader() {
28         super(schemaContext, null);
29         xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext);
30     }
31
32     @Override
33     protected MediaType getMediaType() {
34         return new MediaType(MediaType.APPLICATION_XML, null);
35     }
36
37     @BeforeClass
38     public static void initialization() throws NoSuchFieldException, SecurityException {
39         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
40     }
41
42     @Test
43     public void moduleDataTest() throws Exception {
44         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
45         mockBodyReader(uri, xmlToPatchBodyReader, false);
46         final InputStream inputStream = TestXmlBodyReader.class
47                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
48         final PatchContext returnValue = xmlToPatchBodyReader
49                 .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
61                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
62         try {
63             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
64             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
65         } catch (final RestconfDocumentedException e) {
66             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
67         }
68     }
69
70     /**
71      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
72      * returned.
73      */
74     @Test
75     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
76         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
77         mockBodyReader(uri, xmlToPatchBodyReader, false);
78         final InputStream inputStream = TestXmlBodyReader.class
79                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
80         try {
81             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
82             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
83         } catch (final RestconfDocumentedException e) {
84             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
85         }
86     }
87
88     /**
89      * Test of Yang Patch with absolute target path.
90      */
91     @Test
92     public void moduleDataAbsoluteTargetPathTest() throws Exception {
93         final String uri = "";
94         mockBodyReader(uri, xmlToPatchBodyReader, false);
95         final InputStream inputStream = TestXmlBodyReader.class
96                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
97         final PatchContext returnValue = xmlToPatchBodyReader
98                 .readFrom(null, null, null, mediaType, null, inputStream);
99         checkPatchContext(returnValue);
100     }
101
102     /**
103      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
104      */
105     @Test
106     public void modulePatchCompleteTargetInURITest() throws Exception {
107         final String uri = "instance-identifier-patch-module:patch-cont";
108         mockBodyReader(uri, xmlToPatchBodyReader, false);
109         final InputStream inputStream = TestXmlBodyReader.class
110                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
111         final PatchContext returnValue = xmlToPatchBodyReader
112                 .readFrom(null, null, null, mediaType, null, inputStream);
113         checkPatchContext(returnValue);
114     }
115
116     /**
117      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
118      */
119     @Test
120     public void moduleDataMergeOperationOnListTest() throws Exception {
121         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
122         mockBodyReader(uri, xmlToPatchBodyReader, false);
123         final InputStream inputStream = TestXmlBodyReader.class
124                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
125         final PatchContext returnValue = xmlToPatchBodyReader
126                 .readFrom(null, null, null, mediaType, null, inputStream);
127         checkPatchContext(returnValue);
128     }
129
130     /**
131      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
132      */
133     @Test
134     public void moduleDataMergeOperationOnContainerTest() throws Exception {
135         final String uri = "instance-identifier-patch-module:patch-cont";
136         mockBodyReader(uri, xmlToPatchBodyReader, false);
137         final InputStream inputStream = TestXmlBodyReader.class
138                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
139         final PatchContext returnValue = xmlToPatchBodyReader
140                 .readFrom(null, null, null, mediaType, null, inputStream);
141         checkPatchContext(returnValue);
142     }
143 }