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.nb.rfc8040.jersey.providers.patch;
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.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.patch.PatchContext;
23 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
24 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
25 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 public class XmlPatchBodyReaderTest extends AbstractBodyReaderTest {
30 private final XmlToPatchBodyReader xmlToPatchBodyReader;
31 private static SchemaContext schemaContext;
33 public XmlPatchBodyReaderTest() throws Exception {
35 xmlToPatchBodyReader = new XmlToPatchBodyReader();
39 protected MediaType getMediaType() {
40 return new MediaType(MediaType.APPLICATION_XML, null);
44 public static void initialization() {
45 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
46 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
47 SchemaContextHandler.setActualSchemaContext(schemaContext);
51 public void moduleDataTest() throws Exception {
52 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
53 mockBodyReader(uri, xmlToPatchBodyReader, false);
54 final InputStream inputStream = XmlBodyReaderTest.class
55 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
56 final PatchContext returnValue = xmlToPatchBodyReader
57 .readFrom(null, null, null, mediaType, null, inputStream);
58 checkPatchContext(returnValue);
62 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
65 public void moduleDataValueMissingNegativeTest() throws Exception {
66 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
67 mockBodyReader(uri, xmlToPatchBodyReader, false);
68 final InputStream inputStream = XmlBodyReaderTest.class
69 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
71 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
72 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
73 } catch (final RestconfDocumentedException e) {
74 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
79 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
83 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
84 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
85 mockBodyReader(uri, xmlToPatchBodyReader, false);
86 final InputStream inputStream = XmlBodyReaderTest.class
87 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
89 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
90 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
91 } catch (final RestconfDocumentedException e) {
92 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
98 * Test of Yang Patch with absolute target path.
101 public void moduleDataAbsoluteTargetPathTest() throws Exception {
102 final String uri = "";
103 mockBodyReader(uri, xmlToPatchBodyReader, false);
104 final InputStream inputStream = XmlBodyReaderTest.class
105 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
106 final PatchContext returnValue = xmlToPatchBodyReader
107 .readFrom(null, null, null, mediaType, null, inputStream);
108 checkPatchContext(returnValue);
112 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
115 public void modulePatchCompleteTargetInURITest() throws Exception {
116 final String uri = "instance-identifier-patch-module:patch-cont";
117 mockBodyReader(uri, xmlToPatchBodyReader, false);
118 final InputStream inputStream = XmlBodyReaderTest.class
119 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
120 final PatchContext returnValue = xmlToPatchBodyReader
121 .readFrom(null, null, null, mediaType, null, inputStream);
122 checkPatchContext(returnValue);
126 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
129 public void moduleDataMergeOperationOnListTest() throws Exception {
130 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
131 mockBodyReader(uri, xmlToPatchBodyReader, false);
132 final InputStream inputStream = XmlBodyReaderTest.class
133 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
134 final PatchContext returnValue = xmlToPatchBodyReader
135 .readFrom(null, null, null, mediaType, null, inputStream);
136 checkPatchContext(returnValue);
140 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
143 public void moduleDataMergeOperationOnContainerTest() throws Exception {
144 final String uri = "instance-identifier-patch-module:patch-cont";
145 mockBodyReader(uri, xmlToPatchBodyReader, false);
146 final InputStream inputStream = XmlBodyReaderTest.class
147 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
148 final PatchContext returnValue = xmlToPatchBodyReader
149 .readFrom(null, null, null, mediaType, null, inputStream);
150 checkPatchContext(returnValue);