Update MRI projects for Aluminium
[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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
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.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23
24 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
25
26     private final XmlToPatchBodyReader xmlToPatchBodyReader;
27     private static EffectiveModelContext schemaContext;
28
29     public XmlPatchBodyReaderTest() throws Exception {
30         super(schemaContext);
31         xmlToPatchBodyReader = new XmlToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
32     }
33
34     @Override
35     protected MediaType getMediaType() {
36         return new MediaType(MediaType.APPLICATION_XML, null);
37     }
38
39     @BeforeClass
40     public static void initialization() {
41         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
42     }
43
44     @Test
45     public void moduleDataTest() throws Exception {
46         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
47         mockBodyReader(uri, xmlToPatchBodyReader, false);
48         final InputStream inputStream = XmlBodyReaderTest.class
49                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
50         final PatchContext returnValue = xmlToPatchBodyReader
51                 .readFrom(null, null, null, mediaType, null, inputStream);
52         checkPatchContext(returnValue);
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 = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
61         mockBodyReader(uri, xmlToPatchBodyReader, false);
62         final InputStream inputStream = XmlBodyReaderTest.class
63                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
64         try {
65             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
66             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
67         } catch (final RestconfDocumentedException e) {
68             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
69         }
70     }
71
72     /**
73      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
74      * returned.
75      */
76     @Test
77     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
78         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
79         mockBodyReader(uri, xmlToPatchBodyReader, false);
80         final InputStream inputStream = XmlBodyReaderTest.class
81                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
82         try {
83             xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
84             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
85         } catch (final RestconfDocumentedException e) {
86             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
87         }
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 = "";
97         mockBodyReader(uri, xmlToPatchBodyReader, false);
98         final InputStream inputStream = XmlBodyReaderTest.class
99                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
100         final PatchContext returnValue = xmlToPatchBodyReader
101                 .readFrom(null, null, null, mediaType, null, inputStream);
102         checkPatchContext(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 = "instance-identifier-patch-module:patch-cont";
111         mockBodyReader(uri, xmlToPatchBodyReader, false);
112         final InputStream inputStream = XmlBodyReaderTest.class
113                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
114         final PatchContext returnValue = xmlToPatchBodyReader
115                 .readFrom(null, null, null, mediaType, null, inputStream);
116         checkPatchContext(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 = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
125         mockBodyReader(uri, xmlToPatchBodyReader, false);
126         final InputStream inputStream = XmlBodyReaderTest.class
127                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
128         final PatchContext returnValue = xmlToPatchBodyReader
129                 .readFrom(null, null, null, mediaType, null, inputStream);
130         checkPatchContext(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 = "instance-identifier-patch-module:patch-cont";
139         mockBodyReader(uri, xmlToPatchBodyReader, false);
140         final InputStream inputStream = XmlBodyReaderTest.class
141                 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
142         final PatchContext returnValue = xmlToPatchBodyReader
143                 .readFrom(null, null, null, mediaType, null, inputStream);
144         checkPatchContext(returnValue);
145     }
146 }