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