3eb8f43f687451fdf98027491ea44690966c8d81
[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.model.api.EffectiveModelContext;
22
23 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
24     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
25
26     private static EffectiveModelContext schemaContext;
27
28     private final XmlPatchBodyReader xmlToPatchBodyReader;
29
30     public XmlPatchBodyReaderMountPointTest() throws Exception {
31         super(schemaContext);
32         xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
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() {
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
50         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
51             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml"));
52         checkPatchContextMountPoint(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 = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
61         mockBodyReader(uri, xmlToPatchBodyReader, false);
62         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
63             "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
64
65         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
66             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
67         assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
68     }
69
70     /**
71      * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
72      * returned.
73      */
74     @Test
75     public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
76         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
77         mockBodyReader(uri, xmlToPatchBodyReader, false);
78         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
79             "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
80
81         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
82             () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
83         assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
84     }
85
86
87     /**
88      * Test of Yang Patch with absolute target path.
89      */
90     @Test
91     public void moduleDataAbsoluteTargetPathTest() throws Exception {
92         final String uri = MOUNT_POINT;
93         mockBodyReader(uri, xmlToPatchBodyReader, false);
94
95         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
96             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml"));
97         checkPatchContextMountPoint(returnValue);
98     }
99
100     /**
101      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
102      */
103     @Test
104     public void modulePatchCompleteTargetInURITest() throws Exception {
105         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
106         mockBodyReader(uri, xmlToPatchBodyReader, false);
107
108         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
109             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml"));
110         checkPatchContextMountPoint(returnValue);
111     }
112
113     /**
114      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
115      */
116     @Test
117     public void moduleDataMergeOperationOnListTest() throws Exception {
118         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
119         mockBodyReader(uri, xmlToPatchBodyReader, false);
120
121         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
122             XmlBodyReaderTest.class.getResourceAsStream(
123                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml"));
124         checkPatchContextMountPoint(returnValue);
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         final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
136             XmlBodyReaderTest.class.getResourceAsStream(
137                 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml"));
138         checkPatchContextMountPoint(returnValue);
139     }
140 }