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.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import com.google.common.base.Optional;
18 import java.io.InputStream;
19 import javax.ws.rs.core.MediaType;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.common.patch.PatchContext;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
27 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
33 private final XmlToPatchBodyReader xmlToPatchBodyReader;
34 private static SchemaContext schemaContext;
35 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
37 public XmlPatchBodyReaderMountPointTest() throws Exception {
39 xmlToPatchBodyReader = new XmlToPatchBodyReader(schemaContextHandler);
43 protected MediaType getMediaType() {
44 return new MediaType(MediaType.APPLICATION_XML, null);
48 public static void initialization() {
49 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
51 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
52 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
54 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
55 when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
56 when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
60 public void moduleDataTest() throws Exception {
61 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
62 mockBodyReader(uri, xmlToPatchBodyReader, false);
63 final InputStream inputStream = XmlBodyReaderTest.class
64 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
65 final PatchContext returnValue = xmlToPatchBodyReader
66 .readFrom(null, null, null, mediaType, null, inputStream);
67 checkPatchContextMountPoint(returnValue);
71 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
74 public void moduleDataValueMissingNegativeTest() throws Exception {
75 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
76 mockBodyReader(uri, xmlToPatchBodyReader, false);
77 final InputStream inputStream = XmlBodyReaderTest.class
78 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
80 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
81 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
82 } catch (final RestconfDocumentedException e) {
83 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
88 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
92 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
93 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
94 mockBodyReader(uri, xmlToPatchBodyReader, false);
95 final InputStream inputStream = XmlBodyReaderTest.class
96 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
98 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
99 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
100 } catch (final RestconfDocumentedException e) {
101 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
107 * Test of Yang Patch with absolute target path.
110 public void moduleDataAbsoluteTargetPathTest() throws Exception {
111 final String uri = MOUNT_POINT;
112 mockBodyReader(uri, xmlToPatchBodyReader, false);
113 final InputStream inputStream = XmlBodyReaderTest.class
114 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
115 final PatchContext returnValue = xmlToPatchBodyReader
116 .readFrom(null, null, null, mediaType, null, inputStream);
117 checkPatchContextMountPoint(returnValue);
121 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
124 public void modulePatchCompleteTargetInURITest() throws Exception {
125 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
126 mockBodyReader(uri, xmlToPatchBodyReader, false);
127 final InputStream inputStream = XmlBodyReaderTest.class
128 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
129 final PatchContext returnValue = xmlToPatchBodyReader
130 .readFrom(null, null, null, mediaType, null, inputStream);
131 checkPatchContextMountPoint(returnValue);
135 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
138 public void moduleDataMergeOperationOnListTest() throws Exception {
139 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
140 mockBodyReader(uri, xmlToPatchBodyReader, false);
141 final InputStream inputStream = XmlBodyReaderTest.class
142 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
143 final PatchContext returnValue = xmlToPatchBodyReader
144 .readFrom(null, null, null, mediaType, null, inputStream);
145 checkPatchContextMountPoint(returnValue);
149 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
152 public void moduleDataMergeOperationOnContainerTest() throws Exception {
153 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
154 mockBodyReader(uri, xmlToPatchBodyReader, false);
155 final InputStream inputStream = XmlBodyReaderTest.class
156 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
157 final PatchContext returnValue = xmlToPatchBodyReader
158 .readFrom(null, null, null, mediaType, null, inputStream);
159 checkPatchContextMountPoint(returnValue);