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