Bump versions to 14.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / BitAndUnionTOEnclosingTest.java
1 /*
2  * Copyright (c) 2016 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.mdsal.binding.generator.impl;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsAttributes;
15 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsMethods;
16
17 import java.util.List;
18 import java.util.stream.Collectors;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25
26 public class BitAndUnionTOEnclosingTest {
27
28     private static List<GeneratedType> genTypes = null;
29     private static GeneratedType parentContainer = null;
30
31     @BeforeClass
32     public static void loadTestResources() {
33         genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource("/bit_and_union.yang"));
34
35         for (GeneratedType genType : genTypes) {
36             if (genType.getName().equals("ParentContainer") && !(genType instanceof GeneratedTransferObject)) {
37                 parentContainer = genType;
38             }
39         }
40     }
41
42     @Test
43     public void testNestedTypesInLeaf() {
44         final List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
45         assertEquals(3, enclosedTypes.size());
46
47         // nested types in leaf
48         final List<GeneratedTransferObject> lfLeafs = enclosedTypes.stream()
49             .filter(genType -> genType.getName().equals("Lf"))
50             .map(genType -> {
51                 assertThat(genType, instanceOf(GeneratedTransferObject.class));
52                 return (GeneratedTransferObject) genType;
53             })
54             .collect(Collectors.toList());
55         assertEquals("Lf TO has incorrect number of occurences.", 1, lfLeafs.size());
56         GeneratedTransferObject lfLeaf = lfLeafs.get(0);
57         assertEquals("Lf has incorrect package name.",
58             "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
59             lfLeaf.getIdentifier().immediatelyEnclosingClass().orElseThrow().toString());
60
61         assertEquals("Lf generated TO has incorrect number of properties", 2, lfLeaf.getProperties().size());
62         containsAttributes(lfLeaf, true, true, true, new NameTypePattern("string", "String"));
63         containsAttributes(lfLeaf, true, false, true, new NameTypePattern("lf$1", "Lf$1"));
64
65         // nested types in Lf
66         final List<GeneratedType> lfTypes = lfLeaf.getEnclosedTypes();
67         assertEquals(1, lfTypes.size());
68
69         final GeneratedType lf1Leaf = lfTypes.get(0);
70         assertEquals("Lf$1", lf1Leaf.getName());
71         assertEquals("Lf$1 has incorrect package name.",
72             "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer.Lf",
73             lf1Leaf.getIdentifier().immediatelyEnclosingClass().orElseThrow().toString());
74
75         assertThat(lf1Leaf, instanceOf(GeneratedTransferObject.class));
76         final GeneratedTransferObject lf1gto = (GeneratedTransferObject) lf1Leaf;
77         assertEquals("Lf$1 generated TO has incorrect number of properties", 4, lf1Leaf.getProperties().size());
78         containsAttributes(lf1gto, true, true, true, new NameTypePattern("uint32", "Uint32"));
79         containsAttributes(lf1gto, true, true, true, new NameTypePattern("int8", "Byte"));
80         containsAttributes(lf1gto, true, true, true, new NameTypePattern("string", "String"));
81         containsAttributes(lf1gto, true, false, true, new NameTypePattern("lf$2", "Lf$2"));
82
83         // nested types in Lf1
84         final List<GeneratedType> lf1Types = lf1Leaf.getEnclosedTypes();
85         assertEquals(1, lf1Types.size());
86
87         final GeneratedType lf2Leaf = lf1Types.get(0);
88         assertEquals("Lf$2", lf2Leaf.getName());
89         assertEquals("Lf$2 has incorrect package name.",
90             "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer.Lf.Lf$1",
91             lf2Leaf.getIdentifier().immediatelyEnclosingClass().orElseThrow().toString());
92
93
94         assertThat(lf2Leaf, instanceOf(GeneratedTransferObject.class));
95         final GeneratedTransferObject lf2gto = (GeneratedTransferObject) lf2Leaf;
96         assertEquals("Lf generated TO has incorrect number of properties", 2, lf2Leaf.getProperties().size());
97         containsAttributes(lf2gto, true, true, true, new NameTypePattern("string", "String"));
98         containsAttributes(lf2gto, true, true, true, new NameTypePattern("uint64", "Uint64"));
99     }
100
101     @Test
102     public void testNestedTypesInTypedef() {
103
104         GeneratedTransferObject typeUnionTypedef = null;
105         int typeUnionTypedefCounter = 0;
106
107         for (GeneratedType genType : genTypes) {
108             if (genType.getName().equals("TypeUnion") && genType instanceof GeneratedTransferObject) {
109                 typeUnionTypedef = (GeneratedTransferObject) genType;
110                 typeUnionTypedefCounter++;
111             }
112         }
113
114         assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
115         assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
116
117         assertNotNull("TypeUnion TO wasn't found.", typeUnionTypedef);
118         assertEquals("TypeUnion TO has incorrect number of occurences.", 1, typeUnionTypedefCounter);
119         assertEquals("TypeUnion has incorrect package name.",
120                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnionTypedef.getPackageName());
121
122         assertEquals("TypeUnion generated TO has incorrect number of properties", 2, typeUnionTypedef.getProperties()
123                 .size());
124         containsAttributes(typeUnionTypedef, true, true, true, new NameTypePattern("string", "String"));
125         containsAttributes(typeUnionTypedef, true, false, true, new NameTypePattern("typeUnion$1", "TypeUnion$1"));
126
127         List<GeneratedType> nestedUnions = typeUnionTypedef.getEnclosedTypes();
128         assertEquals("Incorrect number of nested unions", 1, nestedUnions.size());
129
130         GeneratedType typeUnion1 = nestedUnions.get(0);
131         assertEquals("TypeUnion$1", typeUnion1.getName());
132         assertEquals("TypeUnion$1 has incorrect package name.",
133             "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion1.getPackageName());
134         assertEquals("TypeUnion1 generated TO has incorrect number of properties", 4,
135             typeUnion1.getProperties().size());
136
137         assertThat(typeUnion1, instanceOf(GeneratedTransferObject.class));
138         GeneratedTransferObject typeUnion1gto = (GeneratedTransferObject) typeUnion1;
139         containsAttributes(typeUnion1gto, true, true, true, new NameTypePattern("uint32", "Uint32"));
140         containsAttributes(typeUnion1gto, true, true, true, new NameTypePattern("int8", "Byte"));
141         containsAttributes(typeUnion1gto, true, true, true, new NameTypePattern("string", "String"));
142         containsAttributes(typeUnion1gto, true, false, true, new NameTypePattern("typeUnion$2", "TypeUnion$2"));
143
144
145         List<GeneratedType> nestedUnions1 = typeUnion1.getEnclosedTypes();
146         assertEquals(1, nestedUnions1.size());
147
148         GeneratedType typeUnion2 = nestedUnions1.get(0);
149         assertEquals("TypeUnion$2", typeUnion2.getName());
150         assertEquals("TypeUnion$2 has incorrect package name.",
151             "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626", typeUnion2.getPackageName());
152         assertEquals("TypeUnion2 generated TO has incorrect number of properties", 2,
153             typeUnion2.getProperties().size());
154
155         assertThat(typeUnion2, instanceOf(GeneratedTransferObject.class));
156         GeneratedTransferObject typeUnion2gto = (GeneratedTransferObject) typeUnion2;
157         containsAttributes(typeUnion2gto, true, true, true, new NameTypePattern("string", "String"));
158         containsAttributes(typeUnion2gto, true, true, true, new NameTypePattern("uint64", "Uint64"));
159     }
160
161     @Test
162     public void bitAndUnionEnclosingTest() {
163
164         assertNotNull("Parent container object wasn't found.", parentContainer);
165         containsMethods(parentContainer, new NameTypePattern("getLf", "Lf"));
166
167         GeneratedTransferObject bitLeaf = null;
168         GeneratedTransferObject unionLeaf = null;
169         List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
170         for (GeneratedType genType : enclosedTypes) {
171             if (genType instanceof GeneratedTransferObject) {
172                 if (genType.getName().equals("BitLeaf")) {
173                     bitLeaf = (GeneratedTransferObject) genType;
174                 } else if (genType.getName().equals("UnionLeaf")) {
175                     unionLeaf = (GeneratedTransferObject) genType;
176                 }
177             }
178         }
179
180         assertNotNull("BitLeaf TO wasn't found.", bitLeaf);
181         assertNotNull("UnionLeaf TO wasn't found.", unionLeaf);
182
183         assertEquals("BitLeaf has incorrect package name.",
184                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
185                 bitLeaf.getIdentifier().immediatelyEnclosingClass().orElseThrow().toString());
186         assertEquals("UnionLeaf has incorrect package name.",
187                 "org.opendaylight.yang.gen.v1.urn.bit.union.in.leaf.rev130626.ParentContainer",
188                 unionLeaf.getIdentifier().immediatelyEnclosingClass().orElseThrow().toString());
189
190         List<GeneratedProperty> propertiesBitLeaf = bitLeaf.getProperties();
191         GeneratedProperty firstBitProperty = null;
192         GeneratedProperty secondBitProperty = null;
193         GeneratedProperty thirdBitProperty = null;
194
195         for (GeneratedProperty genProperty : propertiesBitLeaf) {
196             if (genProperty.getName().equals("firstBit")) {
197                 firstBitProperty = genProperty;
198             } else if (genProperty.getName().equals("secondBit")) {
199                 secondBitProperty = genProperty;
200             } else if (genProperty.getName().equals("thirdBit")) {
201                 thirdBitProperty = genProperty;
202             }
203         }
204
205         assertNotNull("firstBit property wasn't found", firstBitProperty);
206         assertNotNull("secondBit property wasn't found", secondBitProperty);
207         assertNotNull("thirdBit property wasn't found", thirdBitProperty);
208
209         assertEquals("firstBit property has incorrect type", "boolean", firstBitProperty.getReturnType().getName());
210         assertEquals("secondBit property has incorrect type", "boolean", secondBitProperty.getReturnType().getName());
211         assertEquals("thirdBit property has incorrect type", "boolean", thirdBitProperty.getReturnType().getName());
212
213         GeneratedProperty uint32Property = null;
214         GeneratedProperty stringProperty = null;
215         GeneratedProperty uint8Property = null;
216         List<GeneratedProperty> propertiesUnionLeaf = unionLeaf.getProperties();
217         for (GeneratedProperty genProperty : propertiesUnionLeaf) {
218             if (genProperty.getName().equals("int32")) {
219                 uint32Property = genProperty;
220             } else if (genProperty.getName().equals("string")) {
221                 stringProperty = genProperty;
222             } else if (genProperty.getName().equals("uint8")) {
223                 uint8Property = genProperty;
224             }
225         }
226
227         assertNotNull("uint32 property wasn't found", uint32Property);
228         assertNotNull("string property wasn't found", stringProperty);
229         assertNotNull("uint8 property wasn't found", uint8Property);
230
231         assertEquals("uint32 property has incorrect type", "Integer", uint32Property.getReturnType().getName());
232         assertEquals("string property has incorrect type", "String", stringProperty.getReturnType().getName());
233         assertEquals("uint8 property has incorrect type", "Uint8", uint8Property.getReturnType().getName());
234
235     }
236
237 }