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 org.junit.Assert.assertTrue;
13 import java.io.ByteArrayOutputStream;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import java.util.Collection;
19 import javax.ws.rs.core.MediaType;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
24 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
25 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
26 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
34 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
36 * Created: Mar 12, 2015
38 public class TestJsonBodyWriter extends AbstractBodyReaderTest {
40 private final JsonNormalizedNodeBodyReader jsonBodyReader;
41 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
42 private static SchemaContext schemaContext;
44 public TestJsonBodyWriter() throws NoSuchFieldException, SecurityException {
46 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
47 jsonBodyReader = new JsonNormalizedNodeBodyReader();
51 protected MediaType getMediaType() {
52 return new MediaType(MediaType.APPLICATION_XML, null);
56 public static void initialization() throws Exception {
57 Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
58 testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
59 testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
60 schemaContext = TestRestconfUtils.parseYangSources(testFiles);
61 controllerContext.setSchemas(schemaContext);
65 public void rpcModuleInputTest() throws Exception {
66 final String uri = "invoke-rpc-module:rpc-test";
67 mockBodyReader(uri, jsonBodyReader, true);
68 final InputStream inputStream = TestJsonBodyWriter.class
69 .getResourceAsStream("/invoke-rpc/json/rpc-output.json");
70 final NormalizedNodeContext returnValue = jsonBodyReader.readFrom(null,
71 null, null, mediaType, null, inputStream);
72 final OutputStream output = new ByteArrayOutputStream();
73 jsonBodyWriter.writeTo(returnValue, null, null, null, mediaType, null,
75 assertTrue(output.toString().contains("lf-test"));