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