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
8 package org.opendaylight.controller.sal.rest.impl.test.providers;
10 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.Mockito.mock;
15 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
20 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.common.patch.PatchContext;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
27 private final JsonToPatchBodyReader jsonToPatchBodyReader;
28 private static EffectiveModelContext schemaContext;
29 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount";
31 public TestJsonPatchBodyReaderMountPoint() {
32 super(schemaContext, mock(DOMMountPoint.class));
33 jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
37 protected MediaType getMediaType() {
38 return new MediaType(APPLICATION_JSON, null);
42 public static void initialization() {
43 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
47 public void modulePatchDataTest() throws Exception {
48 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
49 mockBodyReader(uri, jsonToPatchBodyReader, false);
51 final InputStream inputStream = TestJsonBodyReader.class
52 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
54 final PatchContext returnValue = jsonToPatchBodyReader
55 .readFrom(null, null, null, mediaType, null, inputStream);
56 checkPatchContextMountPoint(returnValue);
60 * Test of successful Patch consisting of create and delete Patch operations.
63 public void modulePatchCreateAndDeleteTest() throws Exception {
64 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
65 mockBodyReader(uri, jsonToPatchBodyReader, false);
67 final InputStream inputStream = TestJsonBodyReader.class
68 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
70 final PatchContext returnValue = jsonToPatchBodyReader
71 .readFrom(null, null, null, mediaType, null, inputStream);
72 checkPatchContextMountPoint(returnValue);
76 * Test trying to use Patch create operation which requires value without value. Test should fail with
77 * {@link RestconfDocumentedException} with error code 400.
80 public void modulePatchValueMissingNegativeTest() throws Exception {
81 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
82 mockBodyReader(uri, jsonToPatchBodyReader, false);
84 final InputStream inputStream = TestJsonBodyReader.class.getResourceAsStream(
85 "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
87 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
88 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
89 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
93 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
94 * {@link RestconfDocumentedException} with error code 400.
97 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
98 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
99 mockBodyReader(uri, jsonToPatchBodyReader, false);
101 final InputStream inputStream = TestJsonBodyReader.class
102 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
104 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
105 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
106 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
110 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
113 public void modulePatchCompleteTargetInURITest() throws Exception {
114 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
115 mockBodyReader(uri, jsonToPatchBodyReader, false);
117 final InputStream inputStream = TestJsonBodyReader.class
118 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
120 final PatchContext returnValue = jsonToPatchBodyReader
121 .readFrom(null, null, null, mediaType, null, inputStream);
122 checkPatchContextMountPoint(returnValue);
126 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
129 public void modulePatchMergeOperationOnListTest() throws Exception {
130 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
131 mockBodyReader(uri, jsonToPatchBodyReader, false);
133 final InputStream inputStream = TestJsonBodyReader.class
134 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
136 final PatchContext returnValue = jsonToPatchBodyReader
137 .readFrom(null, null, null, mediaType, null, inputStream);
138 checkPatchContextMountPoint(returnValue);
142 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
145 public void modulePatchMergeOperationOnContainerTest() throws Exception {
146 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
147 mockBodyReader(uri, jsonToPatchBodyReader, false);
149 final InputStream inputStream = TestJsonBodyReader.class
150 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
152 final PatchContext returnValue = jsonToPatchBodyReader
153 .readFrom(null, null, null, mediaType, null, inputStream);
154 checkPatchContextMountPoint(returnValue);
158 * Test reading simple leaf value.
161 public void modulePatchSimpleLeafValueTest() throws Exception {
162 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
163 mockBodyReader(uri, jsonToPatchBodyReader, false);
165 final InputStream inputStream = TestJsonBodyReader.class
166 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
168 final PatchContext returnValue = jsonToPatchBodyReader
169 .readFrom(null, null, null, mediaType, null, inputStream);
170 checkPatchContext(returnValue);