70029e8e9d446c437316b45afeca8a0531080f50
[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.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.binding.model.api.GeneratedProperty;
18 import org.opendaylight.yangtools.binding.model.api.GeneratedTransferObject;
19 import org.opendaylight.yangtools.binding.model.api.MethodSignature;
20 import org.opendaylight.yangtools.binding.model.api.MethodSignature.Parameter;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22
23 public class GeneratedTypesBitsTest {
24     @Test
25     public void testGeneretedTypesBitsTest() {
26         final var genTypes = DefaultBindingGenerator.generateFor(YangParserTestUtils.parseYangResource(
27             "/simple-bits-demo.yang"));
28         assertTrue(genTypes != null);
29
30         List<MethodSignature> methodSignaturesList = null;
31
32         boolean leafParentFound = false;
33
34         boolean byteTypeFound = false;
35         int classPropertiesNumb = 0;
36         int toStringPropertiesNum = 0;
37         int equalPropertiesNum = 0;
38         int hashPropertiesNum = 0;
39
40         String nameReturnParamType = "";
41         boolean getByteLeafMethodFound = false;
42         boolean setByteLeafMethodFound = false;
43         int setByteLeafMethodParamNum = 0;
44
45         for (var genType : genTypes) {
46             if (genType instanceof GeneratedTransferObject genTO) {
47                 if (genTO.getName().equals("ByteType")) {
48                     byteTypeFound = true;
49                     List<GeneratedProperty> genProperties = genTO.getProperties();
50                     classPropertiesNumb = genProperties.size();
51
52                     genProperties = genTO.getToStringIdentifiers();
53                     toStringPropertiesNum = genProperties.size();
54
55                     genProperties = genTO.getEqualsIdentifiers();
56                     equalPropertiesNum = genProperties.size();
57
58                     genProperties = genTO.getHashCodeIdentifiers();
59                     hashPropertiesNum = genProperties.size();
60
61                 }
62             } else if (genType.getName().equals("LeafParentContainer")) {
63                 leafParentFound = true;
64                 // check of methods
65                 methodSignaturesList = genType.getMethodDefinitions();
66                 if (methodSignaturesList != null) {
67                     // loop through all methods
68                     for (var methodSignature : methodSignaturesList) {
69                         if (methodSignature.getName().equals("getByteLeaf")) {
70                             getByteLeafMethodFound = true;
71
72                             nameReturnParamType = methodSignature.getReturnType().getName();
73                         } else if (methodSignature.getName().equals("setByteLeaf")) {
74                             setByteLeafMethodFound = true;
75
76                             List<Parameter> parameters = methodSignature.getParameters();
77                             setByteLeafMethodParamNum = parameters.size();
78                         }
79                     }
80                 }
81             }
82         }
83
84         assertTrue(byteTypeFound);
85
86         assertEquals(8, classPropertiesNumb);
87
88         assertEquals(8, toStringPropertiesNum);
89         assertEquals(8, equalPropertiesNum);
90         assertEquals(8, hashPropertiesNum);
91         assertTrue(leafParentFound);
92
93         assertNotNull(methodSignaturesList);
94
95         assertTrue(getByteLeafMethodFound);
96         assertEquals("ByteType", nameReturnParamType);
97
98         assertFalse(setByteLeafMethodFound);
99         assertEquals(0, setByteLeafMethodParamNum);
100     }
101 }