2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.generator.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
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.mdsal.binding.model.ri.Types;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class GenerateInnerClassForBitsAndUnionInLeavesTest {
25 public void testInnerClassCreationForBitsAndUnionsInLeafes() {
26 final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
27 "/bit_and_union_in_leaf.yang"));
28 assertEquals(4, genTypes.size());
30 boolean parentContainerFound = false;
31 boolean bitLeafTOFound = false;
32 boolean unionLeafTOFound = false;
34 for (GeneratedType 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 (GeneratedType genType : enclosedTypes) {
41 if (genType instanceof GeneratedTransferObject) {
42 final GeneratedTransferObject gto = (GeneratedTransferObject) genType;
44 if (genType.getName().equals("BitLeaf")) {
45 assertFalse("Unexpected duplicate BitLeaf", bitLeafTOFound);
46 bitLeafTOFound = true;
48 List<GeneratedProperty> bitLeafProperties = gto.getProperties();
49 assertEquals(3, bitLeafProperties.size());
51 boolean firstBitPropertyFound = false;
52 boolean secondBitPropertyFound = false;
53 boolean thirdBitPropertyFound = false;
54 for (GeneratedProperty bitLeafProperty : bitLeafProperties) {
55 if (bitLeafProperty.getName().equals("firstBit")) {
56 firstBitPropertyFound = true;
57 assertEquals(Types.primitiveBooleanType(), bitLeafProperty.getReturnType());
58 } else if (bitLeafProperty.getName().equals("secondBit")) {
59 secondBitPropertyFound = true;
60 assertEquals(Types.primitiveBooleanType(), bitLeafProperty.getReturnType());
61 } else if (bitLeafProperty.getName().equals("thirdBit")) {
62 thirdBitPropertyFound = true;
63 assertEquals(Types.primitiveBooleanType(), bitLeafProperty.getReturnType());
66 assertTrue(firstBitPropertyFound);
67 assertTrue(secondBitPropertyFound);
68 assertTrue(thirdBitPropertyFound);
69 } else if (genType.getName().equals("UnionLeaf")) {
70 assertFalse("Unexpected duplicate UnionLeaf", unionLeafTOFound);
71 unionLeafTOFound = true;
73 List<GeneratedProperty> unionLeafProperties = gto.getProperties();
74 assertEquals(3, unionLeafProperties.size());
76 boolean int32UnionPropertyFound = false;
77 boolean stringUnionPropertyFound = false;
78 boolean uint8UnionPropertyFound = false;
79 for (GeneratedProperty unionLeafProperty : unionLeafProperties) {
80 if (unionLeafProperty.getName().equals("int32")) {
81 int32UnionPropertyFound = true;
82 assertEquals(BaseYangTypes.INT32_TYPE, unionLeafProperty.getReturnType());
83 } else if (unionLeafProperty.getName().equals("string")) {
84 stringUnionPropertyFound = true;
85 assertEquals(BaseYangTypes.STRING_TYPE, unionLeafProperty.getReturnType());
86 } else if (unionLeafProperty.getName().equals("uint8")) {
87 uint8UnionPropertyFound = true;
88 assertEquals(BaseYangTypes.UINT8_TYPE, unionLeafProperty.getReturnType());
91 assertTrue(int32UnionPropertyFound);
92 assertTrue(stringUnionPropertyFound);
93 assertTrue(uint8UnionPropertyFound);
100 assertTrue(parentContainerFound);
101 assertTrue(bitLeafTOFound);
102 assertTrue(unionLeafTOFound);