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 {
34 xmlToPatchBodyReader = new XmlToPatchBodyReader();
38 protected MediaType getMediaType() {
39 return new MediaType(MediaType.APPLICATION_XML, null);
43 public static void initialization() {
44 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
45 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
46 SchemaContextHandler.setSchemaContext(schemaContext);
50 public void moduleDataTest() throws Exception {
51 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
52 mockBodyReader(uri, xmlToPatchBodyReader, false);
53 final InputStream inputStream = XmlBodyReaderTest.class
54 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
55 final PatchContext returnValue = xmlToPatchBodyReader
56 .readFrom(null, null, null, mediaType, null, inputStream);
57 checkPatchContext(returnValue);
61 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
64 public void moduleDataValueMissingNegativeTest() throws Exception {
65 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
66 mockBodyReader(uri, xmlToPatchBodyReader, false);
67 final InputStream inputStream = XmlBodyReaderTest.class
68 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
70 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
71 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
72 } catch (final RestconfDocumentedException e) {
73 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
78 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
82 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
83 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
84 mockBodyReader(uri, xmlToPatchBodyReader, false);
85 final InputStream inputStream = XmlBodyReaderTest.class
86 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
88 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
89 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
90 } catch (final RestconfDocumentedException e) {
91 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
97 * Test of Yang Patch with absolute target path.
100 public void moduleDataAbsoluteTargetPathTest() throws Exception {
101 final String uri = "";
102 mockBodyReader(uri, xmlToPatchBodyReader, false);
103 final InputStream inputStream = XmlBodyReaderTest.class
104 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
105 final PatchContext returnValue = xmlToPatchBodyReader
106 .readFrom(null, null, null, mediaType, null, inputStream);
107 checkPatchContext(returnValue);
111 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
114 public void modulePatchCompleteTargetInURITest() throws Exception {
115 final String uri = "instance-identifier-patch-module:patch-cont";
116 mockBodyReader(uri, xmlToPatchBodyReader, false);
117 final InputStream inputStream = XmlBodyReaderTest.class
118 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
119 final PatchContext returnValue = xmlToPatchBodyReader
120 .readFrom(null, null, null, mediaType, null, inputStream);
121 checkPatchContext(returnValue);
125 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
128 public void moduleDataMergeOperationOnListTest() throws Exception {
129 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
130 mockBodyReader(uri, xmlToPatchBodyReader, false);
131 final InputStream inputStream = XmlBodyReaderTest.class
132 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
133 final PatchContext returnValue = xmlToPatchBodyReader
134 .readFrom(null, null, null, mediaType, null, inputStream);
135 checkPatchContext(returnValue);
139 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
142 public void moduleDataMergeOperationOnContainerTest() throws Exception {
143 final String uri = "instance-identifier-patch-module:patch-cont";
144 mockBodyReader(uri, xmlToPatchBodyReader, false);
145 final InputStream inputStream = XmlBodyReaderTest.class
146 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
147 final PatchContext returnValue = xmlToPatchBodyReader
148 .readFrom(null, null, null, mediaType, null, inputStream);
149 checkPatchContext(returnValue);