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.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.controller.sal.rest.impl.test.providers.TestXmlBodyReader;
25 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
26 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 public class XmlPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
32 private final XmlToPatchBodyReader xmlToPatchBodyReader;
33 private static SchemaContext schemaContext;
34 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
36 public XmlPatchBodyReaderMountPointTest() throws Exception {
38 xmlToPatchBodyReader = new XmlToPatchBodyReader();
42 protected MediaType getMediaType() {
43 return new MediaType(MediaType.APPLICATION_XML, null);
47 public static void initialization() {
48 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
50 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
51 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
53 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
54 when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
55 when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
57 CONTROLLER_CONTEXT.setSchemas(schemaContext);
61 public void moduleDataTest() throws Exception {
62 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
63 mockBodyReader(uri, xmlToPatchBodyReader, false);
64 final InputStream inputStream = TestXmlBodyReader.class
65 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdata.xml");
66 final PatchContext returnValue = xmlToPatchBodyReader
67 .readFrom(null, null, null, mediaType, null, inputStream);
68 checkPatchContextMountPoint(returnValue);
72 * Test trying to use Patch create operation which requires value without value. Error code 400 should be returned.
75 public void moduleDataValueMissingNegativeTest() throws Exception {
76 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
77 mockBodyReader(uri, xmlToPatchBodyReader, false);
78 final InputStream inputStream = TestXmlBodyReader.class
79 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueMissing.xml");
81 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
82 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
83 } catch (final RestconfDocumentedException e) {
84 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
89 * Test trying to use value with Patch delete operation which does not support value. Error code 400 should be
93 public void moduleDataNotValueNotSupportedNegativeTest() throws Exception {
94 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
95 mockBodyReader(uri, xmlToPatchBodyReader, false);
96 final InputStream inputStream = TestXmlBodyReader.class
97 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataValueNotSupported.xml");
99 xmlToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
100 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
101 } catch (final RestconfDocumentedException e) {
102 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
108 * Test of Yang Patch with absolute target path.
111 public void moduleDataAbsoluteTargetPathTest() throws Exception {
112 final String uri = MOUNT_POINT;
113 mockBodyReader(uri, xmlToPatchBodyReader, false);
114 final InputStream inputStream = TestXmlBodyReader.class
115 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
116 final PatchContext returnValue = xmlToPatchBodyReader
117 .readFrom(null, null, null, mediaType, null, inputStream);
118 checkPatchContextMountPoint(returnValue);
122 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
125 public void modulePatchCompleteTargetInURITest() throws Exception {
126 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
127 mockBodyReader(uri, xmlToPatchBodyReader, false);
128 final InputStream inputStream = TestXmlBodyReader.class
129 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
130 final PatchContext returnValue = xmlToPatchBodyReader
131 .readFrom(null, null, null, mediaType, null, inputStream);
132 checkPatchContextMountPoint(returnValue);
136 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
139 public void moduleDataMergeOperationOnListTest() throws Exception {
140 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
141 mockBodyReader(uri, xmlToPatchBodyReader, false);
142 final InputStream inputStream = TestXmlBodyReader.class
143 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
144 final PatchContext returnValue = xmlToPatchBodyReader
145 .readFrom(null, null, null, mediaType, null, inputStream);
146 checkPatchContextMountPoint(returnValue);
150 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
153 public void moduleDataMergeOperationOnContainerTest() throws Exception {
154 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
155 mockBodyReader(uri, xmlToPatchBodyReader, false);
156 final InputStream inputStream = TestXmlBodyReader.class
157 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
158 final PatchContext returnValue = xmlToPatchBodyReader
159 .readFrom(null, null, null, mediaType, null, inputStream);
160 checkPatchContextMountPoint(returnValue);