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.restconf.nb.rfc8040.jersey.providers.patch;
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;
15 import static org.mockito.Mockito.when;
17 import java.io.InputStream;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.common.patch.PatchContext;
24 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
25 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
26 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
31 private final JsonToPatchBodyReader jsonToPatchBodyReader;
32 private static SchemaContext schemaContext;
34 public JsonPatchBodyReaderTest() throws Exception {
35 jsonToPatchBodyReader = new JsonToPatchBodyReader();
39 protected MediaType getMediaType() {
40 return new MediaType(APPLICATION_JSON, null);
44 public static void initialization() {
45 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
46 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
47 SchemaContextHandler.setSchemaContext(schemaContext);
51 public void modulePatchDataTest() throws Exception {
52 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
53 mockBodyReader(uri, jsonToPatchBodyReader, false);
55 final InputStream inputStream = JsonBodyReaderTest.class
56 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
58 final PatchContext returnValue = jsonToPatchBodyReader
59 .readFrom(null, null, null, mediaType, null, inputStream);
60 checkPatchContext(returnValue);
64 * Test of successful Patch consisting of create and delete Patch operations.
67 public void modulePatchCreateAndDeleteTest() throws Exception {
68 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
69 mockBodyReader(uri, jsonToPatchBodyReader, false);
71 final InputStream inputStream = JsonBodyReaderTest.class
72 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
74 final PatchContext returnValue = jsonToPatchBodyReader
75 .readFrom(null, null, null, mediaType, null, inputStream);
76 checkPatchContext(returnValue);
80 * Test trying to use Patch create operation which requires value without value. Test should fail with
81 * {@link RestconfDocumentedException} with error code 400.
84 public void modulePatchValueMissingNegativeTest() throws Exception {
85 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
86 mockBodyReader(uri, jsonToPatchBodyReader, false);
88 final InputStream inputStream = JsonBodyReaderTest.class
89 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
92 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
93 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
94 } catch (final RestconfDocumentedException e) {
95 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
100 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
101 * {@link RestconfDocumentedException} with error code 400.
104 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
105 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
106 mockBodyReader(uri, jsonToPatchBodyReader, false);
108 final InputStream inputStream = JsonBodyReaderTest.class
109 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
112 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
113 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
114 } catch (final RestconfDocumentedException e) {
115 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
120 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
123 public void modulePatchCompleteTargetInURITest() throws Exception {
124 final String uri = "instance-identifier-patch-module:patch-cont";
125 mockBodyReader(uri, jsonToPatchBodyReader, false);
127 final InputStream inputStream = JsonBodyReaderTest.class
128 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
130 final PatchContext returnValue = jsonToPatchBodyReader
131 .readFrom(null, null, null, mediaType, null, inputStream);
132 checkPatchContext(returnValue);
136 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
139 public void modulePatchMergeOperationOnListTest() throws Exception {
140 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
141 mockBodyReader(uri, jsonToPatchBodyReader, false);
143 final InputStream inputStream = JsonBodyReaderTest.class
144 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
146 final PatchContext returnValue = jsonToPatchBodyReader
147 .readFrom(null, null, null, mediaType, null, inputStream);
148 checkPatchContext(returnValue);
152 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
155 public void modulePatchMergeOperationOnContainerTest() throws Exception {
156 final String uri = "instance-identifier-patch-module:patch-cont";
157 mockBodyReader(uri, jsonToPatchBodyReader, false);
159 final InputStream inputStream = JsonBodyReaderTest.class
160 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
162 final PatchContext returnValue = jsonToPatchBodyReader
163 .readFrom(null, null, null, mediaType, null, inputStream);
164 checkPatchContext(returnValue);
168 * Test reading simple leaf value.
171 public void modulePatchSimpleLeafValueTest() throws Exception {
172 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
173 mockBodyReader(uri, jsonToPatchBodyReader, false);
175 final InputStream inputStream =
176 JsonBodyReaderTest.class
177 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
179 final PatchContext returnValue = jsonToPatchBodyReader
180 .readFrom(null, null, null, mediaType, null, inputStream);
181 checkPatchContext(returnValue);