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