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 {
36 jsonToPatchBodyReader = new JsonToPatchBodyReader();
40 protected MediaType getMediaType() {
41 return new MediaType(APPLICATION_JSON, null);
45 public static void initialization() {
46 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
47 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
48 SchemaContextHandler.setActualSchemaContext(schemaContext);
52 public void modulePatchDataTest() throws Exception {
53 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
54 mockBodyReader(uri, jsonToPatchBodyReader, false);
56 final InputStream inputStream = JsonBodyReaderTest.class
57 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
59 final PatchContext returnValue = jsonToPatchBodyReader
60 .readFrom(null, null, null, mediaType, null, inputStream);
61 checkPatchContext(returnValue);
65 * Test of successful Patch consisting of create and delete Patch operations.
68 public void modulePatchCreateAndDeleteTest() throws Exception {
69 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
70 mockBodyReader(uri, jsonToPatchBodyReader, false);
72 final InputStream inputStream = JsonBodyReaderTest.class
73 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
75 final PatchContext returnValue = jsonToPatchBodyReader
76 .readFrom(null, null, null, mediaType, null, inputStream);
77 checkPatchContext(returnValue);
81 * Test trying to use Patch create operation which requires value without value. Test should fail with
82 * {@link RestconfDocumentedException} with error code 400.
85 public void modulePatchValueMissingNegativeTest() throws Exception {
86 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
87 mockBodyReader(uri, jsonToPatchBodyReader, false);
89 final InputStream inputStream = JsonBodyReaderTest.class
90 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
93 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
94 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
95 } catch (final RestconfDocumentedException e) {
96 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
101 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
102 * {@link RestconfDocumentedException} with error code 400.
105 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
106 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
107 mockBodyReader(uri, jsonToPatchBodyReader, false);
109 final InputStream inputStream = JsonBodyReaderTest.class
110 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
113 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
114 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
115 } catch (final RestconfDocumentedException e) {
116 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
121 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
124 public void modulePatchCompleteTargetInURITest() throws Exception {
125 final String uri = "instance-identifier-patch-module:patch-cont";
126 mockBodyReader(uri, jsonToPatchBodyReader, false);
128 final InputStream inputStream = JsonBodyReaderTest.class
129 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
131 final PatchContext returnValue = jsonToPatchBodyReader
132 .readFrom(null, null, null, mediaType, null, inputStream);
133 checkPatchContext(returnValue);
137 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
140 public void modulePatchMergeOperationOnListTest() throws Exception {
141 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
142 mockBodyReader(uri, jsonToPatchBodyReader, false);
144 final InputStream inputStream = JsonBodyReaderTest.class
145 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
147 final PatchContext returnValue = jsonToPatchBodyReader
148 .readFrom(null, null, null, mediaType, null, inputStream);
149 checkPatchContext(returnValue);
153 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
156 public void modulePatchMergeOperationOnContainerTest() throws Exception {
157 final String uri = "instance-identifier-patch-module:patch-cont";
158 mockBodyReader(uri, jsonToPatchBodyReader, false);
160 final InputStream inputStream = JsonBodyReaderTest.class
161 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
163 final PatchContext returnValue = jsonToPatchBodyReader
164 .readFrom(null, null, null, mediaType, null, inputStream);
165 checkPatchContext(returnValue);
169 * Test reading simple leaf value.
172 public void modulePatchSimpleLeafValueTest() throws Exception {
173 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
174 mockBodyReader(uri, jsonToPatchBodyReader, false);
176 final InputStream inputStream =
177 JsonBodyReaderTest.class
178 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
180 final PatchContext returnValue = jsonToPatchBodyReader
181 .readFrom(null, null, null, mediaType, null, inputStream);
182 checkPatchContext(returnValue);