Use DOMMountPointServiceHandler non-statically
[netconf.git] / restconf / restconf-nb-rfc8040 / 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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
10
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
15 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.patch.PatchContext;
21 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
22 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 public class JsonPatchBodyReaderTest extends AbstractBodyReaderTest {
26
27     private final JsonToPatchBodyReader jsonToPatchBodyReader;
28     private static SchemaContext schemaContext;
29
30     public JsonPatchBodyReaderTest() throws Exception {
31         super(schemaContext);
32         jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler, mountPointServiceHandler);
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
51                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
52
53         final PatchContext returnValue = jsonToPatchBodyReader
54                 .readFrom(null, null, null, mediaType, null, inputStream);
55         checkPatchContext(returnValue);
56     }
57
58     /**
59      * Test of successful Patch consisting of create and delete Patch operations.
60      */
61     @Test
62     public void modulePatchCreateAndDeleteTest() throws Exception {
63         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
64         mockBodyReader(uri, jsonToPatchBodyReader, false);
65
66         final InputStream inputStream = JsonBodyReaderTest.class
67                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
68
69         final PatchContext returnValue = jsonToPatchBodyReader
70                 .readFrom(null, null, null, mediaType, null, inputStream);
71         checkPatchContext(returnValue);
72     }
73
74     /**
75      * Test trying to use Patch create operation which requires value without value. Test should fail with
76      * {@link RestconfDocumentedException} with error code 400.
77      */
78     @Test
79     public void modulePatchValueMissingNegativeTest() throws Exception {
80         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
81         mockBodyReader(uri, jsonToPatchBodyReader, false);
82
83         final InputStream inputStream = JsonBodyReaderTest.class
84                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
85
86         try {
87             jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
88             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
89         } catch (final RestconfDocumentedException e) {
90             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
91         }
92     }
93
94     /**
95      * Test trying to use value with Patch delete operation which does not support value. Test should fail with
96      * {@link RestconfDocumentedException} with error code 400.
97      */
98     @Test
99     public void modulePatchValueNotSupportedNegativeTest() throws Exception {
100         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
101         mockBodyReader(uri, jsonToPatchBodyReader, false);
102
103         final InputStream inputStream = JsonBodyReaderTest.class
104                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
105
106         try {
107             jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
108             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
109         } catch (final RestconfDocumentedException e) {
110             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
111         }
112     }
113
114     /**
115      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
116      */
117     @Test
118     public void modulePatchCompleteTargetInURITest() throws Exception {
119         final String uri = "instance-identifier-patch-module:patch-cont";
120         mockBodyReader(uri, jsonToPatchBodyReader, false);
121
122         final InputStream inputStream = JsonBodyReaderTest.class
123                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
124
125         final PatchContext returnValue = jsonToPatchBodyReader
126                 .readFrom(null, null, null, mediaType, null, inputStream);
127         checkPatchContext(returnValue);
128     }
129
130     /**
131      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
132      */
133     @Test
134     public void modulePatchMergeOperationOnListTest() throws Exception {
135         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
136         mockBodyReader(uri, jsonToPatchBodyReader, false);
137
138         final InputStream inputStream = JsonBodyReaderTest.class
139                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
140
141         final PatchContext returnValue = jsonToPatchBodyReader
142                 .readFrom(null, null, null, mediaType, null, inputStream);
143         checkPatchContext(returnValue);
144     }
145
146     /**
147      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
148      */
149     @Test
150     public void modulePatchMergeOperationOnContainerTest() throws Exception {
151         final String uri = "instance-identifier-patch-module:patch-cont";
152         mockBodyReader(uri, jsonToPatchBodyReader, false);
153
154         final InputStream inputStream = JsonBodyReaderTest.class
155                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
156
157         final PatchContext returnValue = jsonToPatchBodyReader
158                 .readFrom(null, null, null, mediaType, null, inputStream);
159         checkPatchContext(returnValue);
160     }
161
162     /**
163      * Test reading simple leaf value.
164      */
165     @Test
166     public void modulePatchSimpleLeafValueTest() throws Exception {
167         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
168         mockBodyReader(uri, jsonToPatchBodyReader, false);
169
170         final InputStream inputStream =
171                 JsonBodyReaderTest.class
172                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
173
174         final PatchContext returnValue = jsonToPatchBodyReader
175                 .readFrom(null, null, null, mediaType, null, inputStream);
176         checkPatchContext(returnValue);
177     }
178 }