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