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