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.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
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 checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
51 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json")));
55 * Test of successful Patch consisting of create and delete Patch operations.
58 public void modulePatchCreateAndDeleteTest() throws Exception {
59 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
60 mockBodyReader(uri, jsonToPatchBodyReader, false);
62 checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
63 JsonBodyReaderTest.class.getResourceAsStream(
64 "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json")));
68 * Test trying to use Patch create operation which requires value without value. Test should fail with
69 * {@link RestconfDocumentedException} with error code 400.
72 public void modulePatchValueMissingNegativeTest() throws Exception {
73 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
74 mockBodyReader(uri, jsonToPatchBodyReader, false);
76 final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
77 "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
79 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
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");
96 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
97 () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
98 assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
102 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
105 public void modulePatchCompleteTargetInURITest() throws Exception {
106 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
107 mockBodyReader(uri, jsonToPatchBodyReader, false);
109 checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
110 JsonBodyReaderTest.class.getResourceAsStream(
111 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json")));
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 checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
123 JsonBodyReaderTest.class.getResourceAsStream(
124 "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json")));
128 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
131 public void modulePatchMergeOperationOnContainerTest() throws Exception {
132 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
133 mockBodyReader(uri, jsonToPatchBodyReader, false);
135 checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
136 JsonBodyReaderTest.class.getResourceAsStream(
137 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json")));
141 * Test reading simple leaf value.
144 public void modulePatchSimpleLeafValueTest() throws Exception {
145 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
146 mockBodyReader(uri, jsonToPatchBodyReader, false);
148 checkPatchContext(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
149 JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json")));