Do not generate union builders
[mdsal.git] / binding / mdsal-binding-generator / src / test / java / org / opendaylight / mdsal / binding / generator / impl / GenerateInnerClassForBitsAndUnionInLeavesTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import java.util.List;
15 import org.junit.Test;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.ri.BaseYangTypes;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
21
22 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
23     @Test
24     public void testInnerClassCreationForBitsAndUnionsInLeafes() {
25         final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
26             "/bit_and_union_in_leaf.yang"));
27         assertEquals(4, genTypes.size());
28
29         boolean parentContainerFound = false;
30         boolean bitLeafTOFound = false;
31         boolean unionLeafTOFound = false;
32
33         for (GeneratedType type : genTypes) {
34             if (!(type instanceof GeneratedTransferObject)) {
35                 if (type.getName().equals("ParentContainer")) {
36                     parentContainerFound = true;
37                     GeneratedType parentContainer = type;
38                     List<GeneratedType> enclosedTypes = parentContainer.getEnclosedTypes();
39                     for (GeneratedType genType : enclosedTypes) {
40                         if (genType instanceof GeneratedTransferObject) {
41                             final GeneratedTransferObject gto = (GeneratedTransferObject) genType;
42
43                             if (genType.getName().equals("BitLeaf")) {
44                                 assertFalse("Unexpected duplicate BitLeaf", bitLeafTOFound);
45                                 bitLeafTOFound = true;
46
47                                 List<GeneratedProperty> bitLeafProperties = gto.getProperties();
48                                 assertEquals(3, bitLeafProperties.size());
49
50                                 boolean firstBitPropertyFound = false;
51                                 boolean secondBitPropertyFound = false;
52                                 boolean thirdBitPropertyFound = false;
53                                 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
54                                     if (bitLeafProperty.getName().equals("firstBit")) {
55                                         firstBitPropertyFound = true;
56                                         assertEquals(BaseYangTypes.BOOLEAN_TYPE, bitLeafProperty.getReturnType());
57                                     } else if (bitLeafProperty.getName().equals("secondBit")) {
58                                         secondBitPropertyFound = true;
59                                         assertEquals(BaseYangTypes.BOOLEAN_TYPE, bitLeafProperty.getReturnType());
60                                     } else if (bitLeafProperty.getName().equals("thirdBit")) {
61                                         thirdBitPropertyFound = true;
62                                         assertEquals(BaseYangTypes.BOOLEAN_TYPE, bitLeafProperty.getReturnType());
63                                     }
64                                 }
65                                 assertTrue(firstBitPropertyFound);
66                                 assertTrue(secondBitPropertyFound);
67                                 assertTrue(thirdBitPropertyFound);
68                             } else if (genType.getName().equals("UnionLeaf")) {
69                                 assertFalse("Unexpected duplicate UnionLeaf", unionLeafTOFound);
70                                 unionLeafTOFound = true;
71
72                                 List<GeneratedProperty> unionLeafProperties = gto.getProperties();
73                                 assertEquals(3, unionLeafProperties.size());
74
75                                 boolean int32UnionPropertyFound = false;
76                                 boolean stringUnionPropertyFound = false;
77                                 boolean uint8UnionPropertyFound = false;
78                                 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
79                                     if (unionLeafProperty.getName().equals("int32")) {
80                                         int32UnionPropertyFound = true;
81                                         assertEquals(BaseYangTypes.INT32_TYPE, unionLeafProperty.getReturnType());
82                                     } else if (unionLeafProperty.getName().equals("string")) {
83                                         stringUnionPropertyFound = true;
84                                         assertEquals(BaseYangTypes.STRING_TYPE, unionLeafProperty.getReturnType());
85                                     } else if (unionLeafProperty.getName().equals("uint8")) {
86                                         uint8UnionPropertyFound = true;
87                                         assertEquals(BaseYangTypes.UINT8_TYPE, unionLeafProperty.getReturnType());
88                                     }
89                                 }
90                                 assertTrue(int32UnionPropertyFound);
91                                 assertTrue(stringUnionPropertyFound);
92                                 assertTrue(uint8UnionPropertyFound);
93                             }
94                         }
95                     }
96                 }
97             }
98         }
99         assertTrue(parentContainerFound);
100         assertTrue(bitLeafTOFound);
101         assertTrue(unionLeafTOFound);
102     }
103 }