Merge "Reduce/enhance logging in AbstractLeader"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestJsonBodyWriter.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.rest.impl.test.providers;
10
11 import static org.junit.Assert.assertTrue;
12 import java.io.ByteArrayOutputStream;
13 import java.io.InputStream;
14 import java.io.OutputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
19 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
20 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22
23 /**
24  * sal-rest-connector
25  * org.opendaylight.controller.sal.rest.impl.test.providers
26  *
27  *
28  *
29  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
30  *
31  * Created: Mar 12, 2015
32  */
33 public class TestJsonBodyWriter extends AbstractBodyReaderTest {
34
35     private final JsonNormalizedNodeBodyReader jsonBodyReader;
36     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
37     private static SchemaContext schemaContext;
38
39     public TestJsonBodyWriter () throws NoSuchFieldException, SecurityException {
40         super();
41         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
42         jsonBodyReader = new JsonNormalizedNodeBodyReader();
43     }
44
45     @Override
46     MediaType getMediaType() {
47         return new MediaType(MediaType.APPLICATION_XML, null);
48     }
49
50     @BeforeClass
51     public static void initialization() throws NoSuchFieldException, SecurityException {
52         schemaContext = schemaContextLoader("/instanceidentifier/yang", schemaContext);
53         schemaContext = schemaContextLoader("/modules", schemaContext);
54         schemaContext = schemaContextLoader("/invoke-rpc", schemaContext);
55         controllerContext.setSchemas(schemaContext);
56     }
57
58     @Test
59     public void rpcModuleInputTest() throws Exception {
60         final String uri = "invoke-rpc-module:rpc-test";
61         mockBodyReader(uri, jsonBodyReader, true);
62         final InputStream inputStream = TestJsonBodyWriter.class
63                 .getResourceAsStream("/invoke-rpc/json/rpc-output.json");
64         final NormalizedNodeContext returnValue = jsonBodyReader
65                 .readFrom(null, null, null, mediaType, null, inputStream);
66         final OutputStream output = new ByteArrayOutputStream();
67         jsonBodyWriter.writeTo(returnValue, null, null, null, mediaType, null, output);
68         assertTrue(output.toString().contains("lf-test"));
69     }
70 }