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.restconf.common.errors.RestconfDocumentedException;
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 {
30 xmlToPatchBodyReader = 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 CONTROLLER_CONTEXT.setSchemas(schemaContext);
45 public void moduleDataTest() throws Exception {
46 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
47 mockBodyReader(uri, xmlToPatchBodyReader, false);
48 final InputStream inputStream = TestXmlBodyReader.class
49 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
50 final PatchContext returnValue = xmlToPatchBodyReader
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, xmlToPatchBodyReader, false);
62 final InputStream inputStream = TestXmlBodyReader.class
63 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
65 xmlToPatchBodyReader.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, xmlToPatchBodyReader, false);
80 final InputStream inputStream = TestXmlBodyReader.class
81 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
83 xmlToPatchBodyReader.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());
91 * Test of Yang Patch with absolute target path.
94 public void moduleDataAbsoluteTargetPathTest() throws Exception {
95 final String uri = "";
96 mockBodyReader(uri, xmlToPatchBodyReader, false);
97 final InputStream inputStream = TestXmlBodyReader.class
98 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
99 final PatchContext returnValue = xmlToPatchBodyReader
100 .readFrom(null, null, null, mediaType, null, inputStream);
101 checkPatchContext(returnValue);
105 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
108 public void modulePatchCompleteTargetInURITest() throws Exception {
109 final String uri = "instance-identifier-patch-module:patch-cont";
110 mockBodyReader(uri, xmlToPatchBodyReader, false);
111 final InputStream inputStream = TestXmlBodyReader.class
112 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
113 final PatchContext returnValue = xmlToPatchBodyReader
114 .readFrom(null, null, null, mediaType, null, inputStream);
115 checkPatchContext(returnValue);
119 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
122 public void moduleDataMergeOperationOnListTest() throws Exception {
123 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
124 mockBodyReader(uri, xmlToPatchBodyReader, false);
125 final InputStream inputStream = TestXmlBodyReader.class
126 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
127 final PatchContext returnValue = xmlToPatchBodyReader
128 .readFrom(null, null, null, mediaType, null, inputStream);
129 checkPatchContext(returnValue);
133 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
136 public void moduleDataMergeOperationOnContainerTest() throws Exception {
137 final String uri = "instance-identifier-patch-module:patch-cont";
138 mockBodyReader(uri, xmlToPatchBodyReader, false);
139 final InputStream inputStream = TestXmlBodyReader.class
140 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
141 final PatchContext returnValue = xmlToPatchBodyReader
142 .readFrom(null, null, null, mediaType, null, inputStream);
143 checkPatchContext(returnValue);