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 javax.ws.rs.core.MediaType.APPLICATION_JSON;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.fail;
14 import static org.mockito.Mockito.mock;
16 import java.io.InputStream;
17 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
21 import org.opendaylight.netconf.sal.rest.impl.JsonToPatchBodyReader;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.common.patch.PatchContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
28 private final JsonToPatchBodyReader jsonToPatchBodyReader;
29 private static SchemaContext schemaContext;
30 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount";
32 public TestJsonPatchBodyReaderMountPoint() {
33 super(schemaContext, mock(DOMMountPoint.class));
34 jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
38 protected MediaType getMediaType() {
39 return new MediaType(APPLICATION_JSON, null);
43 public static void initialization() {
44 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
48 public void modulePatchDataTest() throws Exception {
49 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
50 mockBodyReader(uri, jsonToPatchBodyReader, false);
52 final InputStream inputStream = TestJsonBodyReader.class
53 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
55 final PatchContext returnValue = jsonToPatchBodyReader
56 .readFrom(null, null, null, mediaType, null, inputStream);
57 checkPatchContextMountPoint(returnValue);
61 * Test of successful Patch consisting of create and delete Patch operations.
64 public void modulePatchCreateAndDeleteTest() throws Exception {
65 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
66 mockBodyReader(uri, jsonToPatchBodyReader, false);
68 final InputStream inputStream = TestJsonBodyReader.class
69 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
71 final PatchContext returnValue = jsonToPatchBodyReader
72 .readFrom(null, null, null, mediaType, null, inputStream);
73 checkPatchContextMountPoint(returnValue);
77 * Test trying to use Patch create operation which requires value without value. Test should fail with
78 * {@link RestconfDocumentedException} with error code 400.
81 public void modulePatchValueMissingNegativeTest() throws Exception {
82 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
83 mockBodyReader(uri, jsonToPatchBodyReader, false);
85 final InputStream inputStream = TestJsonBodyReader.class
86 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
89 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
90 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
91 } catch (final RestconfDocumentedException e) {
92 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
97 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
98 * {@link RestconfDocumentedException} with error code 400.
101 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
102 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
103 mockBodyReader(uri, jsonToPatchBodyReader, false);
105 final InputStream inputStream = TestJsonBodyReader.class
106 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
109 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
110 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
111 } catch (final RestconfDocumentedException e) {
112 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
117 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
120 public void modulePatchCompleteTargetInURITest() throws Exception {
121 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
122 mockBodyReader(uri, jsonToPatchBodyReader, false);
124 final InputStream inputStream = TestJsonBodyReader.class
125 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
127 final PatchContext returnValue = jsonToPatchBodyReader
128 .readFrom(null, null, null, mediaType, null, inputStream);
129 checkPatchContextMountPoint(returnValue);
133 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
136 public void modulePatchMergeOperationOnListTest() throws Exception {
137 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
138 mockBodyReader(uri, jsonToPatchBodyReader, false);
140 final InputStream inputStream = TestJsonBodyReader.class
141 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
143 final PatchContext returnValue = jsonToPatchBodyReader
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 modulePatchMergeOperationOnContainerTest() throws Exception {
153 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
154 mockBodyReader(uri, jsonToPatchBodyReader, false);
156 final InputStream inputStream = TestJsonBodyReader.class
157 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
159 final PatchContext returnValue = jsonToPatchBodyReader
160 .readFrom(null, null, null, mediaType, null, inputStream);
161 checkPatchContextMountPoint(returnValue);
165 * Test reading simple leaf value.
168 public void modulePatchSimpleLeafValueTest() throws Exception {
169 final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
170 mockBodyReader(uri, jsonToPatchBodyReader, false);
172 final InputStream inputStream = TestJsonBodyReader.class
173 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
175 final PatchContext returnValue = jsonToPatchBodyReader
176 .readFrom(null, null, null, mediaType, null, inputStream);
177 checkPatchContext(returnValue);