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
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
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.netconf.sal.rest.impl.XmlToPatchBodyReader;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.patch.PatchContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 public class TestXmlPatchBodyReader extends AbstractBodyReaderTest {
25 private final XmlToPatchBodyReader xmlToPatchBodyReader;
26 private static SchemaContext schemaContext;
28 public TestXmlPatchBodyReader() throws NoSuchFieldException, SecurityException {
29 xmlToPatchBodyReader = new XmlToPatchBodyReader();
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);
40 CONTROLLER_CONTEXT.setSchemas(schemaContext);
44 public void moduleDataTest() throws Exception {
45 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
46 mockBodyReader(uri, xmlToPatchBodyReader, false);
47 final InputStream inputStream = TestXmlBodyReader.class
48 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
49 final PatchContext returnValue = xmlToPatchBodyReader
50 .readFrom(null, null, null, mediaType, null, inputStream);
51 checkPatchContext(returnValue);
55 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
58 public void moduleDataValueMissingNegativeTest() throws Exception {
59 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
60 mockBodyReader(uri, xmlToPatchBodyReader, false);
61 final InputStream inputStream = TestXmlBodyReader.class
62 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
64 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
65 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
66 } catch (final RestconfDocumentedException e) {
67 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
72 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
76 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
77 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
78 mockBodyReader(uri, xmlToPatchBodyReader, false);
79 final InputStream inputStream = TestXmlBodyReader.class
80 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
82 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
83 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
84 } catch (final RestconfDocumentedException e) {
85 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
90 * Test of Yang Patch with absolute target path.
93 public void moduleDataAbsoluteTargetPathTest() throws Exception {
94 final String uri = "";
95 mockBodyReader(uri, xmlToPatchBodyReader, false);
96 final InputStream inputStream = TestXmlBodyReader.class
97 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
98 final PatchContext returnValue = xmlToPatchBodyReader
99 .readFrom(null, null, null, mediaType, null, inputStream);
100 checkPatchContext(returnValue);
104 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
107 public void modulePatchCompleteTargetInURITest() throws Exception {
108 final String uri = "instance-identifier-patch-module:patch-cont";
109 mockBodyReader(uri, xmlToPatchBodyReader, false);
110 final InputStream inputStream = TestXmlBodyReader.class
111 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
112 final PatchContext returnValue = xmlToPatchBodyReader
113 .readFrom(null, null, null, mediaType, null, inputStream);
114 checkPatchContext(returnValue);
118 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
121 public void moduleDataMergeOperationOnListTest() throws Exception {
122 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
123 mockBodyReader(uri, xmlToPatchBodyReader, false);
124 final InputStream inputStream = TestXmlBodyReader.class
125 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
126 final PatchContext returnValue = xmlToPatchBodyReader
127 .readFrom(null, null, null, mediaType, null, inputStream);
128 checkPatchContext(returnValue);
132 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
135 public void moduleDataMergeOperationOnContainerTest() throws Exception {
136 final String uri = "instance-identifier-patch-module:patch-cont";
137 mockBodyReader(uri, xmlToPatchBodyReader, false);
138 final InputStream inputStream = TestXmlBodyReader.class
139 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
140 final PatchContext returnValue = xmlToPatchBodyReader
141 .readFrom(null, null, null, mediaType, null, inputStream);
142 checkPatchContext(returnValue);