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