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