be28623a581ec28c02bda3119b73272e2d79b537
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.model.statements.yang.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.math.BigDecimal;
26 import java.util.Arrays;
27
28 import org.junit.Test;
29
30 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
31 import org.oran.smo.yangtools.parser.model.statements.yang.YContainer;
32 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
33 import org.oran.smo.yangtools.parser.model.util.DataTypeHelper;
34 import org.oran.smo.yangtools.parser.model.util.DataTypeHelper.YangDataType;
35 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
36
37 public class DataTypeTest extends YangTestCommon {
38
39     @Test
40     public void testDataTypes() {
41
42         severityCalculator.suppressFinding(ParserFindingType.P143_ENUM_WITHOUT_VALUE.toString());
43         severityCalculator.suppressFinding(ParserFindingType.P144_BIT_WITHOUT_POSITION.toString());
44
45         parseRelativeImplementsYangModels(Arrays.asList("data-type-test/data-type-test.yang"));
46
47         final YModule module = getModule("data-type-test-module");
48         final YContainer cont1 = getContainer(module, "cont1");
49
50         assertSubTreeNoFindings(cont1);
51
52         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf1").getType().getDataType()) == YangDataType.INT8);
53
54         assertTrue(getLeaf(cont1, "leaf1").getType().getRange().getBoundaries().size() == 1);
55         assertTrue(getLeaf(cont1, "leaf1").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
56                 -128)) == 0);
57         assertTrue(getLeaf(cont1, "leaf1").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
58                 127)) == 0);
59
60         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf2").getType().getDataType()) == YangDataType.INT16);
61         assertTrue(getLeaf(cont1, "leaf2").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
62                 10)) == 0);
63         assertTrue(getLeaf(cont1, "leaf2").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
64                 20)) == 0);
65
66         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf3").getType().getDataType()) == YangDataType.INT32);
67         assertTrue(getLeaf(cont1, "leaf3").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
68                 -30)) == 0);
69         assertTrue(getLeaf(cont1, "leaf3").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
70                 20)) == 0);
71
72         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf4").getType().getDataType()) == YangDataType.INT64);
73         assertTrue(getLeaf(cont1, "leaf4").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
74                 20)) == 0);
75         assertTrue(getLeaf(cont1, "leaf4").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
76                 Long.MAX_VALUE)) == 0);
77
78         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf5").getType().getDataType()) == YangDataType.UINT8);
79         assertTrue(getLeaf(cont1, "leaf5").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
80                 10)) == 0);
81         assertTrue(getLeaf(cont1, "leaf5").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
82                 10)) == 0);
83
84         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf6").getType().getDataType()) == YangDataType.UINT16);
85         assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
86                 0)) == 0);
87         assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
88                 20)) == 0);
89         assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
90                 40)) == 0);
91         assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(1).upper.compareTo(BigDecimal.valueOf(
92                 40)) == 0);
93
94         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf7").getType().getDataType()) == YangDataType.UINT32);
95         assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
96                 0)) == 0);
97         assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
98                 20)) == 0);
99         assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
100                 40)) == 0);
101         assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(1).upper.compareTo(BigDecimal.valueOf(
102                 60)) == 0);
103
104         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf8").getType().getDataType()) == YangDataType.UINT64);
105         assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
106                 0)) == 0);
107         assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
108                 20)) == 0);
109         assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
110                 30)) == 0);
111         assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(1).upper.compareTo(BigDecimal.valueOf(
112                 30)) == 0);
113         assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(2).lower.compareTo(BigDecimal.valueOf(
114                 40)) == 0);
115         assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(2).upper.compareTo(BigDecimal.valueOf(
116                 60)) == 0);
117
118         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf9").getType()
119                 .getDataType()) == YangDataType.DECIMAL64);
120         assertTrue(getLeaf(cont1, "leaf9").getType().getFractionDigits().getFractionDigits() == 1);
121         assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
122                 0)) == 0);
123         assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
124                 10)) == 0);
125         assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
126                 20.2d)) == 0);
127         assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(1).upper.compareTo(new BigDecimal(
128                 "922337203685477580.7")) == 0);
129
130         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf21").getType().getDataType()) == YangDataType.STRING);
131         assertTrue(getLeaf(cont1, "leaf21").getType().getLength().getBoundaries().get(0).lower == 10L);
132         assertTrue(getLeaf(cont1, "leaf21").getType().getLength().getBoundaries().get(0).upper == 20L);
133         assertTrue(getLeaf(cont1, "leaf21").getType().getPatterns().get(0).getPattern().equals("ab*c"));
134         assertTrue(getLeaf(cont1, "leaf21").getType().getPatterns().get(0).getModifier().getValue().equals("invert-match"));
135
136         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf22").getType()
137                 .getDataType()) == YangDataType.BOOLEAN);
138
139         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf23").getType()
140                 .getDataType()) == YangDataType.ENUMERATION);
141         assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(0).getEnumName().equals("one"));
142         assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(0).getValue().getEnumValue() == 10);
143         assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(0).getStatus().isDeprecated());
144         assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(0).getIfFeatures().get(0).getValue().equals(
145                 "feature1"));
146         assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(1).getEnumName().equals("two"));
147         assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(2).getEnumName().equals("three"));
148
149         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf24").getType().getDataType()) == YangDataType.BITS);
150         assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(0).getBitName().equals("one"));
151         assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(0).getPosition().getPosition() == 10);
152         assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(0).getStatus().isDeprecated());
153         assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(0).getIfFeatures().get(0).getValue().equals(
154                 "feature1"));
155         assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(1).getBitName().equals("two"));
156         assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(2).getBitName().equals("three"));
157
158         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf25").getType().getDataType()) == YangDataType.BINARY);
159         assertTrue(getLeaf(cont1, "leaf25").getType().getLength().getBoundaries().get(0).lower == 90);
160         assertTrue(getLeaf(cont1, "leaf25").getType().getLength().getBoundaries().get(0).upper == 1010);
161
162         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf26").getType()
163                 .getDataType()) == YangDataType.LEAFREF);
164         assertTrue(getLeaf(cont1, "leaf26").getType().getPath().getValue().equals("/this:cont1/this:leaf1"));
165         assertTrue(getLeaf(cont1, "leaf26").getType().getRequireInstance().isRequireInstanceTrue());
166
167         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf27").getType()
168                 .getDataType()) == YangDataType.IDENTITYREF);
169         assertTrue(getLeaf(cont1, "leaf27").getType().getBases().get(0).getValue().equals("identity1"));
170
171         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf28").getType().getDataType()) == YangDataType.EMPTY);
172
173         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf29").getType().getDataType()) == YangDataType.UNION);
174         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf29").getType().getTypes().get(0)
175                 .getDataType()) == YangDataType.INT32);
176         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf29").getType().getTypes().get(1)
177                 .getDataType()) == YangDataType.STRING);
178
179         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf30").getType()
180                 .getDataType()) == YangDataType.INSTANCE_IDENTIFIER);
181         assertTrue(getLeaf(cont1, "leaf30").getType().getRequireInstance().isRequireInstanceFalse());
182
183         // - - - - - container2 - - - - -
184
185         final YContainer cont2 = getContainer(module, "cont2");
186
187         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont2, "leaf51").getType()
188                 .getDataType()) == YangDataType.DECIMAL64);
189         assertStatementHasFindingOfType(getLeaf(cont2, "leaf51").getType().getFractionDigits(),
190                 ParserFindingType.P053_INVALID_VALUE.toString());
191         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont2, "leaf52").getType()
192                 .getDataType()) == YangDataType.DECIMAL64);
193         assertStatementHasFindingOfType(getLeaf(cont2, "leaf52").getType().getFractionDigits(),
194                 ParserFindingType.P053_INVALID_VALUE.toString());
195         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont2, "leaf53").getType()
196                 .getDataType()) == YangDataType.DECIMAL64);
197         assertStatementHasFindingOfType(getLeaf(cont2, "leaf53").getType().getFractionDigits(),
198                 ParserFindingType.P053_INVALID_VALUE.toString());
199         assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont2, "leaf54").getType()
200                 .getDataType()) == YangDataType.DECIMAL64);
201         assertStatementHasFindingOfType(getLeaf(cont2, "leaf54").getType().getFractionDigits(),
202                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
203
204         assertStatementHasFindingOfType(getLeaf(cont2, "leaf58").getType().getBits().get(0).getPosition(),
205                 ParserFindingType.P053_INVALID_VALUE.toString());
206         assertStatementHasFindingOfType(getLeaf(cont2, "leaf58").getType().getBits().get(1).getPosition(),
207                 ParserFindingType.P053_INVALID_VALUE.toString());
208         assertStatementHasFindingOfType(getLeaf(cont2, "leaf58").getType().getBits().get(2).getPosition(),
209                 ParserFindingType.P053_INVALID_VALUE.toString());
210         assertStatementHasFindingOfType(getLeaf(cont2, "leaf58").getType().getBits().get(3).getPosition(),
211                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
212
213         assertNoFindingsOnStatement(getLeafList(cont2, "leaflist62").getMinElements());
214         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist63").getMinElements(),
215                 ParserFindingType.P053_INVALID_VALUE.toString());
216         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist64").getMinElements(),
217                 ParserFindingType.P053_INVALID_VALUE.toString());
218         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist65").getMinElements(),
219                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
220
221         assertNoFindingsOnStatement(getLeafList(cont2, "leaflist71").getMaxElements());
222         assertNoFindingsOnStatement(getLeafList(cont2, "leaflist72").getMaxElements());
223         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist73").getMaxElements(),
224                 ParserFindingType.P053_INVALID_VALUE.toString());
225         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist74").getMaxElements(),
226                 ParserFindingType.P053_INVALID_VALUE.toString());
227         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist75").getMaxElements(),
228                 ParserFindingType.P053_INVALID_VALUE.toString());
229         assertStatementHasFindingOfType(getLeafList(cont2, "leaflist76").getMaxElements(),
230                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
231
232         assertNoFindingsOnStatement(getLeaf(cont2, "leaf81").getType().getPatterns().get(0).getModifier());
233         assertStatementHasFindingOfType(getLeaf(cont2, "leaf82").getType().getPatterns().get(0).getModifier(),
234                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
235         assertStatementHasFindingOfType(getLeaf(cont2, "leaf83").getType().getPatterns().get(0).getModifier(),
236                 ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
237     }
238 }