Remove DOMMountPointServiceHandler
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchBodyReaderTest.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.restconf.nb.rfc8040.jersey.providers.patch;
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.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.patch.PatchContext;
19 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22
23 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
24     private static EffectiveModelContext schemaContext;
25
26     private final XmlToPatchBodyReader xmlToPatchBodyReader;
27
28     public XmlPatchBodyReaderTest() throws Exception {
29         super(schemaContext);
30         xmlToPatchBodyReader = new XmlToPatchBodyReader(schemaContextHandler, mountPointService);
31     }
32
33     @Override
34     protected MediaType getMediaType() {
35         return new MediaType(MediaType.APPLICATION_XML, null);
36     }
37
38     @BeforeClass
39     public static void initialization() {
40         schemaContext = schemaContextLoader("/instanceidentifier/yang", 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 = XmlBodyReaderTest.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 = XmlBodyReaderTest.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 = XmlBodyReaderTest.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     /**
91      * Test of Yang Patch with absolute target path.
92      */
93     @Test
94     public void moduleDataAbsoluteTargetPathTest() throws Exception {
95         final String uri = "";
96         mockBodyReader(uri, xmlToPatchBodyReader, false);
97         final InputStream inputStream = XmlBodyReaderTest.class
98                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
99         final PatchContext returnValue = xmlToPatchBodyReader
100                 .readFrom(null, null, null, mediaType, null, inputStream);
101         checkPatchContext(returnValue);
102     }
103
104     /**
105      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
106      */
107     @Test
108     public void modulePatchCompleteTargetInURITest() throws Exception {
109         final String uri = "instance-identifier-patch-module:patch-cont";
110         mockBodyReader(uri, xmlToPatchBodyReader, false);
111         final InputStream inputStream = XmlBodyReaderTest.class
112                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
113         final PatchContext returnValue = xmlToPatchBodyReader
114                 .readFrom(null, null, null, mediaType, null, inputStream);
115         checkPatchContext(returnValue);
116     }
117
118     /**
119      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
120      */
121     @Test
122     public void moduleDataMergeOperationOnListTest() throws Exception {
123         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
124         mockBodyReader(uri, xmlToPatchBodyReader, false);
125         final InputStream inputStream = XmlBodyReaderTest.class
126                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
127         final PatchContext returnValue = xmlToPatchBodyReader
128                 .readFrom(null, null, null, mediaType, null, inputStream);
129         checkPatchContext(returnValue);
130     }
131
132     /**
133      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
134      */
135     @Test
136     public void moduleDataMergeOperationOnContainerTest() throws Exception {
137         final String uri = "instance-identifier-patch-module:patch-cont";
138         mockBodyReader(uri, xmlToPatchBodyReader, false);
139         final InputStream inputStream = XmlBodyReaderTest.class
140                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
141         final PatchContext returnValue = xmlToPatchBodyReader
142                 .readFrom(null, null, null, mediaType, null, inputStream);
143         checkPatchContext(returnValue);
144     }
145 }