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