Bug 5730 - Delete subset of list items using PATCH?
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonPATCHBodyReader.java
1 /*
2  * Copyright (c) 2015 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.rest.impl.JsonToPATCHBodyReader;
20 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23
24 public class TestJsonPATCHBodyReader extends AbstractBodyReaderTest {
25
26     private final JsonToPATCHBodyReader jsonPATCHBodyReader;
27     private static SchemaContext schemaContext;
28
29     public TestJsonPATCHBodyReader() 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     @Test
59     public void modulePATCHCreateAndDeleteTest() throws Exception {
60         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
61         mockBodyReader(uri, jsonPATCHBodyReader, false);
62
63         final InputStream inputStream = TestJsonBodyReader.class
64                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
65
66         final PATCHContext returnValue = jsonPATCHBodyReader
67                 .readFrom(null, null, null, mediaType, null, inputStream);
68         checkPATCHContext(returnValue);
69     }
70
71     @Test
72     public void modulePATCHValueMissingTest() throws Exception {
73         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
74         mockBodyReader(uri, jsonPATCHBodyReader, false);
75
76         final InputStream inputStream = TestJsonBodyReader.class
77                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
78
79         try {
80             jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
81             fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
82         } catch (RestconfDocumentedException e) {
83             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
84         }
85     }
86
87     @Test
88     public void modulePATCHValueNotSupportedTest() throws Exception {
89         final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
90         mockBodyReader(uri, jsonPATCHBodyReader, false);
91
92         final InputStream inputStream = TestJsonBodyReader.class
93                 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
94
95         try {
96             jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
97             fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
98         } catch (RestconfDocumentedException e) {
99             assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
100         }
101     }
102 }