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.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.CY;
32 import org.oran.smo.yangtools.parser.model.statements.yang.YLeaf;
33 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
34 import org.oran.smo.yangtools.parser.model.statements.yang.YTypedef;
35 import org.oran.smo.yangtools.parser.model.util.DataTypeHelper;
36 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
39 * This test verifies bits/bit statements
41 public class BitsTest extends YangTestCommon {
44 public void testBits() {
46 severityCalculator.suppressFinding(ParserFindingType.P114_TYPEDEF_NOT_USED.toString());
47 severityCalculator.suppressFinding(ParserFindingType.P115_TYPEDEF_USED_ONCE_ONLY.toString());
48 severityCalculator.suppressFinding(ParserFindingType.P144_BIT_WITHOUT_POSITION.toString());
50 parseRelativeImplementsYangModels(Arrays.asList("bits-test/bits-test-module.yang"));
52 final YModule module = getModule("bits-test-module");
53 assertTrue(module != null);
55 final YTypedef typedef1 = getTypedefForModule("bits-test-module", "typedef1");
56 assertTrue(typedef1 != null);
57 final Map<String, Long> positionOfBitsTypedef1 = DataTypeHelper.calculatePositionOfBits(context
58 .getFindingsManager(), typedef1.getType(), null);
59 assertTrue(positionOfBitsTypedef1.containsKey("first"));
60 assertTrue(positionOfBitsTypedef1.containsKey("second"));
61 assertTrue(positionOfBitsTypedef1.containsKey("third"));
62 assertTrue(positionOfBitsTypedef1.get("first").longValue() == 0);
63 assertTrue(positionOfBitsTypedef1.get("second").longValue() == 1);
64 assertTrue(positionOfBitsTypedef1.get("third").longValue() == 2);
65 assertSubTreeNoFindings(typedef1);
67 final YTypedef typedef2 = getTypedefForModule("bits-test-module", "typedef2");
68 assertTrue(typedef2 != null);
69 final Map<String, Long> positionOfBitsTypedef2 = DataTypeHelper.calculatePositionOfBits(context
70 .getFindingsManager(), typedef2.getType(), null);
71 assertTrue(positionOfBitsTypedef2.containsKey("first"));
72 assertTrue(positionOfBitsTypedef2.containsKey("second"));
73 assertTrue(positionOfBitsTypedef2.containsKey("third"));
74 assertTrue(positionOfBitsTypedef2.get("first").longValue() == 7);
75 assertTrue(positionOfBitsTypedef2.get("second").longValue() == 23);
76 assertTrue(positionOfBitsTypedef2.get("third").longValue() == 195);
77 assertSubTreeNoFindings(typedef2);
79 final YTypedef typedef3 = getTypedefForModule("bits-test-module", "typedef3");
80 assertTrue(typedef3 != null);
81 final Map<String, Long> positionOfBitsTypedef3 = DataTypeHelper.calculatePositionOfBits(context
82 .getFindingsManager(), typedef3.getType(), null);
83 assertTrue(positionOfBitsTypedef3.containsKey("first"));
84 assertTrue(positionOfBitsTypedef3.containsKey("second"));
85 assertTrue(positionOfBitsTypedef3.containsKey("third"));
86 assertTrue(positionOfBitsTypedef3.get("first").longValue() == 98);
87 assertTrue(positionOfBitsTypedef3.get("second").longValue() == 99);
88 assertTrue(positionOfBitsTypedef3.get("third").longValue() == 100);
89 assertSubTreeNoFindings(typedef3);
91 final YTypedef typedef4 = getTypedefForModule("bits-test-module", "typedef4");
92 assertTrue(typedef4 != null);
93 final Map<String, Long> positionOfBitsTypedef4 = DataTypeHelper.calculatePositionOfBits(context
94 .getFindingsManager(), typedef4.getType(), null);
95 assertTrue(positionOfBitsTypedef4.containsKey("first"));
96 assertTrue(positionOfBitsTypedef4.containsKey("second"));
97 assertTrue(positionOfBitsTypedef4.containsKey("third"));
98 assertTrue(positionOfBitsTypedef4.get("first").longValue() == 0);
99 assertTrue(positionOfBitsTypedef4.get("second").longValue() == 10);
100 assertTrue(positionOfBitsTypedef4.get("third").longValue() == 11);
101 assertSubTreeNoFindings(typedef4);
103 final YTypedef typedefDuplicatePosition1 = getTypedefForModule("bits-test-module", "typedef-duplicate-position1");
104 assertTrue(typedefDuplicatePosition1 != null);
105 final Map<String, Long> positionOfBitsTypedefDuplicatePosition1 = DataTypeHelper.calculatePositionOfBits(context
106 .getFindingsManager(), typedefDuplicatePosition1.getType(), null);
107 assertTrue(positionOfBitsTypedefDuplicatePosition1.containsKey("first"));
108 assertTrue(positionOfBitsTypedefDuplicatePosition1.containsKey("second"));
109 assertTrue(positionOfBitsTypedefDuplicatePosition1.containsKey("third"));
110 assertTrue(positionOfBitsTypedefDuplicatePosition1.get("first").longValue() == 0);
111 assertTrue(positionOfBitsTypedefDuplicatePosition1.get("second").longValue() == 10);
112 assertTrue(positionOfBitsTypedefDuplicatePosition1.get("third").longValue() == 10);
113 assertStatementHasFindingOfType(getChild(typedefDuplicatePosition1.getType(), CY.BIT, "third"),
114 ParserFindingType.P053_INVALID_VALUE.toString());
116 final YTypedef typedefDuplicatePosition2 = getTypedefForModule("bits-test-module", "typedef-duplicate-position2");
117 assertTrue(typedefDuplicatePosition2 != null);
118 final Map<String, Long> positionOfBitsTypedefDuplicatePosition2 = DataTypeHelper.calculatePositionOfBits(context
119 .getFindingsManager(), typedefDuplicatePosition2.getType(), null);
120 assertTrue(positionOfBitsTypedefDuplicatePosition2.containsKey("first"));
121 assertTrue(positionOfBitsTypedefDuplicatePosition2.containsKey("second"));
122 assertTrue(positionOfBitsTypedefDuplicatePosition2.containsKey("third"));
123 assertTrue(positionOfBitsTypedefDuplicatePosition2.get("first").longValue() == 0);
124 assertTrue(positionOfBitsTypedefDuplicatePosition2.get("second").longValue() == 0);
125 assertTrue(positionOfBitsTypedefDuplicatePosition2.get("third").longValue() == 1);
126 assertStatementHasFindingOfType(getChild(typedefDuplicatePosition2.getType(), CY.BIT, "second"),
127 ParserFindingType.P053_INVALID_VALUE.toString());
129 final YTypedef typedefTooLarge1 = getTypedefForModule("bits-test-module", "typedef-too-large1");
130 assertTrue(typedefTooLarge1 != null);
131 final Map<String, Long> positionOfBitsTypedefTooLarge1 = DataTypeHelper.calculatePositionOfBits(context
132 .getFindingsManager(), typedefTooLarge1.getType(), null);
133 assertTrue(positionOfBitsTypedefTooLarge1.containsKey("first"));
134 assertTrue(positionOfBitsTypedefTooLarge1.containsKey("second"));
135 assertTrue(positionOfBitsTypedefTooLarge1.containsKey("third"));
136 assertTrue(positionOfBitsTypedefTooLarge1.get("first").longValue() == 0);
137 assertTrue(positionOfBitsTypedefTooLarge1.get("second").longValue() == 4294967295L);
138 assertTrue(positionOfBitsTypedefTooLarge1.get("third").longValue() == 4294967296L);
139 assertStatementHasFindingOfType(getChild(typedefTooLarge1.getType(), CY.BIT, "third"),
140 ParserFindingType.P053_INVALID_VALUE.toString());
142 final YTypedef typedefTooLarge2 = getTypedefForModule("bits-test-module", "typedef-too-large2");
143 assertTrue(typedefTooLarge2 != null);
144 final Map<String, Long> positionOfBitsTypedefTooLarge2 = DataTypeHelper.calculatePositionOfBits(context
145 .getFindingsManager(), typedefTooLarge2.getType(), null);
146 assertTrue(positionOfBitsTypedefTooLarge2.containsKey("first"));
147 assertTrue(positionOfBitsTypedefTooLarge2.containsKey("second"));
148 assertTrue(positionOfBitsTypedefTooLarge2.containsKey("third"));
149 assertTrue(positionOfBitsTypedefTooLarge2.get("first").longValue() == 0);
150 assertTrue(positionOfBitsTypedefTooLarge2.get("second").longValue() == -1);
151 assertTrue(positionOfBitsTypedefTooLarge2.get("third").longValue() == 0);
152 assertStatementHasFindingOfType(getChild(typedefTooLarge2.getType(), CY.BIT, "third"),
153 ParserFindingType.P053_INVALID_VALUE.toString());
155 // ------------------------------- leafs tests using the typedefs above --------------------------
157 final YLeaf leaf1 = getLeaf(module, "leaf1");
158 assertTrue(leaf1 != null);
159 assertTrue(leaf1.getType() != null);
160 assertTrue(leaf1.getType().getDataType().equals("bits"));
161 final Map<String, Long> positionOfBitsLeaf1 = DataTypeHelper.calculatePositionOfBits(context.getFindingsManager(),
162 leaf1.getType(), positionOfBitsTypedef1);
163 assertTrue(positionOfBitsLeaf1.containsKey("first"));
164 assertTrue(positionOfBitsLeaf1.containsKey("second"));
165 assertTrue(positionOfBitsLeaf1.containsKey("third"));
166 assertTrue(positionOfBitsLeaf1.get("first").longValue() == 0);
167 assertTrue(positionOfBitsLeaf1.get("second").longValue() == 1);
168 assertTrue(positionOfBitsLeaf1.get("third").longValue() == 2);
169 assertSubTreeNoFindings(leaf1);
171 final YLeaf leaf2 = getLeaf(module, "leaf2");
172 assertTrue(leaf2 != null);
173 assertTrue(leaf2.getType() != null);
174 final Map<String, Long> positionOfBitsLeaf2 = DataTypeHelper.calculatePositionOfBits(context.getFindingsManager(),
175 leaf2.getType(), positionOfBitsTypedef2);
176 assertTrue(positionOfBitsLeaf2.containsKey("first") == false);
177 assertTrue(positionOfBitsLeaf2.containsKey("second"));
178 assertTrue(positionOfBitsLeaf2.containsKey("third") == false);
179 assertTrue(positionOfBitsLeaf2.get("second").longValue() == 23);
180 assertSubTreeNoFindings(leaf2);
182 final YLeaf leafWrongPosition1 = getLeaf(module, "leaf-wrong-position1");
183 assertTrue(leafWrongPosition1 != null);
184 assertTrue(leafWrongPosition1.getType() != null);
185 final Map<String, Long> positionOfBitsLeafWrongPosition1 = DataTypeHelper.calculatePositionOfBits(context
186 .getFindingsManager(), leafWrongPosition1.getType(), positionOfBitsTypedef2);
187 assertTrue(positionOfBitsLeafWrongPosition1.containsKey("first"));
188 assertTrue(positionOfBitsLeafWrongPosition1.containsKey("second"));
189 assertTrue(positionOfBitsLeafWrongPosition1.containsKey("third") == false);
190 assertTrue(positionOfBitsLeafWrongPosition1.get("first").longValue() == 7);
191 assertTrue(positionOfBitsLeafWrongPosition1.get("second").longValue() == 24);
192 assertStatementHasFindingOfType(leafWrongPosition1.getType().getBits().get(1),
193 ParserFindingType.P117_ILLEGAL_DATA_TYPE_RESTRICTION.toString());
195 final YLeaf leafWrongPosition2 = getLeaf(module, "leaf-wrong-position2");
196 assertTrue(leafWrongPosition2 != null);
197 assertTrue(leafWrongPosition2.getType() != null);
198 final Map<String, Long> positionOfBitsLeafWrongPosition2 = DataTypeHelper.calculatePositionOfBits(context
199 .getFindingsManager(), leafWrongPosition2.getType(), positionOfBitsTypedef2);
200 assertTrue(positionOfBitsLeafWrongPosition2.containsKey("first"));
201 assertTrue(positionOfBitsLeafWrongPosition2.containsKey("second"));
202 assertTrue(positionOfBitsLeafWrongPosition2.containsKey("third") == false);
203 assertTrue(positionOfBitsLeafWrongPosition2.get("first").longValue() == 7);
204 assertTrue(positionOfBitsLeafWrongPosition2.get("second").longValue() == 24);
205 assertStatementHasFindingOfType(leafWrongPosition2.getType().getBits().get(1),
206 ParserFindingType.P117_ILLEGAL_DATA_TYPE_RESTRICTION.toString());
208 final YLeaf leafNewBit = getLeaf(module, "leaf-new-bit");
209 assertTrue(leafNewBit != null);
210 assertTrue(leafNewBit.getType() != null);
211 final Map<String, Long> positionOfBitsLeafNewBit = DataTypeHelper.calculatePositionOfBits(context
212 .getFindingsManager(), leafNewBit.getType(), positionOfBitsTypedef2);
213 assertTrue(positionOfBitsLeafNewBit.containsKey("first"));
214 assertTrue(positionOfBitsLeafNewBit.containsKey("second"));
215 assertTrue(positionOfBitsLeafNewBit.containsKey("third"));
216 assertTrue(positionOfBitsLeafNewBit.containsKey("fourth"));
217 assertTrue(positionOfBitsLeafNewBit.get("first").longValue() == 7);
218 assertTrue(positionOfBitsLeafNewBit.get("second").longValue() == 23);
219 assertTrue(positionOfBitsLeafNewBit.get("third").longValue() == 195);
220 assertTrue(positionOfBitsLeafNewBit.get("fourth").longValue() == 34);
221 assertStatementHasFindingOfType(leafNewBit.getType().getBits().get(3),
222 ParserFindingType.P117_ILLEGAL_DATA_TYPE_RESTRICTION.toString());
224 final YLeaf leafNotBits = getLeaf(module, "leaf-not-bits");
225 assertStatementHasFindingOfType(leafNotBits, ParserFindingType.P117_ILLEGAL_DATA_TYPE_RESTRICTION.toString());