2 * Copyright (c) 2015 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;
15 import java.io.InputStream;
16 import javax.ws.rs.core.MediaType;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.netconf.sal.rest.impl.JsonToPATCHBodyReader;
20 import org.opendaylight.netconf.sal.restconf.impl.PATCHContext;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 public class TestJsonPATCHBodyReader extends AbstractBodyReaderTest {
26 private final JsonToPATCHBodyReader jsonPATCHBodyReader;
27 private static SchemaContext schemaContext;
29 public TestJsonPATCHBodyReader() throws NoSuchFieldException, SecurityException {
31 jsonPATCHBodyReader = new JsonToPATCHBodyReader();
35 protected MediaType getMediaType() {
36 return new MediaType(APPLICATION_JSON, null);
40 public static void initialization() {
41 schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
42 controllerContext.setSchemas(schemaContext);
46 public void modulePATCHDataTest() throws Exception {
47 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
48 mockBodyReader(uri, jsonPATCHBodyReader, false);
50 final InputStream inputStream = TestJsonBodyReader.class
51 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdata.json");
53 final PATCHContext returnValue = jsonPATCHBodyReader
54 .readFrom(null, null, null, mediaType, null, inputStream);
55 checkPATCHContext(returnValue);
59 public void modulePATCHCreateAndDeleteTest() throws Exception {
60 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
61 mockBodyReader(uri, jsonPATCHBodyReader, false);
63 final InputStream inputStream = TestJsonBodyReader.class
64 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataCreateAndDelete.json");
66 final PATCHContext returnValue = jsonPATCHBodyReader
67 .readFrom(null, null, null, mediaType, null, inputStream);
68 checkPATCHContext(returnValue);
72 public void modulePATCHValueMissingTest() throws Exception {
73 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
74 mockBodyReader(uri, jsonPATCHBodyReader, false);
76 final InputStream inputStream = TestJsonBodyReader.class
77 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueMissing.json");
80 jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
81 fail("Test should return error 400 due to missing value node when attempt to invoke create operation");
82 } catch (RestconfDocumentedException e) {
83 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
88 public void modulePATCHValueNotSupportedTest() throws Exception {
89 final String uri = "instance-identifier-patch-module:patch-cont/my-list1/leaf1";
90 mockBodyReader(uri, jsonPATCHBodyReader, false);
92 final InputStream inputStream = TestJsonBodyReader.class
93 .getResourceAsStream("/instanceidentifier/json/jsonPATCHdataValueNotSupported.json");
96 jsonPATCHBodyReader.readFrom(null, null, null, mediaType, null, inputStream);
97 fail("Test should return error 400 due to present value node when attempt to invoke delete operation");
98 } catch (RestconfDocumentedException e) {
99 assertEquals("Error code 400 expected", 400, e.getErrors().get(0).getErrorTag().getStatusCode());