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.jersey.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.controller.sal.rest.impl.test.providers.TestJsonBodyReader;
26 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 public class JsonPATCHBodyReaderMountPointTest extends AbstractBodyReaderTest {
33 private final JsonToPATCHBodyReader jsonPATCHBodyReader;
34 private static SchemaContext schemaContext;
35 private static final String MOUNT_POINT = "instance-identifier-module:cont/yang-ext:mount/";
37 public JsonPATCHBodyReaderMountPointTest() throws Exception {
39 jsonPATCHBodyReader = new JsonToPATCHBodyReader();
43 protected MediaType getMediaType() {
44 return new MediaType(APPLICATION_JSON, null);
48 public static void initialization() {
49 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
51 final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
52 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
54 when(mountPointServiceHandler.get()).thenReturn(mountPointService);
55 when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
56 when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
58 controllerContext.setSchemas(schemaContext);
62 public void modulePATCHDataTest() throws Exception {
63 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
64 mockBodyReader(uri, jsonPATCHBodyReader, false);
66 final InputStream inputStream = TestJsonBodyReader.class
67 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
69 final PATCHContext returnValue = jsonPATCHBodyReader
70 .readFrom(null, null, null, mediaType, null, inputStream);
71 checkPATCHContextMountPoint(returnValue);
75 * Test of successful PATCH consisting of create and delete PATCH operations.
78 public void modulePATCHCreateAndDeleteTest() throws Exception {
79 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
80 mockBodyReader(uri, jsonPATCHBodyReader, false);
82 final InputStream inputStream = TestJsonBodyReader.class
83 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
85 final PATCHContext returnValue = jsonPATCHBodyReader
86 .readFrom(null, null, null, mediaType, null, inputStream);
87 checkPATCHContextMountPoint(returnValue);
91 * Test trying to use PATCH create operation which requires value without value. Test should fail with
92 * {@link RestconfDocumentedException} with error code 400.
95 public void modulePATCHValueMissingNegativeTest() throws Exception {
96 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
97 mockBodyReader(uri, jsonPATCHBodyReader, false);
99 final InputStream inputStream = TestJsonBodyReader.class
100 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
103 jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
104 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
105 } catch (final RestconfDocumentedException e) {
106 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
111 * Test trying to use value with PATCH delete operation which does not support value. Test should fail with
112 * {@link RestconfDocumentedException} with error code 400.
115 public void modulePATCHValueNotSupportedNegativeTest() throws Exception {
116 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
117 mockBodyReader(uri, jsonPATCHBodyReader, false);
119 final InputStream inputStream = TestJsonBodyReader.class
120 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
123 jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
124 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
125 } catch (final RestconfDocumentedException e) {
126 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
131 * Test using PATCH when target is completely specified in request URI and thus target leaf contains only '/' sign.
134 public void modulePATCHCompleteTargetInURITest() throws Exception {
135 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
136 mockBodyReader(uri, jsonPATCHBodyReader, false);
138 final InputStream inputStream = TestJsonBodyReader.class
139 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCompleteTargetInURI.json");
141 final PATCHContext returnValue = jsonPATCHBodyReader
142 .readFrom(null, null, null, mediaType, null, inputStream);
143 checkPATCHContextMountPoint(returnValue);
147 * Test of Yang PATCH merge operation on list. Test consists of two edit operations - replace and merge.
150 public void modulePATCHMergeOperationOnListTest() throws Exception {
151 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont/my-list1=leaf1";
152 mockBodyReader(uri, jsonPATCHBodyReader, false);
154 final InputStream inputStream = TestJsonBodyReader.class
155 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnList.json");
157 final PATCHContext returnValue = jsonPATCHBodyReader
158 .readFrom(null, null, null, mediaType, null, inputStream);
159 checkPATCHContextMountPoint(returnValue);
163 * Test of Yang PATCH merge operation on container. Test consists of two edit operations - create and merge.
166 public void modulePATCHMergeOperationOnContainerTest() throws Exception {
167 final String uri = MOUNT_POINT + "instance-identifier-patch-module:patch-cont";
168 mockBodyReader(uri, jsonPATCHBodyReader, false);
170 final InputStream inputStream = TestJsonBodyReader.class
171 .getResourceAsStream("/instanceidentifier/json/jsonPATCHMergeOperationOnContainer.json");
173 final PATCHContext returnValue = jsonPATCHBodyReader
174 .readFrom(null, null, null, mediaType, null, inputStream);
175 checkPATCHContextMountPoint(returnValue);