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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.model.statements.yang.test;
23 import static org.junit.Assert.assertTrue;
25 import java.math.BigDecimal;
26 import java.util.Arrays;
28 import org.junit.Test;
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;
37 public class DataTypeTest extends YangTestCommon {
40 public void testDataTypes() {
42 severityCalculator.suppressFinding(ParserFindingType.P143_ENUM_WITHOUT_VALUE.toString());
43 severityCalculator.suppressFinding(ParserFindingType.P144_BIT_WITHOUT_POSITION.toString());
45 parseRelativeImplementsYangModels(Arrays.asList("data-type-test/data-type-test.yang"));
47 final YModule module = getModule("data-type-test-module");
48 final YContainer cont1 = getContainer(module, "cont1");
50 assertSubTreeNoFindings(cont1);
52 assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf1").getType().getDataType()) == YangDataType.INT8);
54 assertTrue(getLeaf(cont1, "leaf1").getType().getRange().getBoundaries().size() == 1);
55 assertTrue(getLeaf(cont1, "leaf1").getType().getRange().getBoundaries().get(0).lower.compareTo(BigDecimal.valueOf(
57 assertTrue(getLeaf(cont1, "leaf1").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
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(
63 assertTrue(getLeaf(cont1, "leaf2").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
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(
69 assertTrue(getLeaf(cont1, "leaf3").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
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(
75 assertTrue(getLeaf(cont1, "leaf4").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
76 Long.MAX_VALUE)) == 0);
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(
81 assertTrue(getLeaf(cont1, "leaf5").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
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(
87 assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
89 assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
91 assertTrue(getLeaf(cont1, "leaf6").getType().getRange().getBoundaries().get(1).upper.compareTo(BigDecimal.valueOf(
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(
97 assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
99 assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
101 assertTrue(getLeaf(cont1, "leaf7").getType().getRange().getBoundaries().get(1).upper.compareTo(BigDecimal.valueOf(
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(
107 assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
109 assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
111 assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(1).upper.compareTo(BigDecimal.valueOf(
113 assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(2).lower.compareTo(BigDecimal.valueOf(
115 assertTrue(getLeaf(cont1, "leaf8").getType().getRange().getBoundaries().get(2).upper.compareTo(BigDecimal.valueOf(
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(
123 assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(0).upper.compareTo(BigDecimal.valueOf(
125 assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(1).lower.compareTo(BigDecimal.valueOf(
127 assertTrue(getLeaf(cont1, "leaf9").getType().getRange().getBoundaries().get(1).upper.compareTo(new BigDecimal(
128 "922337203685477580.7")) == 0);
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"));
136 assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf22").getType()
137 .getDataType()) == YangDataType.BOOLEAN);
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(
146 assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(1).getEnumName().equals("two"));
147 assertTrue(getLeaf(cont1, "leaf23").getType().getEnums().get(2).getEnumName().equals("three"));
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(
155 assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(1).getBitName().equals("two"));
156 assertTrue(getLeaf(cont1, "leaf24").getType().getBits().get(2).getBitName().equals("three"));
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);
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());
167 assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf27").getType()
168 .getDataType()) == YangDataType.IDENTITYREF);
169 assertTrue(getLeaf(cont1, "leaf27").getType().getBases().get(0).getValue().equals("identity1"));
171 assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf28").getType().getDataType()) == YangDataType.EMPTY);
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);
179 assertTrue(DataTypeHelper.getYangDataType(getLeaf(cont1, "leaf30").getType()
180 .getDataType()) == YangDataType.INSTANCE_IDENTIFIER);
181 assertTrue(getLeaf(cont1, "leaf30").getType().getRequireInstance().isRequireInstanceFalse());
183 // - - - - - container2 - - - - -
185 final YContainer cont2 = getContainer(module, "cont2");
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());
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());
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());
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());
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());