3ea5f837b7efbea7d234372efeaa02347c0238e9
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlBodyWriter.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.OutputStream;
16 import java.util.Collection;
17 import javax.ws.rs.core.MediaType;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
21 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
22 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 /**
27  * sal-rest-connector org.opendaylight.controller.sal.rest.impl.test.providers
28  *
29  *
30  *
31  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
32  *
33  *         Created: Mar 12, 2015
34  */
35 public class TestXmlBodyWriter extends AbstractBodyReaderTest {
36
37     private final NormalizedNodeXmlBodyWriter xmlBodyWriter;
38     private static SchemaContext schemaContext;
39
40     public TestXmlBodyWriter() throws NoSuchFieldException, SecurityException {
41         super();
42         this.xmlBodyWriter = new NormalizedNodeXmlBodyWriter();
43     }
44
45     @Override
46     protected MediaType getMediaType() {
47         return new MediaType(MediaType.APPLICATION_XML, null);
48     }
49
50     @BeforeClass
51     public static void initialization() throws Exception {
52         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
53         testFiles.addAll(TestRestconfUtils.loadFiles("/modules"));
54         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
55         schemaContext = YangParserTestUtils.parseYangSources(testFiles);
56         controllerContext.setSchemas(schemaContext);
57     }
58
59     @Test
60     public void rpcModuleInputTest() throws Exception {
61         final String uri = "invoke-rpc-module:rpc-test";
62         final String pathToInputFile = "/invoke-rpc/xml/rpc-output.xml";
63         final NormalizedNodeContext nnContext = TestRestconfUtils
64                 .loadNormalizedContextFromXmlFile(pathToInputFile, uri);
65         final OutputStream output = new ByteArrayOutputStream();
66         this.xmlBodyWriter.writeTo(nnContext, null, null, null, this.mediaType, null,
67                 output);
68         assertTrue(output.toString().contains("lf-test"));
69     }
70 }