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