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