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