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.controller.sal.rest.impl.test.providers;
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.netconf.sal.rest.impl.JsonToPatchBodyReader;
26 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
27 import org.opendaylight.restconf.common.patch.PatchContext;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 public class TestJsonPatchBodyReaderMountPoint extends AbstractBodyReaderTest {
33 private final JsonToPatchBodyReader jsonToPatchBodyReader;
34 private static SchemaContext schemaContext;
35 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount";
37 public TestJsonPatchBodyReaderMountPoint() throws NoSuchFieldException, SecurityException {
38 jsonToPatchBodyReader = new JsonToPatchBodyReader();
42 protected MediaType getMediaType() {
43 return new MediaType(APPLICATION_JSON, null);
47 public static void initialization() {
48 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
50 final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
51 when(mockMountPoint.getSchemaContext()).thenReturn(schemaContext);
52 final DOMMountPointService mockMountPointService = mock(DOMMountPointService.class);
53 when(mockMountPointService.getMountPoint(any(YangInstanceIdentifier.class)))
54 .thenReturn(Optional.of(mockMountPoint));
56 CONTROLLER_CONTEXT.setMountService(mockMountPointService);
57 CONTROLLER_CONTEXT.setSchemas(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 = TestJsonBodyReader.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 = TestJsonBodyReader.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 = TestJsonBodyReader.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 = TestJsonBodyReader.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 = TestJsonBodyReader.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 = TestJsonBodyReader.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 = TestJsonBodyReader.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 = TestJsonBodyReader.class
186 .getResourceAsStream("/instanceidentifier/json/jsonPATCHSimpleLeafValue.json");
188 final PatchContext returnValue = jsonToPatchBodyReader
189 .readFrom(null, null, null, mediaType, null, inputStream);
190 checkPatchContext(returnValue);