Reduce exception guard
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / patch / JsonPatchBodyReaderMountPointTest.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.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.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
20 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
21 import org.opendaylight.yangtools.yang.common.ErrorTag;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23
24 public class JsonPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
25     private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
26     private static EffectiveModelContext schemaContext;
27
28     private final JsonPatchBodyReader jsonToPatchBodyReader;
29
30     public JsonPatchBodyReaderMountPointTest() throws Exception {
31         super(schemaContext);
32         jsonToPatchBodyReader = new JsonPatchBodyReader(schemaContextHandler, mountPointService);
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 = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
48         mockBodyReader(uri, jsonToPatchBodyReader, false);
49
50         checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
51             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json")));
52     }
53
54     /**
55      * Test of successful Patch consisting of create and delete Patch operations.
56      */
57     @Test
58     public void modulePatchCreateAndDeleteTest() throws Exception {
59         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
60         mockBodyReader(uri, jsonToPatchBodyReader, false);
61
62         checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
63             JsonBodyReaderTest.class.getResourceAsStream(
64                 "/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json")));
65     }
66
67     /**
68      * Test trying to use Patch create operation which requires value without value. Test should fail with
69      * {@link RestconfDocumentedException} with error code 400.
70      */
71     @Test
72     public void modulePatchValueMissingNegativeTest() throws Exception {
73         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
74         mockBodyReader(uri, jsonToPatchBodyReader, false);
75
76         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
77             "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
78
79         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
82     }
83
84     /**
85      * Test trying to use value with Patch delete operation which does not support value. Test should fail with
86      * {@link RestconfDocumentedException} with error code 400.
87      */
88     @Test
89     public void modulePatchValueNotSupportedNegativeTest() throws Exception {
90         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
91         mockBodyReader(uri, jsonToPatchBodyReader, false);
92
93         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
94             "/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
95
96         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
97             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
98         assertEquals(ErrorTag.MALFORMED_MESSAGE, ex.getErrors().get(0).getErrorTag());
99     }
100
101     /**
102      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
103      */
104     @Test
105     public void modulePatchCompleteTargetInURITest() throws Exception {
106         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
107         mockBodyReader(uri, jsonToPatchBodyReader, false);
108
109         checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
110             JsonBodyReaderTest.class.getResourceAsStream(
111                 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json")));
112     }
113
114     /**
115      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
116      */
117     @Test
118     public void modulePatchMergeOperationOnListTest() throws Exception {
119         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
120         mockBodyReader(uri, jsonToPatchBodyReader, false);
121
122         checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
123             JsonBodyReaderTest.class.getResourceAsStream(
124                 "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json")));
125     }
126
127     /**
128      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
129      */
130     @Test
131     public void modulePatchMergeOperationOnContainerTest() throws Exception {
132         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
133         mockBodyReader(uri, jsonToPatchBodyReader, false);
134
135         checkPatchContextMountPoint(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
136             JsonBodyReaderTest.class.getResourceAsStream(
137                 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json")));
138     }
139
140     /**
141      * Test reading simple leaf value.
142      */
143     @Test
144     public void modulePatchSimpleLeafValueTest() throws Exception {
145         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
146         mockBodyReader(uri, jsonToPatchBodyReader, false);
147
148         checkPatchContext(jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
149             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json")));
150     }
151 }