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.jersey.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;
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.controller.sal.rest.impl.test.providers.TestJsonBodyReader;
23 import org.opendaylight.netconf.sal.restconf.impl.PatchContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
29 private final JsonToPatchBodyReader jsonToPatchBodyReader;
30 private static SchemaContext schemaContext;
32 public JsonPatchBodyReaderTest() throws Exception {
34 jsonToPatchBodyReader = new JsonToPatchBodyReader();
38 protected MediaType getMediaType() {
39 return new MediaType(APPLICATION_JSON, null);
43 public static void initialization() {
44 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
45 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mock(DOMMountPointService.class));
46 CONTROLLER_CONTEXT.setSchemas(schemaContext);
50 public void modulePatchDataTest() throws Exception {
51 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
52 mockBodyReader(uri, jsonToPatchBodyReader, false);
54 final InputStream inputStream = TestJsonBodyReader.class
55 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
57 final PatchContext returnValue = jsonToPatchBodyReader
58 .readFrom(null, null, null, mediaType, null, inputStream);
59 checkPatchContext(returnValue);
63 * Test of successful Patch consisting of create and delete Patch operations.
66 public void modulePatchCreateAndDeleteTest() throws Exception {
67 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
68 mockBodyReader(uri, jsonToPatchBodyReader, false);
70 final InputStream inputStream = TestJsonBodyReader.class
71 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
73 final PatchContext returnValue = jsonToPatchBodyReader
74 .readFrom(null, null, null, mediaType, null, inputStream);
75 checkPatchContext(returnValue);
79 * Test trying to use Patch create operation which requires value without value. Test should fail with
80 * {@link RestconfDocumentedException} with error code 400.
83 public void modulePatchValueMissingNegativeTest() throws Exception {
84 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
85 mockBodyReader(uri, jsonToPatchBodyReader, false);
87 final InputStream inputStream = TestJsonBodyReader.class
88 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
91 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
92 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
93 } catch (final RestconfDocumentedException e) {
94 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
99 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
100 * {@link RestconfDocumentedException} with error code 400.
103 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
104 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
105 mockBodyReader(uri, jsonToPatchBodyReader, false);
107 final InputStream inputStream = TestJsonBodyReader.class
108 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
111 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
112 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
113 } catch (final RestconfDocumentedException e) {
114 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
119 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
122 public void modulePatchCompleteTargetInURITest() throws Exception {
123 final String uri = "instance-identifier-patch-module:patch-cont";
124 mockBodyReader(uri, jsonToPatchBodyReader, false);
126 final InputStream inputStream = TestJsonBodyReader.class
127 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
129 final PatchContext returnValue = jsonToPatchBodyReader
130 .readFrom(null, null, null, mediaType, null, inputStream);
131 checkPatchContext(returnValue);
135 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
138 public void modulePatchMergeOperationOnListTest() throws Exception {
139 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
140 mockBodyReader(uri, jsonToPatchBodyReader, false);
142 final InputStream inputStream = TestJsonBodyReader.class
143 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
145 final PatchContext returnValue = jsonToPatchBodyReader
146 .readFrom(null, null, null, mediaType, null, inputStream);
147 checkPatchContext(returnValue);
151 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
154 public void modulePatchMergeOperationOnContainerTest() throws Exception {
155 final String uri = "instance-identifier-patch-module:patch-cont";
156 mockBodyReader(uri, jsonToPatchBodyReader, false);
158 final InputStream inputStream = TestJsonBodyReader.class
159 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
161 final PatchContext returnValue = jsonToPatchBodyReader
162 .readFrom(null, null, null, mediaType, null, inputStream);
163 checkPatchContext(returnValue);
167 * Test reading simple leaf value.
170 public void modulePatchSimpleLeafValueTest() throws Exception {
171 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
172 mockBodyReader(uri, jsonToPatchBodyReader, false);
174 final InputStream inputStream = TestJsonBodyReader.class
175 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
177 final PatchContext returnValue = jsonToPatchBodyReader
178 .readFrom(null, null, null, mediaType, null, inputStream);
179 checkPatchContext(returnValue);