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