614f36ed026c29a98586a3918854d0d89ff1d7c6
[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 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 import static org.mockito.Mockito.mock;
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.mdsal.dom.api.DOMMountPoint;
19 import org.opendaylight.netconf.sal.rest.impl.XmlToPatchBodyReader;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.common.patch.PatchContext;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24
25 public class TestXmlPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
26
27     private final XmlToPatchBodyReader xmlToPatchBodyReader;
28     private static EffectiveModelContext 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     @BeforeClass
37     public static void initialization() throws NoSuchFieldException, SecurityException {
38         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
39     }
40
41     @Override
42     protected MediaType getMediaType() {
43         return new MediaType(MediaType.APPLICATION_XML, null);
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.getResourceAsStream(
51             "/instanceidentifier/xml/xmlPATCHdata.xml");
52         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
53     }
54
55     /**
56      * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
57      */
58     @Test
59     public void moduleDataValueMissingNegativeTest() throws Exception {
60         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
61         mockBodyReader(uri, xmlToPatchBodyReader, false);
62         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
63             "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
64         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
65             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
66         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
67     }
68
69     /**
70      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
71      * returned.
72      */
73     @Test
74     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
75         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
76         mockBodyReader(uri, xmlToPatchBodyReader, false);
77         final InputStream inputStream = TestXmlBodyReader.class
78                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
79         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
82     }
83
84     /**
85      * Test of Yang Patch with absolute target path.
86      */
87     @Test
88     public void moduleDataAbsoluteTargetPathTest() throws Exception {
89         final String uri = MOUNT_POINT;
90         mockBodyReader(uri, xmlToPatchBodyReader, false);
91         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
92             "/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
93         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
94         checkPatchContextMountPoint(returnValue);
95     }
96
97     /**
98      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
99      */
100     @Test
101     public void modulePatchCompleteTargetInURITest() throws Exception {
102         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
103         mockBodyReader(uri, xmlToPatchBodyReader, false);
104         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
105             "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
106         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
107         checkPatchContextMountPoint(returnValue);
108     }
109
110     /**
111      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
112      */
113     @Test
114     public void moduleDataMergeOperationOnListTest() throws Exception {
115         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
116         mockBodyReader(uri, xmlToPatchBodyReader, false);
117         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
118             "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
119         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
120     }
121
122     /**
123      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
124      */
125     @Test
126     public void moduleDataMergeOperationOnContainerTest() throws Exception {
127         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
128         mockBodyReader(uri, xmlToPatchBodyReader, false);
129         final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
130             "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
131         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
132     }
133 }