2 * Copyright (c) 2015 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.fail;
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.netconf.sal.rest.impl.XmlToPatchBodyReader;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 public class TestXmlPatchBodyReader extends AbstractBodyReaderTest {
24 private final XmlToPatchBodyReader xmlToPatchBodyReader;
25 private static EffectiveModelContext schemaContext;
27 public TestXmlPatchBodyReader() {
28 super(schemaContext, null);
29 xmlToPatchBodyReader = new XmlToPatchBodyReader(controllerContext);
33 protected MediaType getMediaType() {
34 return new MediaType(MediaType.APPLICATION_XML, null);
38 public static void initialization() throws NoSuchFieldException, SecurityException {
39 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
43 public void moduleDataTest() throws Exception {
44 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
45 mockBodyReader(uri, xmlToPatchBodyReader, false);
46 final InputStream inputStream = TestXmlBodyReader.class
47 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
48 final PatchContext returnValue = xmlToPatchBodyReader
49 .readFrom(null, null, null, mediaType, null, inputStream);
50 checkPatchContext(returnValue);
54 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
57 public void moduleDataValueMissingNegativeTest() throws Exception {
58 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
59 mockBodyReader(uri, xmlToPatchBodyReader, false);
60 final InputStream inputStream = TestXmlBodyReader.class
61 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
63 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
64 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
65 } catch (final RestconfDocumentedException e) {
66 assertEquals("Error code 400 expected", 400, e.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 = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
77 mockBodyReader(uri, xmlToPatchBodyReader, false);
78 final InputStream inputStream = TestXmlBodyReader.class
79 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
81 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
82 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
83 } catch (final RestconfDocumentedException e) {
84 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
89 * Test of Yang Patch with absolute target path.
92 public void moduleDataAbsoluteTargetPathTest() throws Exception {
93 final String uri = "";
94 mockBodyReader(uri, xmlToPatchBodyReader, false);
95 final InputStream inputStream = TestXmlBodyReader.class
96 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
97 final PatchContext returnValue = xmlToPatchBodyReader
98 .readFrom(null, null, null, mediaType, null, inputStream);
99 checkPatchContext(returnValue);
103 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
106 public void modulePatchCompleteTargetInURITest() throws Exception {
107 final String uri = "instance-identifier-patch-module:patch-cont";
108 mockBodyReader(uri, xmlToPatchBodyReader, false);
109 final InputStream inputStream = TestXmlBodyReader.class
110 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
111 final PatchContext returnValue = xmlToPatchBodyReader
112 .readFrom(null, null, null, mediaType, null, inputStream);
113 checkPatchContext(returnValue);
117 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
120 public void moduleDataMergeOperationOnListTest() throws Exception {
121 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
122 mockBodyReader(uri, xmlToPatchBodyReader, false);
123 final InputStream inputStream = TestXmlBodyReader.class
124 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
125 final PatchContext returnValue = xmlToPatchBodyReader
126 .readFrom(null, null, null, mediaType, null, inputStream);
127 checkPatchContext(returnValue);
131 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
134 public void moduleDataMergeOperationOnContainerTest() throws Exception {
135 final String uri = "instance-identifier-patch-module:patch-cont";
136 mockBodyReader(uri, xmlToPatchBodyReader, false);
137 final InputStream inputStream = TestXmlBodyReader.class
138 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
139 final PatchContext returnValue = xmlToPatchBodyReader
140 .readFrom(null, null, null, mediaType, null, inputStream);
141 checkPatchContext(returnValue);