Reduce exception guard
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / JsonPatchBodyReaderTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
9
10 import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13
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.common.patch.PatchContext;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24
25 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
26
27     private final JsonPatchBodyReader jsonToPatchBodyReader;
28     private static EffectiveModelContext schemaContext;
29
30     public JsonPatchBodyReaderTest() throws Exception {
31         super(schemaContext);
32         jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
33     }
34
35     @Override
36     protected MediaType getMediaType() {
37         return new MediaType(APPLICATION_JSON, null);
38     }
39
40     @BeforeClass
41     public static void initialization() {
42         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
43     }
44
45     @Test
46     public void modulePatchDataTest() throws Exception {
47         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
48         mockBodyReader(uri, jsonToPatchBodyReader, false);
49
50         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
51             "/instanceidentifier/json/jsonPATCHdata.json");
52
53         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
54         checkPatchContext(returnValue);
55     }
56
57     /**
58      * Test of successful Patch consisting of create and delete Patch operations.
59      */
60     @Test
61     public void modulePatchCreateAndDeleteTest() throws Exception {
62         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
63         mockBodyReader(uri, jsonToPatchBodyReader, false);
64
65         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
66             "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
67
68         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
69         checkPatchContext(returnValue);
70     }
71
72     /**
73      * Test trying to use Patch create operation which requires value without value. Test should fail with
74      * {@link RestconfDocumentedException} with error code 400.
75      */
76     @Test
77     public void modulePatchValueMissingNegativeTest() throws Exception {
78         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
79         mockBodyReader(uri, jsonToPatchBodyReader, false);
80
81         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
82             "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
83
84         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
85             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
86         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
87     }
88
89     /**
90      * Test trying to use value with Patch delete operation which does not support value. Test should fail with
91      * {@link RestconfDocumentedException} with error code 400.
92      */
93     @Test
94     public void modulePatchValueNotSupportedNegativeTest() throws Exception {
95         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
96         mockBodyReader(uri, jsonToPatchBodyReader, false);
97
98         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
99             "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
100
101         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
102             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
103         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
104     }
105
106     /**
107      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
108      */
109     @Test
110     public void modulePatchCompleteTargetInURITest() throws Exception {
111         final String uri = "instance-identifier-patch-module:patch-cont";
112         mockBodyReader(uri, jsonToPatchBodyReader, false);
113
114         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
115             "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
116
117         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
118         checkPatchContext(returnValue);
119     }
120
121     /**
122      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
123      */
124     @Test
125     public void modulePatchMergeOperationOnListTest() throws Exception {
126         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
127         mockBodyReader(uri, jsonToPatchBodyReader, false);
128
129         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
130             "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
131
132         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
133         checkPatchContext(returnValue);
134     }
135
136     /**
137      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
138      */
139     @Test
140     public void modulePatchMergeOperationOnContainerTest() throws Exception {
141         final String uri = "instance-identifier-patch-module:patch-cont";
142         mockBodyReader(uri, jsonToPatchBodyReader, false);
143
144         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
145             "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
146
147         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
148         checkPatchContext(returnValue);
149     }
150
151     /**
152      * Test reading simple leaf value.
153      */
154     @Test
155     public void modulePatchSimpleLeafValueTest() throws Exception {
156         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
157         mockBodyReader(uri, jsonToPatchBodyReader, false);
158
159         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
160             "/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
161
162         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
163         checkPatchContext(returnValue);
164     }
165
166     /**
167      * Test of Yang Patch on the top-level container with empty URI for data root.
168      */
169     @Test
170     public void modulePatchTargetTopLevelContainerWithEmptyURITest() throws Exception {
171         final String uri = "";
172         mockBodyReader(uri, jsonToPatchBodyReader, false);
173
174         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
175                 "/instanceidentifier/json/jsonPATCHTargetTopLevelContainerWithEmptyURI.json");
176
177         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
178         checkPatchContext(returnValue);
179     }
180 }