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