Sal-rest-connector unit tests switch to new yang parser.
[netconf.git] / opendaylight / restconf / 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
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
19 import javax.ws.rs.core.MediaType;
20
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;
28
29 /**
30  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
31  *
32  *
33  *
34  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
35  *
36  *         Created: Mar 12, 2015
37  */
38 public class TestJsonBodyWriter extends AbstractBodyReaderTest {
39
40     private final JsonNormalizedNodeBodyReader jsonBodyReader;
41     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
42     private static SchemaContext schemaContext;
43
44     public TestJsonBodyWriter() throws NoSuchFieldException, SecurityException {
45         super();
46         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
47         jsonBodyReader = new JsonNormalizedNodeBodyReader();
48     }
49
50     @Override
51     protected MediaType getMediaType() {
52         return new MediaType(MediaType.APPLICATION_XML, null);
53     }
54
55     @BeforeClass
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);
62     }
63
64     @Test
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,
74                 output);
75         assertTrue(output.toString().contains("lf-test"));
76     }
77 }