d1c23ed6bd209fed3744a99e59a7c7ee2c3e265d
[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.fail;
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.model.api.EffectiveModelContext;
22
23 public class TestJsonPatchBodyReader extends AbstractBodyReaderTest {
24
25     private final JsonToPatchBodyReader jsonToPatchBodyReader;
26     private static EffectiveModelContext schemaContext;
27
28     public TestJsonPatchBodyReader() {
29         super(schemaContext, null);
30         jsonToPatchBodyReader = new JsonToPatchBodyReader(controllerContext);
31     }
32
33     @Override
34     protected MediaType getMediaType() {
35         return new MediaType(APPLICATION_JSON, null);
36     }
37
38     @BeforeClass
39     public static void initialization() {
40         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
41     }
42
43     @Test
44     public void modulePatchDataTest() throws Exception {
45         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
46         mockBodyReader(uri, jsonToPatchBodyReader, false);
47
48         final InputStream inputStream = TestJsonBodyReader.class
49                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
50
51         final PatchContext returnValue = jsonToPatchBodyReader
52                 .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
65                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
66
67         final PatchContext returnValue = jsonToPatchBodyReader
68                 .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 = TestJsonBodyReader.class
82                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
83
84         try {
85             jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
86             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
87         } catch (final RestconfDocumentedException e) {
88             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
89         }
90     }
91
92     /**
93      * Test trying to use value with Patch delete operation which does not support value. Test should fail with
94      * {@link RestconfDocumentedException} with error code 400.
95      */
96     @Test
97     public void modulePatchValueNotSupportedNegativeTest() throws Exception {
98         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
99         mockBodyReader(uri, jsonToPatchBodyReader, false);
100
101         final InputStream inputStream = TestJsonBodyReader.class
102                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
103
104         try {
105             jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
106             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
107         } catch (final RestconfDocumentedException e) {
108             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
109         }
110     }
111
112     /**
113      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
114      */
115     @Test
116     public void modulePatchCompleteTargetInURITest() throws Exception {
117         final String uri = "instance-identifier-patch-module:patch-cont";
118         mockBodyReader(uri, jsonToPatchBodyReader, false);
119
120         final InputStream inputStream = TestJsonBodyReader.class
121                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
122
123         final PatchContext returnValue = jsonToPatchBodyReader
124                 .readFrom(null, null, null, mediaType, null, inputStream);
125         checkPatchContext(returnValue);
126     }
127
128     /**
129      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
130      */
131     @Test
132     public void modulePatchMergeOperationOnListTest() throws Exception {
133         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
134         mockBodyReader(uri, jsonToPatchBodyReader, false);
135
136         final InputStream inputStream = TestJsonBodyReader.class
137                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
138
139         final PatchContext returnValue = jsonToPatchBodyReader
140                 .readFrom(null, null, null, mediaType, null, inputStream);
141         checkPatchContext(returnValue);
142     }
143
144     /**
145      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
146      */
147     @Test
148     public void modulePatchMergeOperationOnContainerTest() throws Exception {
149         final String uri = "instance-identifier-patch-module:patch-cont";
150         mockBodyReader(uri, jsonToPatchBodyReader, false);
151
152         final InputStream inputStream = TestJsonBodyReader.class
153                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
154
155         final PatchContext returnValue = jsonToPatchBodyReader
156                 .readFrom(null, null, null, mediaType, null, inputStream);
157         checkPatchContext(returnValue);
158     }
159
160     /**
161      * Test reading simple leaf value.
162      */
163     @Test
164     public void modulePatchSimpleLeafValueTest() throws Exception {
165         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
166         mockBodyReader(uri, jsonToPatchBodyReader, false);
167
168         final InputStream inputStream = TestJsonBodyReader.class
169                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
170
171         final PatchContext returnValue = jsonToPatchBodyReader
172                 .readFrom(null, null, null, mediaType, null, inputStream);
173         checkPatchContext(returnValue);
174     }
175 }