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.fail;
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.model.api.SchemaContext;
25 public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
27 private final JsonToPatchBodyReader jsonToPatchBodyReader;
28 private static SchemaContext 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
85 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
88 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
89 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
90 } catch (final RestconfDocumentedException e) {
91 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
96 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
97 * {@link RestconfDocumentedException} with error code 400.
100 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
101 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
102 mockBodyReader(uri, jsonToPatchBodyReader, false);
104 final InputStream inputStream = TestJsonBodyReader.class
105 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
108 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
109 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
110 } catch (final RestconfDocumentedException e) {
111 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
116 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
119 public void modulePatchCompleteTargetInURITest() throws Exception {
120 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
121 mockBodyReader(uri, jsonToPatchBodyReader, false);
123 final InputStream inputStream = TestJsonBodyReader.class
124 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
126 final PatchContext returnValue = jsonToPatchBodyReader
127 .readFrom(null, null, null, mediaType, null, inputStream);
128 checkPatchContextMountPoint(returnValue);
132 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
135 public void modulePatchMergeOperationOnListTest() throws Exception {
136 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
137 mockBodyReader(uri, jsonToPatchBodyReader, false);
139 final InputStream inputStream = TestJsonBodyReader.class
140 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
142 final PatchContext returnValue = jsonToPatchBodyReader
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 modulePatchMergeOperationOnContainerTest() throws Exception {
152 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
153 mockBodyReader(uri, jsonToPatchBodyReader, false);
155 final InputStream inputStream = TestJsonBodyReader.class
156 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
158 final PatchContext returnValue = jsonToPatchBodyReader
159 .readFrom(null, null, null, mediaType, null, inputStream);
160 checkPatchContextMountPoint(returnValue);
164 * Test reading simple leaf value.
167 public void modulePatchSimpleLeafValueTest() throws Exception {
168 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
169 mockBodyReader(uri, jsonToPatchBodyReader, false);
171 final InputStream inputStream = TestJsonBodyReader.class
172 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
174 final PatchContext returnValue = jsonToPatchBodyReader
175 .readFrom(null, null, null, mediaType, null, inputStream);
176 checkPatchContext(returnValue);