Merge "Fix for Bug 3"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / xml / test / CnSnToXmlWithChoiceTest.java
1 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.io.IOException;
6
7 import javax.ws.rs.WebApplicationException;
8
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
12 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
13 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
14 import org.opendaylight.yangtools.yang.data.api.*;
15 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
16
17 /**
18  * 
19  * CnSn = Composite node and Simple node data structure Class contains test of
20  * serializing simple nodes data values according data types from YANG schema to
21  * XML file
22  * 
23  */
24 public class CnSnToXmlWithChoiceTest extends YangAndXmlAndDataSchemaLoader {
25     @BeforeClass
26     public static void initialization() {
27         dataLoad("/cnsn-to-xml/choice", 1, "module-with-choice", "cont");
28     }
29
30     @Test
31     public void cnSnToXmlWithYangChoice() {
32         String xmlOutput = "";
33         try {
34             xmlOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(
35                     prepareCnStructForYangData("lf1", "String data1"), modules, dataSchemaNode,
36                     StructuredDataToXmlProvider.INSTANCE);
37         } catch (WebApplicationException | IOException e) {
38         }
39
40         assertTrue(xmlOutput.contains("<lf1>String data1</lf1>"));
41
42         try {
43             xmlOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(
44                     prepareCnStructForYangData("lf2", "String data2"), modules, dataSchemaNode,
45                     StructuredDataToXmlProvider.INSTANCE);
46         } catch (WebApplicationException | IOException e) {
47         }
48         assertTrue(xmlOutput.contains("<lf2>String data2</lf2>"));
49
50     }
51
52     private CompositeNode prepareCnStructForYangData(String lfName, Object data) {
53         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
54                 ModifyAction.CREATE, null);
55
56         MutableSimpleNode<Object> lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName(lfName), cont, data,
57                 ModifyAction.CREATE, null);
58         cont.getChildren().add(lf1);
59         cont.init();
60
61         return cont;
62     }
63
64 }