505a4b41cc82fb250b85a2f11883f3377da3b1f9
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.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 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         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
51             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json"));
52         checkPatchContextMountPoint(returnValue);
53     }
54
55     /**
56      * Test of successful Patch consisting of create and delete Patch operations.
57      */
58     @Test
59     public void modulePatchCreateAndDeleteTest() throws Exception {
60         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
61         mockBodyReader(uri, jsonToPatchBodyReader, false);
62
63         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
64             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json"));
65         checkPatchContextMountPoint(returnValue);
66     }
67
68     /**
69      * Test trying to use Patch create operation which requires value without value. Test should fail with
70      * {@link RestconfDocumentedException} with error code 400.
71      */
72     @Test
73     public void modulePatchValueMissingNegativeTest() throws Exception {
74         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
75         mockBodyReader(uri, jsonToPatchBodyReader, false);
76
77         final InputStream inputStream = JsonBodyReaderTest.class.getResourceAsStream(
78             "/instanceidentifier/json/jsonPATCHdataValueMissing.json");
79         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
80             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
81         assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
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         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
96             () -> jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream));
97         assertEquals("Error code 400 expected", 400, ex.getErrors().get(0).getErrorTag().getStatusCode());
98     }
99
100     /**
101      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
102      */
103     @Test
104     public void modulePatchCompleteTargetInURITest() throws Exception {
105         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
106         mockBodyReader(uri, jsonToPatchBodyReader, false);
107
108         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
109             JsonBodyReaderTest.class.getResourceAsStream(
110                 "/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json"));
111         checkPatchContextMountPoint(returnValue);
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         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
123             JsonBodyReaderTest.class.getResourceAsStream(
124                 "/instanceidentifier/json/jsonPATCHMergeOperationOnList.json"));
125         checkPatchContextMountPoint(returnValue);
126     }
127
128     /**
129      * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
130      */
131     @Test
132     public void modulePatchMergeOperationOnContainerTest() throws Exception {
133         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
134         mockBodyReader(uri, jsonToPatchBodyReader, false);
135
136         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
137             JsonBodyReaderTest.class.getResourceAsStream(
138                 "/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json"));
139         checkPatchContextMountPoint(returnValue);
140     }
141
142     /**
143      * Test reading simple leaf value.
144      */
145     @Test
146     public void modulePatchSimpleLeafValueTest() throws Exception {
147         final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
148         mockBodyReader(uri, jsonToPatchBodyReader, false);
149
150         final PatchContext returnValue = jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null,
151             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json"));
152         checkPatchContext(returnValue);
153     }
154 }