1 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test;
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
6 import java.io.IOException;
8 import javax.ws.rs.WebApplicationException;
9 import javax.xml.transform.TransformerFactoryConfigurationError;
11 import org.junit.BeforeClass;
12 import org.junit.Test;
13 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
14 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
15 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
16 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
17 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
18 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
19 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
20 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
21 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
25 * CnSn = Composite node and Simple node data structure Class contains test of
26 * serializing simple nodes data values according data types from YANG schema to
30 public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
32 public static void initialization() {
33 dataLoad("/cnsn-to-xml/yang", 2, "basic-module", "cont");
37 public void snAsYangIdentityrefToXMLTest() {
38 serializeToXml(prepareIdentityrefData(null, true), "<lf11 xmlns:x=\"referenced:module\">x:iden</lf11>");
42 public void snAsYangIdentityrefWithQNamePrefixToXMLTest() {
43 serializeToXml(prepareIdentityrefData("prefix", true),
44 "<lf11 xmlns:prefix=\"referenced:module\">prefix:iden</lf11>");
48 public void snAsYangIdentityrefWithPrefixToXMLTest() {
49 serializeToXml(prepareIdentityrefData("prefix", false), "<lf11>no qname value</lf11>");
53 public void snAsYangLeafrefWithPrefixToXMLTest() {
54 serializeToXml(prepareLeafrefData(), "<lfBoolean>true</lfBoolean>", "<lfLfref>true</lfLfref>");
59 public void snAsYangStringToXmlTest() {
61 prepareCnStructForYangData(TypeDefinitionAwareCodec.STRING_DEFAULT_CODEC.deserialize("lfStr value"),
62 "lfStr"), "<lfStr>lfStr value</lfStr>");
66 public void snAsYangInt8ToXmlTest() {
67 String elName = "lfInt8";
69 prepareCnStructForYangData(TypeDefinitionAwareCodec.INT8_DEFAULT_CODEC.deserialize("14"), elName), "<"
70 + elName + ">14</" + elName + ">");
74 public void snAsYangInt16ToXmlTest() {
75 String elName = "lfInt16";
77 prepareCnStructForYangData(TypeDefinitionAwareCodec.INT16_DEFAULT_CODEC.deserialize("3000"), elName),
78 "<" + elName + ">3000</" + elName + ">");
82 public void snAsYangInt32ToXmlTest() {
83 String elName = "lfInt32";
85 prepareCnStructForYangData(TypeDefinitionAwareCodec.INT32_DEFAULT_CODEC.deserialize("201234"), elName),
86 "<" + elName + ">201234</" + elName + ">");
90 public void snAsYangInt64ToXmlTest() {
91 String elName = "lfInt64";
93 prepareCnStructForYangData(TypeDefinitionAwareCodec.INT64_DEFAULT_CODEC.deserialize("5123456789"),
94 elName), "<" + elName + ">5123456789</" + elName + ">");
98 public void snAsYangUint8ToXmlTest() {
99 String elName = "lfUint8";
101 prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT8_DEFAULT_CODEC.deserialize("200"), elName),
102 "<" + elName + ">200</" + elName + ">");
106 public void snAsYangUint16ToXmlTest() {
107 String elName = "lfUint16";
109 prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT16_DEFAULT_CODEC.deserialize("4000"), elName),
110 "<" + elName + ">4000</" + elName + ">");
114 public void snAsYangUint32ToXmlTest() {
115 String elName = "lfUint32";
117 prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT32_DEFAULT_CODEC.deserialize("4123456789"),
118 elName), "<" + elName + ">4123456789</" + elName + ">");
122 public void snAsYangUint64ToXmlTest() {
123 String elName = "lfUint64";
125 prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT64_DEFAULT_CODEC.deserialize("5123456789"),
126 elName), "<" + elName + ">5123456789</" + elName + ">");
130 public void snAsYangBinaryToXmlTest() {
131 String elName = "lfBinary";
133 prepareCnStructForYangData(
134 TypeDefinitionAwareCodec.BINARY_DEFAULT_CODEC
135 .deserialize("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"),
136 elName), "<" + elName + ">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567</"
141 public void snAsYangBitsToXmlTest() {
142 String elName = "lfBits";
144 prepareCnStructForYangData(TypeDefinitionAwareCodec.BITS_DEFAULT_CODEC.deserialize("one two"), elName),
145 "<" + elName + ">one two</" + elName + ">", "<" + elName + ">two one</" + elName + ">");
149 public void snAsYangEnumerationToXmlTest() {
150 String elName = "lfEnumeration";
152 prepareCnStructForYangData(TypeDefinitionAwareCodec.ENUMERATION_DEFAULT_CODEC.deserialize("enum2"),
153 elName), "<" + elName + ">enum2</" + elName + ">");
157 public void snAsYangEmptyToXmlTest() {
158 String elName = "lfEmpty";
160 prepareCnStructForYangData(TypeDefinitionAwareCodec.EMPTY_DEFAULT_CODEC.deserialize(null), elName), "<"
165 public void snAsYangBooleanToXmlTest() {
166 String elName = "lfBoolean";
168 prepareCnStructForYangData(TypeDefinitionAwareCodec.BOOLEAN_DEFAULT_CODEC.deserialize("str"), elName),
169 "<" + elName + ">false</" + elName + ">");
171 prepareCnStructForYangData(TypeDefinitionAwareCodec.BOOLEAN_DEFAULT_CODEC.deserialize("true"), elName),
172 "<" + elName + ">true</" + elName + ">");
176 public void snAsYangUnionToXmlTest() {
177 String elName = "lfUnion";
180 prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(int8), elName), "<"
181 + elName + ">15</" + elName + ">");
183 String bits = "first second";
185 prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(bits), elName), "<"
186 + elName + ">first second</" + elName + ">");
190 prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(bool), elName), "<"
191 + elName + ">str</" + elName + ">");
194 private void serializeToXml(CompositeNode compositeNode, String... xmlRepresentation)
195 throws TransformerFactoryConfigurationError {
196 String xmlString = "";
198 xmlString = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
199 StructuredDataToXmlProvider.INSTANCE);
200 } catch (WebApplicationException | IOException e) {
202 assertNotNull(xmlString);
203 boolean containSearchedStr = false;
204 String strRepresentation = "";
205 for (String searchedStr : xmlRepresentation) {
206 if (xmlString.contains(searchedStr)) {
207 containSearchedStr = true;
210 strRepresentation = strRepresentation + "[" + searchedStr + "]";
212 assertTrue("At least one of specified strings " + strRepresentation + " wasn't found.", containSearchedStr);
216 private CompositeNode prepareIdentityrefData(String prefix, boolean valueAsQName) {
217 MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
218 TestUtils.buildQName("cont", "basic:module", "2013-12-2"), null, null, ModifyAction.CREATE, null);
219 MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(
220 TestUtils.buildQName("cont1", "basic:module", "2013-12-2"), cont, null, ModifyAction.CREATE, null);
221 cont.getChildren().add(cont1);
225 value = TestUtils.buildQName("iden", "referenced:module", "2013-12-2", prefix);
227 value = "no qname value";
229 MutableSimpleNode<Object> lf11 = NodeFactory.createMutableSimpleNode(
230 TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, value, ModifyAction.CREATE, null);
231 cont1.getChildren().add(lf11);
238 private CompositeNode prepareCnStructForYangData(Object data, String leafName) {
239 MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
240 ModifyAction.CREATE, null);
242 MutableSimpleNode<Object> lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName(leafName), cont, data,
243 ModifyAction.CREATE, null);
244 cont.getChildren().add(lf1);
250 private CompositeNode prepareLeafrefData() {
251 MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
252 ModifyAction.CREATE, null);
254 MutableSimpleNode<Object> lfBoolean = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfBoolean"),
255 cont, Boolean.TRUE, ModifyAction.CREATE, null);
256 MutableSimpleNode<Object> lfLfref = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfLfref"), cont,
257 "true", ModifyAction.CREATE, null);
258 cont.getChildren().add(lfBoolean);
259 cont.getChildren().add(lfLfref);