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