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

©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.