f49251319a39dbeaeb92c5c63df674acf6814a30
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestDraft11JsonPATCHBodyReader.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
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.netconf.sal.restconf.impl.PATCHContext;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
21 import org.opendaylight.restconf.utils.patch.JsonToPATCHBodyReader;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23
24 public class TestDraft11JsonPATCHBodyReader extends Draft11AbstractBodyReaderTest {
25
26     private final JsonToPATCHBodyReader jsonPATCHBodyReader;
27     private static SchemaContext schemaContext;
28
29     public TestDraft11JsonPATCHBodyReader() throws NoSuchFieldException, SecurityException {
30         super();
31         jsonPATCHBodyReader = new JsonToPATCHBodyReader();
32     }
33
34     @Override
35     protected MediaType getMediaType() {
36         return new MediaType(APPLICATION_JSON, null);
37     }
38
39     @BeforeClass
40     public static void initialization() {
41         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
42         controllerContext.setSchemas(schemaContext);
43     }
44
45     @Test
46     public void modulePATCHDataTest() throws Exception {
47         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
48         mockBodyReader(uri, jsonPATCHBodyReader, false);
49
50         final InputStream inputStream = TestJsonBodyReader.class
51                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
52
53         final PATCHContext returnValue = jsonPATCHBodyReader
54                 .readFrom(null, null, null, mediaType, null, inputStream);
55         checkPATCHContext(returnValue);
56     }
57
58     /**
59      * Test of successful PATCH consisting of create and delete PATCH operations.
60      */
61     @Test
62     public void modulePATCHCreateAndDeleteTest() throws Exception {
63         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
64         mockBodyReader(uri, jsonPATCHBodyReader, false);
65
66         final InputStream inputStream = TestJsonBodyReader.class
67                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
68
69         final PATCHContext returnValue = jsonPATCHBodyReader
70                 .readFrom(null, null, null, mediaType, null, inputStream);
71         checkPATCHContext(returnValue);
72     }
73
74     /**
75      * Test trying to use PATCH create operation which requires value without value. Test should fail with
76      * {@link RestconfDocumentedException} with error code 400.
77      */
78     @Test
79     public void modulePATCHValueMissingNegativeTest() throws Exception {
80         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
81         mockBodyReader(uri, jsonPATCHBodyReader, false);
82
83         final InputStream inputStream = TestJsonBodyReader.class
84                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
85
86         try {
87             jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
88             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
89         } catch (final RestconfDocumentedException e) {
90             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
91         }
92     }
93
94     /**
95      * Test trying to use value with PATCH delete operation which does not support value. Test should fail with
96      * {@link RestconfDocumentedException} with error code 400.
97      */
98     @Test
99     public void modulePATCHValueNotSupportedNegativeTest() throws Exception {
100         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
101         mockBodyReader(uri, jsonPATCHBodyReader, false);
102
103         final InputStream inputStream = TestJsonBodyReader.class
104                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
105
106         try {
107             jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
108             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
109         } catch (final RestconfDocumentedException e) {
110             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
111         }
112     }
113
114     /**
115      * Test using PATCH when target is completely specified in request URI and thus target leaf contains only '/' sign.
116      */
117     @Test
118     public void modulePATCHCompleteTargetInURITest() throws Exception {
119         final String uri = "instance-identifier-patch-module:patch-cont";
120         mockBodyReader(uri, jsonPATCHBodyReader, false);
121
122         final InputStream inputStream = TestJsonBodyReader.class
123                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
124
125         final PATCHContext returnValue = jsonPATCHBodyReader
126                 .readFrom(null, null, null, mediaType, null, inputStream);
127         checkPATCHContext(returnValue);
128     }
129
130     /**
131      * Test of Yang PATCH merge operation on list. Test consists of two edit operations - replace and merge.
132      */
133     @Test
134     public void modulePATCHMergeOperationOnListTest() throws Exception {
135         final String uri = "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
136         mockBodyReader(uri, jsonPATCHBodyReader, false);
137
138         final InputStream inputStream = TestJsonBodyReader.class
139                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
140
141         final PATCHContext returnValue = jsonPATCHBodyReader
142                 .readFrom(null, null, null, mediaType, null, inputStream);
143         checkPATCHContext(returnValue);
144     }
145
146     /**
147      * Test of Yang PATCH merge operation on container. Test consists of two edit operations - create and merge.
148      */
149     @Test
150     public void modulePATCHMergeOperationOnContainerTest() throws Exception {
151         final String uri = "instance-identifier-patch-module:patch-cont";
152         mockBodyReader(uri, jsonPATCHBodyReader, false);
153
154         final InputStream inputStream = TestJsonBodyReader.class
155                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
156
157         final PATCHContext returnValue = jsonPATCHBodyReader
158                 .readFrom(null, null, null, mediaType, null, inputStream);
159         checkPATCHContext(returnValue);
160     }
161 }