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.controller.sal.rest.impl.test.providers;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.mockito.Mockito.mock;
14 import java.io.InputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
19 import org.opendaylight.netconf.sal.rest.impl.XmlToPatchBodyReader;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.common.patch.PatchContext;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 public class TestXmlPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
27 private final XmlToPatchBodyReader xmlToPatchBodyReader;
28 private static EffectiveModelContext schemaContext;
29 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount";
31 public TestXmlPatchBodyReaderMountPoint() {
32 super(schemaContext, mock(DOMMountPoint.class));
33 xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext);
37 public static void initialization() throws NoSuchFieldException, SecurityException {
38 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
42 protected MediaType getMediaType() {
43 return new MediaType(MediaType.APPLICATION_XML, null);
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);
50 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
51 "/instanceidentifier/xml/xmlPATCHdata.xml");
52 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
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 = TestXmlBodyReader.class.getResourceAsStream(
63 "/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
64 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
65 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
66 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
70 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
74 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
75 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
76 mockBodyReader(uri, xmlToPatchBodyReader, false);
77 final InputStream inputStream = TestXmlBodyReader.class
78 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
79 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80 () -> xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
85 * Test of Yang Patch with absolute target path.
88 public void moduleDataAbsoluteTargetPathTest() throws Exception {
89 final String uri = MOUNT_POINT;
90 mockBodyReader(uri, xmlToPatchBodyReader, false);
91 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
92 "/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
93 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
94 checkPatchContextMountPoint(returnValue);
98 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
101 public void modulePatchCompleteTargetInURITest() throws Exception {
102 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
103 mockBodyReader(uri, xmlToPatchBodyReader, false);
104 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
105 "/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
106 final PatchContext returnValue = xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
107 checkPatchContextMountPoint(returnValue);
111 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
114 public void moduleDataMergeOperationOnListTest() throws Exception {
115 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
116 mockBodyReader(uri, xmlToPatchBodyReader, false);
117 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
118 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
119 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
123 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
126 public void moduleDataMergeOperationOnContainerTest() throws Exception {
127 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
128 mockBodyReader(uri, xmlToPatchBodyReader, false);
129 final InputStream inputStream = TestXmlBodyReader.class.getResourceAsStream(
130 "/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
131 checkPatchContextMountPoint(xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));