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.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
25 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
27 private static EffectiveModelContext schemaContext;
29 private final XmlPatchBodyReader xmlToPatchBodyReader;
31 public XmlPatchBodyReaderMountPointTest() throws Exception {
33 xmlToPatchBodyReader = new XmlPatchBodyReader(schemaContextHandler, mountPointService);
37 protected MediaType getMediaType() {
38 return new MediaType(MediaType.APPLICATION_XML, null);
42 public static void initialization() {
43 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
47 public void moduleDataTest() throws Exception {
48 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
49 mockBodyReader(uri, xmlToPatchBodyReader, false);
51 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
52 "/instanceidentifier/xml/xmlPATCHdata.xml");
53 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
54 checkPatchContextMountPoint(returnValue);
58 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
61 public void moduleDataValueMissingNegativeTest() 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 = XmlBodyReaderTest.class.getResourceAsStream(
65 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
67 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
68 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
69 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
73 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
77 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
78 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
79 mockBodyReader(uri, xmlToPatchBodyReader, false);
80 final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
81 "/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
83 RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
84 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
85 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
89 * Test of Yang Patch with absolute target path.
92 public void moduleDataAbsoluteTargetPathTest() throws Exception {
93 final String uri = MOUNT_POINT;
94 mockBodyReader(uri, xmlToPatchBodyReader, false);
96 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
97 XmlBodyReaderTest.class.getResourceAsStream(
98 "/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml")));
102 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
105 public void modulePatchCompleteTargetInURITest() throws Exception {
106 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
107 mockBodyReader(uri, xmlToPatchBodyReader, false);
109 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
110 XmlBodyReaderTest.class.getResourceAsStream(
111 "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml")));
115 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
118 public void moduleDataMergeOperationOnListTest() throws Exception {
119 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
120 mockBodyReader(uri, xmlToPatchBodyReader, false);
122 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
123 XmlBodyReaderTest.class.getResourceAsStream(
124 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml")));
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 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null,
136 XmlBodyReaderTest.class.getResourceAsStream(
137 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml")));