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