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.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 public class JsonPATCHBodyReaderTest extends AbstractBodyReaderTest {
29 private final JsonToPATCHBodyReader jsonPATCHBodyReader;
30 private static SchemaContext schemaContext;
32 public JsonPATCHBodyReaderTest() throws Exception {
34 jsonPATCHBodyReader = 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(mountPointServiceHandler.get()).thenReturn(mock(DOMMountPointService.class));
46 controllerContext.setSchemas(schemaContext);
50 public void modulePATCHDataTest() throws Exception {
51 final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
52 mockBodyReader(uri, jsonPATCHBodyReader, false);
54 final InputStream inputStream = TestJsonBodyReader.class
55 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
57 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
70 final InputStream inputStream = TestJsonBodyReader.class
71 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
73 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
87 final InputStream inputStream = TestJsonBodyReader.class
88 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
91 jsonPATCHBodyReader.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, jsonPATCHBodyReader, false);
107 final InputStream inputStream = TestJsonBodyReader.class
108 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
111 jsonPATCHBodyReader.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, jsonPATCHBodyReader, false);
126 final InputStream inputStream = TestJsonBodyReader.class
127 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
129 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
142 final InputStream inputStream = TestJsonBodyReader.class
143 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
145 final PATCHContext returnValue = jsonPATCHBodyReader
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, jsonPATCHBodyReader, false);
158 final InputStream inputStream = TestJsonBodyReader.class
159 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
161 final PATCHContext returnValue = jsonPATCHBodyReader
162 .readFrom(null, null, null, mediaType, null, inputStream);
163 checkPATCHContext(returnValue);