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
9 package org.opendaylight.restconf.jersey.providers;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import java.io.InputStream;
17 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
21 import org.opendaylight.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
22 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
28 private final XmlToPatchBodyReader xmlToPatchBodyReader;
29 private static SchemaContext schemaContext;
31 public XmlPatchBodyReaderTest() throws Exception {
33 xmlToPatchBodyReader = new XmlToPatchBodyReader();
37 protected MediaType getMediaType() {
38 return new MediaType(MediaType.APPLICATION_XML, null);
42 public static void initialization() {
43 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
44 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
45 CONTROLLER_CONTEXT.setSchemas(schemaContext);
49 public void moduleDataTest() throws Exception {
50 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
51 mockBodyReader(uri, xmlToPatchBodyReader, false);
52 final InputStream inputStream = TestXmlBodyReader.class
53 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
54 final PatchContext returnValue = xmlToPatchBodyReader
55 .readFrom(null, null, null, mediaType, null, inputStream);
56 checkPatchContext(returnValue);
60 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
63 public void moduleDataValueMissingNegativeTest() throws Exception {
64 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
65 mockBodyReader(uri, xmlToPatchBodyReader, false);
66 final InputStream inputStream = TestXmlBodyReader.class
67 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
69 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
70 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
71 } catch (final RestconfDocumentedException e) {
72 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
77 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
81 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
82 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
83 mockBodyReader(uri, xmlToPatchBodyReader, false);
84 final InputStream inputStream = TestXmlBodyReader.class
85 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
87 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
88 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
89 } catch (final RestconfDocumentedException e) {
90 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
96 * Test of Yang Patch with absolute target path.
99 public void moduleDataAbsoluteTargetPathTest() throws Exception {
100 final String uri = "";
101 mockBodyReader(uri, xmlToPatchBodyReader, false);
102 final InputStream inputStream = TestXmlBodyReader.class
103 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
104 final PatchContext returnValue = xmlToPatchBodyReader
105 .readFrom(null, null, null, mediaType, null, inputStream);
106 checkPatchContext(returnValue);
110 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
113 public void modulePatchCompleteTargetInURITest() throws Exception {
114 final String uri = "instance-identifier-patch-module:patch-cont";
115 mockBodyReader(uri, xmlToPatchBodyReader, false);
116 final InputStream inputStream = TestXmlBodyReader.class
117 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
118 final PatchContext returnValue = xmlToPatchBodyReader
119 .readFrom(null, null, null, mediaType, null, inputStream);
120 checkPatchContext(returnValue);
124 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
127 public void moduleDataMergeOperationOnListTest() throws Exception {
128 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
129 mockBodyReader(uri, xmlToPatchBodyReader, false);
130 final InputStream inputStream = TestXmlBodyReader.class
131 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
132 final PatchContext returnValue = xmlToPatchBodyReader
133 .readFrom(null, null, null, mediaType, null, inputStream);
134 checkPatchContext(returnValue);
138 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
141 public void moduleDataMergeOperationOnContainerTest() throws Exception {
142 final String uri = "instance-identifier-patch-module:patch-cont";
143 mockBodyReader(uri, xmlToPatchBodyReader, false);
144 final InputStream inputStream = TestXmlBodyReader.class
145 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
146 final PatchContext returnValue = xmlToPatchBodyReader
147 .readFrom(null, null, null, mediaType, null, inputStream);
148 checkPatchContext(returnValue);