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