d316bcd9e2836b650314c592141d67ac31833a91
[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.fail;
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.model.api.SchemaContext;
24
25 public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
26
27     private final JsonToPatchBodyReader jsonToPatchBodyReader;
28     private static SchemaContext 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
85                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
86
87         try {
88             jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
89             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
90         } catch (final RestconfDocumentedException e) {
91             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
92         }
93     }
94
95     /**
96      * Test trying to use value with Patch delete operation which does not support value. Test should fail with
97      * {@link RestconfDocumentedException} with error code 400.
98      */
99     @Test
100     public void modulePatchValueNotSupportedNegativeTest() throws Exception {
101         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
102         mockBodyReader(uri, jsonToPatchBodyReader, false);
103
104         final InputStream inputStream = TestJsonBodyReader.class
105                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
106
107         try {
108             jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
109             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
110         } catch (final RestconfDocumentedException e) {
111             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
112         }
113     }
114
115     /**
116      * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
117      */
118     @Test
119     public void modulePatchCompleteTargetInURITest() throws Exception {
120         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
121         mockBodyReader(uri, jsonToPatchBodyReader, false);
122
123         final InputStream inputStream = TestJsonBodyReader.class
124                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
125
126         final PatchContext returnValue = jsonToPatchBodyReader
127                 .readFrom(null, null, null, mediaType, null, inputStream);
128         checkPatchContextMountPoint(returnValue);
129     }
130
131     /**
132      * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
133      */
134     @Test
135     public void modulePatchMergeOperationOnListTest() throws Exception {
136         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
137         mockBodyReader(uri, jsonToPatchBodyReader, false);
138
139         final InputStream inputStream = TestJsonBodyReader.class
140                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.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 container. Test consists of two edit operations - create and merge.
149      */
150     @Test
151     public void modulePatchMergeOperationOnContainerTest() throws Exception {
152         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont";
153         mockBodyReader(uri, jsonToPatchBodyReader, false);
154
155         final InputStream inputStream = TestJsonBodyReader.class
156                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
157
158         final PatchContext returnValue = jsonToPatchBodyReader
159                 .readFrom(null, null, null, mediaType, null, inputStream);
160         checkPatchContextMountPoint(returnValue);
161     }
162
163     /**
164      * Test reading simple leaf value.
165      */
166     @Test
167     public void modulePatchSimpleLeafValueTest() throws Exception {
168         final String uri = MOUNT_POINT + "/instance-identifier-patch-module:patch-cont/my-list1/leaf1";
169         mockBodyReader(uri, jsonToPatchBodyReader, false);
170
171         final InputStream inputStream = TestJsonBodyReader.class
172                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
173
174         final PatchContext returnValue = jsonToPatchBodyReader
175                 .readFrom(null, null, null, mediaType, null, inputStream);
176         checkPatchContext(returnValue);
177     }
178 }