2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
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;
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;
32 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
34 private final XmlToPatchBodyReader xmlToPatchBodyReader;
35 private static SchemaContext schemaContext;
36 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
38 public XmlPatchBodyReaderMountPointTest() throws Exception {
40 xmlToPatchBodyReader = new XmlToPatchBodyReader();
44 protected MediaType getMediaType() {
45 return new MediaType(MediaType.APPLICATION_XML, null);
49 public static void initialization() {
50 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
52 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
53 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
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);
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);
73 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
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");
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());
90 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
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");
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());
109 * Test of Yang Patch with absolute target path.
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);
123 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
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);
137 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
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);
151 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
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);