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