Bug 762: Fixed bug in CsSnToXmlTest java.
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / xml / test / CnSnToXmlTest.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.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.io.IOException;
16 import java.util.List;
17
18 import javax.ws.rs.WebApplicationException;
19 import javax.xml.transform.TransformerFactoryConfigurationError;
20
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
24 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
25 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
28 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
29 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
30 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
31 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
35 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
36 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
37 import org.opendaylight.yangtools.yang.model.util.BinaryType;
38 import org.opendaylight.yangtools.yang.model.util.BitsType;
39 import org.opendaylight.yangtools.yang.model.util.BooleanType;
40 import org.opendaylight.yangtools.yang.model.util.EmptyType;
41 import org.opendaylight.yangtools.yang.model.util.EnumerationType;
42 import org.opendaylight.yangtools.yang.model.util.Int16;
43 import org.opendaylight.yangtools.yang.model.util.Int32;
44 import org.opendaylight.yangtools.yang.model.util.Int64;
45 import org.opendaylight.yangtools.yang.model.util.Int8;
46 import org.opendaylight.yangtools.yang.model.util.StringType;
47 import org.opendaylight.yangtools.yang.model.util.Uint16;
48 import org.opendaylight.yangtools.yang.model.util.Uint32;
49 import org.opendaylight.yangtools.yang.model.util.Uint64;
50 import org.opendaylight.yangtools.yang.model.util.Uint8;
51 import org.opendaylight.yangtools.yang.model.util.UnionType;
52
53 import com.google.common.base.Optional;
54 import com.google.common.collect.Lists;
55
56 /**
57  *
58  * CnSn = Composite node and Simple node data structure Class contains test of
59  * serializing simple nodes data values according data types from YANG schema to
60  * XML file
61  *
62  */
63 public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
64     @BeforeClass
65     public static void initialization() {
66         dataLoad("/cnsn-to-xml/yang", 2, "basic-module", "cont");
67     }
68
69     @Test
70     public void snAsYangIdentityrefToXMLTest() {
71         serializeToXml(prepareIdentityrefData(null, true), "<lf11 xmlns:x=\"referenced:module\">x:iden</lf11>");
72     }
73
74     @Test
75     public void snAsYangIdentityrefWithQNamePrefixToXMLTest() {
76         serializeToXml(prepareIdentityrefData("prefix", true),
77                 "<lf11 xmlns:prefix=\"referenced:module\">prefix:iden</lf11>");
78     }
79
80     @Test
81     public void snAsYangIdentityrefWithPrefixToXMLTest() {
82         serializeToXml(prepareIdentityrefData("prefix", false), "<lf11>no qname value</lf11>");
83     }
84
85     @Test
86     public void snAsYangLeafrefWithPrefixToXMLTest() {
87         serializeToXml(prepareLeafrefData(), "<lfBoolean>true</lfBoolean>", "<lfLfref>true</lfLfref>");
88     }
89
90
91     @Test
92     public void snAsYangStringToXmlTest() {
93         serializeToXml(
94                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(StringType.getInstance()).deserialize("lfStr value"),
95                         "lfStr"), "<lfStr>lfStr value</lfStr>");
96     }
97
98     @Test
99     public void snAsYangInt8ToXmlTest() {
100         String elName = "lfInt8";
101         serializeToXml(
102                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int8.getInstance()).deserialize("14"), elName), "<"
103                         + elName + ">14</" + elName + ">");
104     }
105
106     @Test
107     public void snAsYangInt16ToXmlTest() {
108         String elName = "lfInt16";
109         serializeToXml(
110                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int16.getInstance()).deserialize("3000"), elName),
111                 "<" + elName + ">3000</" + elName + ">");
112     }
113
114     @Test
115     public void snAsYangInt32ToXmlTest() {
116         String elName = "lfInt32";
117         serializeToXml(
118                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int32.getInstance()).deserialize("201234"), elName),
119                 "<" + elName + ">201234</" + elName + ">");
120     }
121
122     @Test
123     public void snAsYangInt64ToXmlTest() {
124         String elName = "lfInt64";
125         serializeToXml(
126                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int64.getInstance()).deserialize("5123456789"),
127                         elName), "<" + elName + ">5123456789</" + elName + ">");
128     }
129
130     @Test
131     public void snAsYangUint8ToXmlTest() {
132         String elName = "lfUint8";
133         serializeToXml(
134                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint8.getInstance()).deserialize("200"), elName),
135                 "<" + elName + ">200</" + elName + ">");
136     }
137
138     @Test
139     public void snAsYangUint16ToXmlTest() {
140         String elName = "lfUint16";
141         serializeToXml(
142                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint16.getInstance()).deserialize("4000"), elName),
143                 "<" + elName + ">4000</" + elName + ">");
144     }
145
146     @Test
147     public void snAsYangUint32ToXmlTest() {
148         String elName = "lfUint32";
149         serializeToXml(
150                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint32.getInstance()).deserialize("4123456789"),
151                         elName), "<" + elName + ">4123456789</" + elName + ">");
152     }
153
154     @Test
155     public void snAsYangUint64ToXmlTest() {
156         String elName = "lfUint64";
157         serializeToXml(
158                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint64.getInstance()).deserialize("5123456789"),
159                         elName), "<" + elName + ">5123456789</" + elName + ">");
160     }
161
162     @Test
163     public void snAsYangBinaryToXmlTest() {
164         String elName = "lfBinary";
165         serializeToXml(
166                 prepareCnStructForYangData(
167                         TypeDefinitionAwareCodec.from(BinaryType.getInstance())
168                         .deserialize("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"),
169                         elName), "<" + elName + ">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567</"
170                                 + elName + ">");
171     }
172
173     @Test
174     public void snAsYangBitsToXmlTest() {
175         BitsTypeDefinition.Bit mockBit1 = mock( BitsTypeDefinition.Bit.class );
176         when( mockBit1.getName() ).thenReturn( "one" );
177         BitsTypeDefinition.Bit mockBit2 = mock( BitsTypeDefinition.Bit.class );
178         when( mockBit2.getName() ).thenReturn( "two" );
179         List<BitsTypeDefinition.Bit> bitList = Lists.newArrayList( mockBit1, mockBit2 );
180
181         String elName = "lfBits";
182         serializeToXml(
183                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(
184                         BitsType.create( mock( SchemaPath.class ), bitList ) )
185                         .deserialize("one two"), elName),
186                         "<" + elName + ">one two</" + elName + ">", "<" + elName + ">two one</" + elName + ">");
187     }
188
189     @Test
190     public void snAsYangEnumerationToXmlTest() {
191         EnumTypeDefinition.EnumPair mockEnum = mock( EnumTypeDefinition.EnumPair.class );
192         when( mockEnum.getName() ).thenReturn( "enum2" );
193         List<EnumPair> enumList = Lists.newArrayList( mockEnum );
194
195         String elName = "lfEnumeration";
196         serializeToXml(
197                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(
198                         EnumerationType.create( mock( SchemaPath.class ), enumList,
199                                 Optional.<EnumTypeDefinition.EnumPair>absent() ) )
200                                 .deserialize("enum2"),
201                                 elName), "<" + elName + ">enum2</" + elName + ">");
202     }
203
204     @Test
205     public void snAsYangEmptyToXmlTest() {
206         String elName = "lfEmpty";
207         serializeToXml(
208                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(EmptyType.getInstance()).deserialize(null), elName), "<"
209                         + elName + "/>");
210     }
211
212     @Test
213     public void snAsYangBooleanToXmlTest() {
214         String elName = "lfBoolean";
215         serializeToXml(
216                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(BooleanType.getInstance()).deserialize("str"), elName),
217                 "<" + elName + ">false</" + elName + ">");
218         serializeToXml(
219                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(BooleanType.getInstance()).deserialize("true"), elName),
220                 "<" + elName + ">true</" + elName + ">");
221     }
222
223     @Test
224     public void snAsYangUnionToXmlTest() {
225
226         BitsTypeDefinition.Bit mockBit1 = mock( BitsTypeDefinition.Bit.class );
227         when( mockBit1.getName() ).thenReturn( "first" );
228         BitsTypeDefinition.Bit mockBit2 = mock( BitsTypeDefinition.Bit.class );
229         when( mockBit2.getName() ).thenReturn( "second" );
230         List<BitsTypeDefinition.Bit> bitList = Lists.newArrayList( mockBit1, mockBit2 );
231
232         List<TypeDefinition<?>> types = Lists.<TypeDefinition<?>>newArrayList(
233                 Int8.getInstance(),
234                 BitsType.create( mock( SchemaPath.class ) , bitList ),
235                 BooleanType.getInstance() );
236         UnionType unionType = UnionType.create( types );
237
238         String elName = "lfUnion";
239         String int8 = "15";
240         serializeToXml(
241                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(unionType).deserialize(int8), elName), "<"
242                         + elName + ">15</" + elName + ">");
243
244         String bits = "first second";
245         serializeToXml(
246                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(unionType).deserialize(bits), elName), "<"
247                         + elName + ">first second</" + elName + ">");
248
249         String bool = "str";
250         serializeToXml(
251                 prepareCnStructForYangData(TypeDefinitionAwareCodec.from(unionType).deserialize(bool), elName), "<"
252                         + elName + ">str</" + elName + ">");
253     }
254
255     private void serializeToXml(final CompositeNode compositeNode, final String... xmlRepresentation)
256             throws TransformerFactoryConfigurationError {
257         String xmlString = "";
258         try {
259             xmlString = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
260                     StructuredDataToXmlProvider.INSTANCE);
261         } catch (WebApplicationException | IOException e) {
262         }
263         assertNotNull(xmlString);
264         boolean containSearchedStr = false;
265         String strRepresentation = "";
266         for (String searchedStr : xmlRepresentation) {
267             if (xmlString.contains(searchedStr)) {
268                 containSearchedStr = true;
269                 break;
270             }
271             strRepresentation = strRepresentation + "[" + searchedStr + "]";
272         }
273         assertTrue("At least one of specified strings " + strRepresentation + " wasn't found.", containSearchedStr);
274
275     }
276
277     private CompositeNode prepareIdentityrefData(final String prefix, final boolean valueAsQName) {
278         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
279                 TestUtils.buildQName("cont", "basic:module", "2013-12-2"), null, null, ModifyAction.CREATE, null);
280         MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(
281                 TestUtils.buildQName("cont1", "basic:module", "2013-12-2"), cont, null, ModifyAction.CREATE, null);
282         cont.getValue().add(cont1);
283
284         Object value = null;
285         if (valueAsQName) {
286             value = TestUtils.buildQName("iden", "referenced:module", "2013-12-2", prefix);
287         } else {
288             value = "no qname value";
289         }
290         MutableSimpleNode<Object> lf11 = NodeFactory.createMutableSimpleNode(
291                 TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, value, ModifyAction.CREATE, null);
292         cont1.getValue().add(lf11);
293         cont1.init();
294         cont.init();
295
296         return cont;
297     }
298
299     private CompositeNode prepareCnStructForYangData(final Object data, final String leafName) {
300         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
301                 TestUtils.buildQName("cont", "basic:module", "2013-12-2"), null, null, ModifyAction.CREATE, null);
302
303         MutableSimpleNode<Object> lf1 = NodeFactory.createMutableSimpleNode(
304                 TestUtils.buildQName(leafName, "basic:module", "2013-12-2"), cont, data, ModifyAction.CREATE, null);
305         cont.getValue().add(lf1);
306         cont.init();
307
308         return cont;
309     }
310
311     private CompositeNode prepareLeafrefData() {
312         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
313                 ModifyAction.CREATE, null);
314
315         MutableSimpleNode<Object> lfBoolean = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfBoolean"),
316                 cont, Boolean.TRUE, ModifyAction.CREATE, null);
317         MutableSimpleNode<Object> lfLfref = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfLfref"), cont,
318                 "true", ModifyAction.CREATE, null);
319         cont.getValue().add(lfBoolean);
320         cont.getValue().add(lfLfref);
321         cont.init();
322
323         return cont;
324     }
325
326 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.