1bdbab9519a476c4967b77e5304fccdf53520d20
[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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
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.netconf.sal.rest.impl.XmlToPatchBodyReader;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.patch.PatchContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22
23 public class TestXmlPatchBodyReader extends AbstractBodyReaderTest {
24
25     private final XmlToPatchBodyReader xmlToPatchBodyReader;
26     private static SchemaContext schemaContext;
27
28     public TestXmlPatchBodyReader() throws NoSuchFieldException, SecurityException {
29         xmlToPatchBodyReader = new XmlToPatchBodyReader();
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         CONTROLLER_CONTEXT.setSchemas(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
48                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
49         final PatchContext returnValue = xmlToPatchBodyReader
50                 .readFrom(null, null, null, mediaType, null, inputStream);
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 = TestXmlBodyReader.class
62                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
63         try {
64             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
65             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
66         } catch (final RestconfDocumentedException e) {
67             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
68         }
69     }
70
71     /**
72      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
73      * returned.
74      */
75     @Test
76     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
77         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
78         mockBodyReader(uri, xmlToPatchBodyReader, false);
79         final InputStream inputStream = TestXmlBodyReader.class
80                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
81         try {
82             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
83             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
84         } catch (final RestconfDocumentedException e) {
85             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
86         }
87     }
88
89     /**
90      * Test of Yang Patch with absolute target path.
91      */
92     @Test
93     public void moduleDataAbsoluteTargetPathTest() throws Exception {
94         final String uri = "";
95         mockBodyReader(uri, xmlToPatchBodyReader, false);
96         final InputStream inputStream = TestXmlBodyReader.class
97                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
98         final PatchContext returnValue = xmlToPatchBodyReader
99                 .readFrom(null, null, null, mediaType, null, inputStream);
100         checkPatchContext(returnValue);
101     }
102
103     /**
104      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
105      */
106     @Test
107     public void modulePatchCompleteTargetInURITest() throws Exception {
108         final String uri = "instance-identifier-patch-module:patch-cont";
109         mockBodyReader(uri, xmlToPatchBodyReader, false);
110         final InputStream inputStream = TestXmlBodyReader.class
111                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
112         final PatchContext returnValue = xmlToPatchBodyReader
113                 .readFrom(null, null, null, mediaType, null, inputStream);
114         checkPatchContext(returnValue);
115     }
116
117     /**
118      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
119      */
120     @Test
121     public void moduleDataMergeOperationOnListTest() throws Exception {
122         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
123         mockBodyReader(uri, xmlToPatchBodyReader, false);
124         final InputStream inputStream = TestXmlBodyReader.class
125                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
126         final PatchContext returnValue = xmlToPatchBodyReader
127                 .readFrom(null, null, null, mediaType, null, inputStream);
128         checkPatchContext(returnValue);
129     }
130
131     /**
132      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
133      */
134     @Test
135     public void moduleDataMergeOperationOnContainerTest() throws Exception {
136         final String uri = "instance-identifier-patch-module:patch-cont";
137         mockBodyReader(uri, xmlToPatchBodyReader, false);
138         final InputStream inputStream = TestXmlBodyReader.class
139                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
140         final PatchContext returnValue = xmlToPatchBodyReader
141                 .readFrom(null, null, null, mediaType, null, inputStream);
142         checkPatchContext(returnValue);
143     }
144 }