Merge "Convert rcf8040 from web.xml to programmtic web API"
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlPatchBodyReaderMountPoint.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.controller.sal.rest.impl.test.providers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Mockito.mock;
14
15 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
20 import org.opendaylight.netconf.sal.rest.impl.XmlToPatchBodyReader;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.patch.PatchContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 public class TestXmlPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
26
27     private final XmlToPatchBodyReader xmlToPatchBodyReader;
28     private static SchemaContext schemaContext;
29     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount";
30
31     public TestXmlPatchBodyReaderMountPoint() {
32         super(schemaContext, mock(DOMMountPoint.class));
33         xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext);
34     }
35
36     @Override
37     protected MediaType getMediaType() {
38         return new MediaType(MediaType.APPLICATION_XML, null);
39     }
40
41     @BeforeClass
42     public static void initialization() throws NoSuchFieldException, SecurityException {
43         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
44     }
45
46     @Test
47     public void moduleDataTest() throws Exception {
48         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
49         mockBodyReader(uri, xmlToPatchBodyReader, false);
50         final InputStream inputStream = TestXmlBodyReader.class
51                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
52         final PatchContext returnValue = xmlToPatchBodyReader
53                 .readFrom(null, null, null, mediaType, null, inputStream);
54         checkPatchContextMountPoint(returnValue);
55     }
56
57     /**
58      * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
59      */
60     @Test
61     public void moduleDataValueMissingNegativeTest() throws Exception {
62         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
63         mockBodyReader(uri, xmlToPatchBodyReader, false);
64         final InputStream inputStream = TestXmlBodyReader.class
65                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
66         try {
67             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
68             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
69         } catch (final RestconfDocumentedException e) {
70             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
71         }
72     }
73
74     /**
75      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
76      * returned.
77      */
78     @Test
79     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
80         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
81         mockBodyReader(uri, xmlToPatchBodyReader, false);
82         final InputStream inputStream = TestXmlBodyReader.class
83                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
84         try {
85             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
86             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
87         } catch (final RestconfDocumentedException e) {
88             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
89         }
90     }
91
92     /**
93      * Test of Yang Patch with absolute target path.
94      */
95     @Test
96     public void moduleDataAbsoluteTargetPathTest() throws Exception {
97         final String uri = MOUNT_POINT;
98         mockBodyReader(uri, xmlToPatchBodyReader, false);
99         final InputStream inputStream = TestXmlBodyReader.class
100                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
101         final PatchContext returnValue = xmlToPatchBodyReader
102                 .readFrom(null, null, null, mediaType, null, inputStream);
103         checkPatchContextMountPoint(returnValue);
104     }
105
106     /**
107      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
108      */
109     @Test
110     public void modulePatchCompleteTargetInURITest() throws Exception {
111         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
112         mockBodyReader(uri, xmlToPatchBodyReader, false);
113         final InputStream inputStream = TestXmlBodyReader.class
114                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
115         final PatchContext returnValue = xmlToPatchBodyReader
116                 .readFrom(null, null, null, mediaType, null, inputStream);
117         checkPatchContextMountPoint(returnValue);
118     }
119
120     /**
121      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
122      */
123     @Test
124     public void moduleDataMergeOperationOnListTest() throws Exception {
125         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
126         mockBodyReader(uri, xmlToPatchBodyReader, false);
127         final InputStream inputStream = TestXmlBodyReader.class
128                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
129         final PatchContext returnValue = xmlToPatchBodyReader
130                 .readFrom(null, null, null, mediaType, null, inputStream);
131         checkPatchContextMountPoint(returnValue);
132     }
133
134     /**
135      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
136      */
137     @Test
138     public void moduleDataMergeOperationOnContainerTest() throws Exception {
139         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
140         mockBodyReader(uri, xmlToPatchBodyReader, false);
141         final InputStream inputStream = TestXmlBodyReader.class
142                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
143         final PatchContext returnValue = xmlToPatchBodyReader
144                 .readFrom(null, null, null, mediaType, null, inputStream);
145         checkPatchContextMountPoint(returnValue);
146     }
147 }