Fixed RESTConf support for identity-ref build-in datatype
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / CnSnToXmlTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.StringWriter;
8 import java.util.Set;
9
10 import javax.activation.UnsupportedDataTypeException;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerException;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.dom.DOMSource;
15 import javax.xml.transform.stream.StreamResult;
16
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.rest.impl.XmlMapper;
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
22 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
24 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.w3c.dom.Document;
29
30 public class CnSnToXmlTest {
31
32     private static Set<Module> modules;
33     private static DataSchemaNode dataSchemaNode;
34
35     @BeforeClass
36     public static void initialization() {
37         modules = TestUtils.resolveModules("/cnsn-to-xml/identityref");
38         assertEquals(2, modules.size());
39         Module module = TestUtils.resolveModule("identityref-module", modules);
40         assertNotNull(module);
41         dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont");
42         assertNotNull(dataSchemaNode);
43
44     }
45
46     @Test
47     public void compositeNodeToXMLTest() {
48         XmlMapper xmlMapper = new XmlMapper();
49         String xmlString = null;
50         if (dataSchemaNode instanceof DataNodeContainer) {
51             try {
52                 Document doc = xmlMapper.write(prepareData(), (DataNodeContainer) dataSchemaNode);
53                 DOMSource domSource = new DOMSource(doc);
54                 StringWriter writer = new StringWriter();
55                 StreamResult result = new StreamResult(writer);
56                 TransformerFactory tf = TransformerFactory.newInstance();
57                 Transformer transformer = tf.newTransformer();
58                 transformer.transform(domSource, result);
59                 xmlString = writer.toString();
60
61             } catch (UnsupportedDataTypeException | TransformerException e) {
62                 // TODO Auto-generated catch block
63                 e.printStackTrace();
64             }
65         }
66         assertNotNull(xmlString);
67         assertTrue(xmlString.contains("<lf1 xmlns:x=\"identity:module\">x:iden</lf1>"));
68
69     }
70
71     private CompositeNode prepareData() {
72         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
73                 TestUtils.buildQName("cont", "identityref:module", "2013-12-2"), null, null, ModifyAction.CREATE, null);
74         MutableCompositeNode cont1 = NodeFactory
75                 .createMutableCompositeNode(TestUtils.buildQName("cont1", "identityref:module", "2013-12-2"), cont,
76                         null, ModifyAction.CREATE, null);
77         cont.getChildren().add(cont1);
78
79         MutableSimpleNode<Object> lf11 = NodeFactory.createMutableSimpleNode(
80                 TestUtils.buildQName("lf1", "identityref:module", "2013-12-2"), cont1,
81                 TestUtils.buildQName("iden", "identity:module", "2013-12-2"), ModifyAction.CREATE, null);
82         cont1.getChildren().add(lf11);
83         cont1.init();
84         cont.init();
85
86         return cont;
87     }
88
89 }