2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040.jersey.providers.patch;
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;
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;
32 public class JsonPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
34 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
35 private static SchemaContext schemaContext;
36 private final JsonToPatchBodyReader jsonToPatchBodyReader;
38 public JsonPatchBodyReaderMountPointTest() throws Exception {
40 jsonToPatchBodyReader = new JsonToPatchBodyReader(schemaContextHandler);
44 protected MediaType getMediaType() {
45 return new MediaType(APPLICATION_JSON, null);
49 public static void initialization() {
50 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
52 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
53 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
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);
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);
65 final InputStream inputStream = JsonBodyReaderTest.class
66 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
68 final PatchContext returnValue = jsonToPatchBodyReader
69 .readFrom(null, null, null, mediaType, null, inputStream);
70 checkPatchContextMountPoint(returnValue);
74 * Test of successful Patch consisting of create and delete Patch operations.
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);
81 final InputStream inputStream = JsonBodyReaderTest.class
82 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
84 final PatchContext returnValue = jsonToPatchBodyReader
85 .readFrom(null, null, null, mediaType, null, inputStream);
86 checkPatchContextMountPoint(returnValue);
90 * Test trying to use Patch create operation which requires value without value. Test should fail with
91 * {@link RestconfDocumentedException} with error code 400.
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);
98 final InputStream inputStream = JsonBodyReaderTest.class
99 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
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());
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.
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);
118 final InputStream inputStream = JsonBodyReaderTest.class
119 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
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());
130 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
133 public void modulePatchCompleteTargetInURITest() throws Exception {
134 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
135 mockBodyReader(uri, jsonToPatchBodyReader, false);
137 final InputStream inputStream = JsonBodyReaderTest.class
138 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
140 final PatchContext returnValue = jsonToPatchBodyReader
141 .readFrom(null, null, null, mediaType, null, inputStream);
142 checkPatchContextMountPoint(returnValue);
146 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
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);
153 final InputStream inputStream = JsonBodyReaderTest.class
154 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
156 final PatchContext returnValue = jsonToPatchBodyReader
157 .readFrom(null, null, null, mediaType, null, inputStream);
158 checkPatchContextMountPoint(returnValue);
162 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
165 public void modulePatchMergeOperationOnContainerTest() throws Exception {
166 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
167 mockBodyReader(uri, jsonToPatchBodyReader, false);
169 final InputStream inputStream = JsonBodyReaderTest.class
170 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
172 final PatchContext returnValue = jsonToPatchBodyReader
173 .readFrom(null, null, null, mediaType, null, inputStream);
174 checkPatchContextMountPoint(returnValue);
178 * Test reading simple leaf value.
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);
185 final InputStream inputStream =
186 JsonBodyReaderTest.class
187 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
189 final PatchContext returnValue = jsonToPatchBodyReader
190 .readFrom(null, null, null, mediaType, null, inputStream);
191 checkPatchContext(returnValue);