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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
13 import java.io.InputStream;
14 import javax.ws.rs.core.MediaType;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.patch.PatchContext;
19 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
24 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
26 private static EffectiveModelContext schemaContext;
28 private final XmlPatchBodyReader xmlToPatchBodyReader;
30 public XmlPatchBodyReaderMountPointTest() throws Exception {
32 xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
36 protected MediaType getMediaType() {
37 return new MediaType(MediaType.APPLICATION_XML, null);
41 public static void initialization() {
42 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
46 public void moduleDataTest() throws Exception {
47 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
48 mockBodyReader(uri, xmlToPatchBodyReader, false);
50 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
51 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml"));
52 checkPatchContextMountPoint(returnValue);
56 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
59 public void moduleDataValueMissingNegativeTest() throws Exception {
60 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
61 mockBodyReader(uri, xmlToPatchBodyReader, false);
62 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
63 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
65 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
66 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
67 assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
71 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
75 public void moduleDataNotValueNotSupportedNegativeTest() 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 = XmlBodyReaderTest.class.getResourceAsStream(
79 "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
81 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
82 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
83 assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
88 * Test of Yang Patch with absolute target path.
91 public void moduleDataAbsoluteTargetPathTest() throws Exception {
92 final String uri = MOUNT_POINT;
93 mockBodyReader(uri, xmlToPatchBodyReader, false);
95 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
96 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml"));
97 checkPatchContextMountPoint(returnValue);
101 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
104 public void modulePatchCompleteTargetInURITest() throws Exception {
105 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
106 mockBodyReader(uri, xmlToPatchBodyReader, false);
108 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
109 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml"));
110 checkPatchContextMountPoint(returnValue);
114 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
117 public void moduleDataMergeOperationOnListTest() throws Exception {
118 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
119 mockBodyReader(uri, xmlToPatchBodyReader, false);
121 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
122 XmlBodyReaderTest.class.getResourceAsStream(
123 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml"));
124 checkPatchContextMountPoint(returnValue);
128 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
131 public void moduleDataMergeOperationOnContainerTest() throws Exception {
132 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
133 mockBodyReader(uri, xmlToPatchBodyReader, false);
135 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
136 XmlBodyReaderTest.class.getResourceAsStream(
137 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml"));
138 checkPatchContextMountPoint(returnValue);