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.controller.sal.rest.impl.test.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.netconf.sal.rest.impl.XmlToPatchBodyReader;
25 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
26 import org.opendaylight.restconf.common.patch.PatchContext;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 public class TestXmlPatchBodyReaderMountPoint 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 TestXmlPatchBodyReaderMountPoint() throws NoSuchFieldException, SecurityException {
37 xmlToPatchBodyReader = new XmlToPatchBodyReader();
41 protected MediaType getMediaType() {
42 return new MediaType(MediaType.APPLICATION_XML, null);
46 public static void initialization() throws NoSuchFieldException, SecurityException {
47 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
49 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
50 when(mockMountPoint.getSchemaContext()).thenReturn(schemaContext);
51 final DOMMountPointService mockMountPointService = mock(DOMMountPointService.class);
52 when(mockMountPointService.getMountPoint(any(YangInstanceIdentifier.class)))
53 .thenReturn(Optional.of(mockMountPoint));
55 CONTROLLER_CONTEXT.setMountService(mockMountPointService);
56 CONTROLLER_CONTEXT.setSchemas(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 = TestXmlBodyReader.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 = TestXmlBodyReader.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 = TestXmlBodyReader.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());
106 * Test of Yang Patch with absolute target path.
109 public void moduleDataAbsoluteTargetPathTest() throws Exception {
110 final String uri = MOUNT_POINT;
111 mockBodyReader(uri, xmlToPatchBodyReader, false);
112 final InputStream inputStream = TestXmlBodyReader.class
113 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataAbsoluteTargetPath.xml");
114 final PatchContext returnValue = xmlToPatchBodyReader
115 .readFrom(null, null, null, mediaType, null, inputStream);
116 checkPatchContextMountPoint(returnValue);
120 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
123 public void modulePatchCompleteTargetInURITest() throws Exception {
124 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
125 mockBodyReader(uri, xmlToPatchBodyReader, false);
126 final InputStream inputStream = TestXmlBodyReader.class
127 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataCompleteTargetInURI.xml");
128 final PatchContext returnValue = xmlToPatchBodyReader
129 .readFrom(null, null, null, mediaType, null, inputStream);
130 checkPatchContextMountPoint(returnValue);
134 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
137 public void moduleDataMergeOperationOnListTest() throws Exception {
138 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
139 mockBodyReader(uri, xmlToPatchBodyReader, false);
140 final InputStream inputStream = TestXmlBodyReader.class
141 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnList.xml");
142 final PatchContext returnValue = xmlToPatchBodyReader
143 .readFrom(null, null, null, mediaType, null, inputStream);
144 checkPatchContextMountPoint(returnValue);
148 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
151 public void moduleDataMergeOperationOnContainerTest() throws Exception {
152 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
153 mockBodyReader(uri, xmlToPatchBodyReader, false);
154 final InputStream inputStream = TestXmlBodyReader.class
155 .getResourceAsStream("/instanceidentifier/xml/xmlPATCHdataMergeOperationOnContainer.xml");
156 final PatchContext returnValue = xmlToPatchBodyReader
157 .readFrom(null, null, null, mediaType, null, inputStream);
158 checkPatchContextMountPoint(returnValue);