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.handlers.SchemaContextHandler;
28 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
29 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.JsonBodyReaderTest;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 public class JsonPatchBodyReaderMountPointTest extends AbstractBodyReaderTest {
35 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
36 private static SchemaContext schemaContext;
37 private final JsonToPatchBodyReader jsonToPatchBodyReader;
39 public JsonPatchBodyReaderMountPointTest() throws Exception {
41 jsonToPatchBodyReader = new JsonToPatchBodyReader();
45 protected MediaType getMediaType() {
46 return new MediaType(APPLICATION_JSON, null);
50 public static void initialization() {
51 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
53 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
54 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
56 when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
57 when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
58 when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
59 SchemaContextHandler.setActualSchemaContext(schemaContext);
63 public void modulePatchDataTest() throws Exception {
64 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
65 mockBodyReader(uri, jsonToPatchBodyReader, false);
67 final InputStream inputStream = JsonBodyReaderTest.class
68 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
70 final PatchContext returnValue = jsonToPatchBodyReader
71 .readFrom(null, null, null, mediaType, null, inputStream);
72 checkPatchContextMountPoint(returnValue);
76 * Test of successful Patch consisting of create and delete Patch operations.
79 public void modulePatchCreateAndDeleteTest() throws Exception {
80 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
81 mockBodyReader(uri, jsonToPatchBodyReader, false);
83 final InputStream inputStream = JsonBodyReaderTest.class
84 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
86 final PatchContext returnValue = jsonToPatchBodyReader
87 .readFrom(null, null, null, mediaType, null, inputStream);
88 checkPatchContextMountPoint(returnValue);
92 * Test trying to use Patch create operation which requires value without value. Test should fail with
93 * {@link RestconfDocumentedException} with error code 400.
96 public void modulePatchValueMissingNegativeTest() throws Exception {
97 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
98 mockBodyReader(uri, jsonToPatchBodyReader, false);
100 final InputStream inputStream = JsonBodyReaderTest.class
101 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
104 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
105 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
106 } catch (final RestconfDocumentedException e) {
107 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
112 * Test trying to use value with Patch delete operation which does not support value. Test should fail with
113 * {@link RestconfDocumentedException} with error code 400.
116 public void modulePatchValueNotSupportedNegativeTest() throws Exception {
117 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
118 mockBodyReader(uri, jsonToPatchBodyReader, false);
120 final InputStream inputStream = JsonBodyReaderTest.class
121 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
124 jsonToPatchBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
125 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
126 } catch (final RestconfDocumentedException e) {
127 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
132 * Test using Patch when target is completely specified in request URI and thus target leaf contains only '/' sign.
135 public void modulePatchCompleteTargetInURITest() throws Exception {
136 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
137 mockBodyReader(uri, jsonToPatchBodyReader, false);
139 final InputStream inputStream = JsonBodyReaderTest.class
140 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
142 final PatchContext returnValue = jsonToPatchBodyReader
143 .readFrom(null, null, null, mediaType, null, inputStream);
144 checkPatchContextMountPoint(returnValue);
148 * Test of Yang Patch merge operation on list. Test consists of two edit operations - replace and merge.
151 public void modulePatchMergeOperationOnListTest() throws Exception {
152 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
153 mockBodyReader(uri, jsonToPatchBodyReader, false);
155 final InputStream inputStream = JsonBodyReaderTest.class
156 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
158 final PatchContext returnValue = jsonToPatchBodyReader
159 .readFrom(null, null, null, mediaType, null, inputStream);
160 checkPatchContextMountPoint(returnValue);
164 * Test of Yang Patch merge operation on container. Test consists of two edit operations - create and merge.
167 public void modulePatchMergeOperationOnContainerTest() throws Exception {
168 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
169 mockBodyReader(uri, jsonToPatchBodyReader, false);
171 final InputStream inputStream = JsonBodyReaderTest.class
172 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
174 final PatchContext returnValue = jsonToPatchBodyReader
175 .readFrom(null, null, null, mediaType, null, inputStream);
176 checkPatchContextMountPoint(returnValue);
180 * Test reading simple leaf value.
183 public void modulePatchSimpleLeafValueTest() throws Exception {
184 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
185 mockBodyReader(uri, jsonToPatchBodyReader, false);
187 final InputStream inputStream =
188 JsonBodyReaderTest.class
189 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
191 final PatchContext returnValue = jsonToPatchBodyReader
192 .readFrom(null, null, null, mediaType, null, inputStream);
193 checkPatchContext(returnValue);