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.restconf.nb.rfc8040.jersey.providers.patch;
10 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
14 import java.io.InputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.patch.PatchContext;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 public class JsonPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
25 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
26 private static EffectiveModelContext schemaContext;
28 private final JsonPatchBodyReader jsonToPatchBodyReader;
30 public JsonPatchBodyReaderMountPointTest() throws Exception {
32 jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
36 protected MediaType getMediaType() {
37 return new MediaType(APPLICATION_JSON, null);
41 public static void initialization() {
42 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
46 public void modulePatchDataTest() throws Exception {
47 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
48 mockBodyReader(uri, jsonToPatchBodyReader, false);
50 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
51 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json"));
52 checkPatchContextMountPoint(returnValue);
56 * Test of successful Patch consisting of create and delete Patch operations.
59 public void modulePatchCreateAndDeleteTest() throws Exception {
60 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
61 mockBodyReader(uri, jsonToPatchBodyReader, false);
63 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
64 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json"));
65 checkPatchContextMountPoint(returnValue);
69 * Test trying to use Patch create operation which requires value without value. Test should fail with
70 * {@link RestconfDocumentedException} with error code 400.
73 public void modulePatchValueMissingNegativeTest() throws Exception {
74 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
75 mockBodyReader(uri, jsonToPatchBodyReader, false);
77 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
78 "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
79 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81 assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
85 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
86 * {@link RestconfDocumentedException} with error code 400.
89 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
90 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
91 mockBodyReader(uri, jsonToPatchBodyReader, false);
93 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
94 "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
95 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
96 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
97 assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
101 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
104 public void modulePatchCompleteTargetInURITest() throws Exception {
105 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
106 mockBodyReader(uri, jsonToPatchBodyReader, false);
108 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
109 JsonBodyReaderTest.class.getResourceAsStream(
110 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json"));
111 checkPatchContextMountPoint(returnValue);
115 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
118 public void modulePatchMergeOperationOnListTest() throws Exception {
119 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
120 mockBodyReader(uri, jsonToPatchBodyReader, false);
122 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
123 JsonBodyReaderTest.class.getResourceAsStream(
124 "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json"));
125 checkPatchContextMountPoint(returnValue);
129 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
132 public void modulePatchMergeOperationOnContainerTest() throws Exception {
133 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
134 mockBodyReader(uri, jsonToPatchBodyReader, false);
136 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
137 JsonBodyReaderTest.class.getResourceAsStream(
138 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json"));
139 checkPatchContextMountPoint(returnValue);
143 * Test reading simple leaf value.
146 public void modulePatchSimpleLeafValueTest() throws Exception {
147 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
148 mockBodyReader(uri, jsonToPatchBodyReader, false);
150 final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
151 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json"));
152 checkPatchContext(returnValue);