Remove RestconfError.ErrorTag
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / XmlPatchBodyReaderMountPointTest.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.assertThrows;
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.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23
24 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
25     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
26
27     private static EffectiveModelContext schemaContext;
28
29     private final XmlPatchBodyReader xmlToPatchBodyReader;
30
31     public XmlPatchBodyReaderMountPointTest() throws Exception {
32         super(schemaContext);
33         xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
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() {
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
51         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
52             "/instanceidentifier/xml/xmlPATCHdata.xml");
53         final PatchContext returnValue = xmlToPatchBodyReader.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 = XmlBodyReaderTest.class.getResourceAsStream(
65             "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
66
67         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
68             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
69         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
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 = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
79         mockBodyReader(uri, xmlToPatchBodyReader, false);
80         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
81             "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
82
83         RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
84             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
85         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
86     }
87
88     /**
89      * Test of Yang Patch with absolute target path.
90      */
91     @Test
92     public void moduleDataAbsoluteTargetPathTest() throws Exception {
93         final String uri = MOUNT_POINT;
94         mockBodyReader(uri, xmlToPatchBodyReader, false);
95
96         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
97             XmlBodyReaderTest.class.getResourceAsStream(
98                 "/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml")));
99     }
100
101     /**
102      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
103      */
104     @Test
105     public void modulePatchCompleteTargetInURITest() throws Exception {
106         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
107         mockBodyReader(uri, xmlToPatchBodyReader, false);
108
109         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
110             XmlBodyReaderTest.class.getResourceAsStream(
111                 "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml")));
112     }
113
114     /**
115      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
116      */
117     @Test
118     public void moduleDataMergeOperationOnListTest() throws Exception {
119         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
120         mockBodyReader(uri, xmlToPatchBodyReader, false);
121
122         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
123             XmlBodyReaderTest.class.getResourceAsStream(
124                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml")));
125     }
126
127     /**
128      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
129      */
130     @Test
131     public void moduleDataMergeOperationOnContainerTest() throws Exception {
132         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
133         mockBodyReader(uri, xmlToPatchBodyReader, false);
134
135         checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
136             XmlBodyReaderTest.class.getResourceAsStream(
137                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml")));
138     }
139 }