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.netconf.sal.restconf.impl.PATCHContext;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 public class TestXmlPATCHBodyReader extends AbstractBodyReaderTest {
25 private final XmlToPATCHBodyReader xmlPATCHBodyReader;
26 private static SchemaContext schemaContext;
28 public TestXmlPATCHBodyReader() throws NoSuchFieldException, SecurityException {
30 xmlPATCHBodyReader = new XmlToPATCHBodyReader();
34 protected MediaType getMediaType() {
35 return new MediaType(MediaType.APPLICATION_XML, null);
39 public static void initialization() throws NoSuchFieldException, SecurityException {
40 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
41 controllerContext.setSchemas(schemaContext);
45 public void moduleDataTest() throws Exception {
46 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
47 mockBodyReader(uri, xmlPATCHBodyReader, false);
48 final InputStream inputStream = TestXmlBodyReader.class
49 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
50 final PATCHContext returnValue = xmlPATCHBodyReader
51 .readFrom(null, null, null, mediaType, null, inputStream);
52 checkPATCHContext(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 = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
61 mockBodyReader(uri, xmlPATCHBodyReader, false);
62 final InputStream inputStream = TestXmlBodyReader.class
63 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
65 xmlPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
66 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
67 } catch (final RestconfDocumentedException e) {
68 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
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 = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
79 mockBodyReader(uri, xmlPATCHBodyReader, false);
80 final InputStream inputStream = TestXmlBodyReader.class
81 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
83 xmlPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
84 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
85 } catch (final RestconfDocumentedException e) {
86 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
92 * Test of Yang PATCH with absolute target path.
95 public void moduleDataAbsoluteTargetPathTest() throws Exception {
96 final String uri = "";
97 mockBodyReader(uri, xmlPATCHBodyReader, false);
98 final InputStream inputStream = TestXmlBodyReader.class
99 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
100 final PATCHContext returnValue = xmlPATCHBodyReader
101 .readFrom(null, null, null, mediaType, null, inputStream);
102 checkPATCHContext(returnValue);
106 * Test using PATCH when target is completely specified in request URI and thus target leaf contains only '/' sign.
109 public void modulePATCHCompleteTargetInURITest() throws Exception {
110 final String uri = "instance-identifier-patch-module:patch-cont";
111 mockBodyReader(uri, xmlPATCHBodyReader, false);
112 final InputStream inputStream = TestXmlBodyReader.class
113 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
114 final PATCHContext returnValue = xmlPATCHBodyReader
115 .readFrom(null, null, null, mediaType, null, inputStream);
116 checkPATCHContext(returnValue);
120 * Test of Yang PATCH merge operation on list. Test consists of two edit operations - replace and merge.
123 public void moduleDataMergeOperationOnListTest() throws Exception {
124 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
125 mockBodyReader(uri, xmlPATCHBodyReader, false);
126 final InputStream inputStream = TestXmlBodyReader.class
127 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
128 final PATCHContext returnValue = xmlPATCHBodyReader
129 .readFrom(null, null, null, mediaType, null, inputStream);
130 checkPATCHContext(returnValue);
134 * Test of Yang PATCH merge operation on container. Test consists of two edit operations - create and merge.
137 public void moduleDataMergeOperationOnContainerTest() throws Exception {
138 final String uri = "instance-identifier-patch-module:patch-cont";
139 mockBodyReader(uri, xmlPATCHBodyReader, false);
140 final InputStream inputStream = TestXmlBodyReader.class
141 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
142 final PATCHContext returnValue = xmlPATCHBodyReader
143 .readFrom(null, null, null, mediaType, null, inputStream);
144 checkPATCHContext(returnValue);