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