121a7d6492a0a7fd4754e5c3a899c43862188363
[netconf.git] / restconf / restconf-nb-bierman02 / 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
13 import java.io.ByteArrayOutputStream;
14 import java.io.File;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import java.util.Collection;
18 import javax.ws.rs.core.MediaType;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
23 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
24 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 public class TestJsonBodyWriter extends AbstractBodyReaderTest {
29
30     private final JsonNormalizedNodeBodyReader jsonBodyReader;
31     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
32     private static EffectiveModelContext schemaContext;
33
34     public TestJsonBodyWriter() {
35         super(schemaContext, null);
36         this.jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
37         this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
38     }
39
40     @Override
41     protected MediaType getMediaType() {
42         return new MediaType(MediaType.APPLICATION_XML, null);
43     }
44
45     @BeforeClass
46     public static void initialization() throws Exception {
47         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
48         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
49         schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
50     }
51
52     @Test
53     public void rpcModuleInputTest() throws Exception {
54         final String uri = "invoke-rpc-module:rpc-test";
55         mockBodyReader(uri, this.jsonBodyReader, true);
56         final InputStream inputStream = TestJsonBodyWriter.class
57                 .getResourceAsStream("/invoke-rpc/json/rpc-output.json");
58         final NormalizedNodeContext returnValue = this.jsonBodyReader.readFrom(null,
59                 null, null, this.mediaType, null, inputStream);
60         final OutputStream output = new ByteArrayOutputStream();
61         this.jsonBodyWriter.writeTo(returnValue, null, null, null, this.mediaType, null,
62                 output);
63         assertTrue(output.toString().contains("lf-test"));
64     }
65 }