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;
27 import org.junit.Test;
29 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
30 import org.oran.smo.yangtools.parser.model.YangModel;
31 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
32 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
34 public class StringTokenizationTest extends YangTestCommon {
37 public void testYang1StringTokenization() {
39 severityCalculator.suppressFinding(ParserFindingType.P101_EMPTY_DOCUMENTATION_VALUE.toString());
41 parseRelativeImplementsYangModels(Arrays.asList(
42 "string-tokenization-test/string-tokenization-yang1-test-module.yang"));
44 final YangModel yangModelFile = yangDeviceModel.getModuleRegistry().byModuleName(
45 "string-tokenization-yang1-test-module").get(0);
47 assertTrue(yangModelFile != null);
48 assertTrue(yangModelFile.getYangModelRoot().getYangVersion().equals("1"));
50 final YModule module = getModule("string-tokenization-yang1-test-module");
52 assertTrue(getContainer(module, "cont1") != null);
53 assertTrue(getContainer(module, "cont1").getDescription().getValue().equals("valid YANG 1 double-quoted string"));
55 assertTrue(getContainer(module, "cont2") != null);
56 assertTrue(getContainer(module, "cont2").getDescription().getValue().equals(
57 "valid YANG 1 double-quoted string with special characters \n \t \" \\ \\X \\_"));
59 assertTrue(getContainer(module, "cont3") != null);
60 assertTrue(getContainer(module, "cont3").getDescription().getValue().equals("valid YANG 1 double-quoted string"));
62 assertTrue(getContainer(module, "cont4") != null);
63 assertTrue(getContainer(module, "cont4").getDescription().getValue().equals("valid YANG 1 \ndouble-quoted string"));
65 assertTrue(getContainer(module, "cont5") != null);
66 assertTrue(getContainer(module, "cont5").getDescription().getValue().equals(""));
68 assertTrue(getContainer(module, "cont6") != null);
69 assertTrue(getContainer(module, "cont6").getDescription().getValue().equals(
70 "invalid YANG 1 double-quoted string because of trailing backslash"));
72 assertTrue(getContainer(module, "cont11") != null);
73 assertTrue(getContainer(module, "cont11").getDescription().getValue().equals("valid YANG 1 single-quoted string"));
75 assertTrue(getContainer(module, "cont12") != null);
76 assertTrue(getContainer(module, "cont12").getDescription().getValue().equals(
77 "valid YANG 1 single-quoted string \" \\n \\t \\\\ \\X"));
79 assertTrue(getContainer(module, "cont13") != null);
80 assertTrue(getContainer(module, "cont13").getDescription().getValue().equals("valid YANG 1\nsingle-quoted string"));
82 assertTrue(getContainer(module, "cont14") != null);
83 assertTrue(getContainer(module, "cont14").getDescription().getValue().equals(
84 "valid YANG 1\n single-quoted string"));
86 assertTrue(getContainer(module, "cont21") != null);
87 assertTrue(getContainer(module, "cont21").getDescription().getValue().equals("valid_YANG_1_unquoted_string"));
89 assertTrue(getContainer(module, "cont22") != null);
90 assertTrue(getContainer(module, "cont22").getDescription().getValue().equals(
91 "valid_YANG_1_unquoted_string_with_a_quote_at_the_end\""));
93 assertFindingCount(1);
94 assertHasFindingOfType(ParserFindingType.P011_INVALID_CHARACTER_ESCAPING_IN_QUOTED_TEXT.toString());
95 assertHasFinding(yangModelFile, 37, ParserFindingType.P011_INVALID_CHARACTER_ESCAPING_IN_QUOTED_TEXT.toString());
99 public void testYang1dot1StringTokenization() {
101 severityCalculator.suppressFinding(ParserFindingType.P101_EMPTY_DOCUMENTATION_VALUE.toString());
103 parseRelativeImplementsYangModels(Arrays.asList(
104 "string-tokenization-test/string-tokenization-yang1.1-test-module.yang"));
106 final YangModel yangModelFile = yangDeviceModel.getModuleRegistry().byModuleName(
107 "string-tokenization-yang1.1-test-module").get(0);
109 assertTrue(yangModelFile != null);
110 assertTrue(yangModelFile.getYangModelRoot().getYangVersion().equals("1.1"));
112 final YModule module = getModule("string-tokenization-yang1.1-test-module");
114 assertTrue(getContainer(module, "cont1") != null);
115 assertTrue(getContainer(module, "cont1").getDescription().getValue().equals("valid YANG 1.1 double-quoted string"));
117 assertTrue(getContainer(module, "cont2") != null);
118 assertTrue(getContainer(module, "cont2").getDescription().getValue().equals(
119 "invalid YANG 1.1 double-quoted string with special characters \n \t \" \\ \\X \\_"));
121 assertTrue(getContainer(module, "cont3") != null);
122 assertTrue(getContainer(module, "cont3").getDescription().getValue().equals("valid YANG 1.1 double-quoted string"));
124 assertTrue(getContainer(module, "cont4") != null);
125 assertTrue(getContainer(module, "cont4").getDescription().getValue().equals(
126 "valid YANG 1.1 \ndouble-quoted string"));
128 assertTrue(getContainer(module, "cont5") != null);
129 assertTrue(getContainer(module, "cont5").getDescription().getValue().equals(""));
131 assertTrue(getContainer(module, "cont6") != null);
132 assertTrue(getContainer(module, "cont6").getDescription().getValue().equals(
133 "invalid YANG 1.1 double-quoted string because of trailing backslash"));
135 assertTrue(getContainer(module, "cont11") != null);
136 assertTrue(getContainer(module, "cont11").getDescription().getValue().equals(
137 "valid YANG 1.1 single-quoted string"));
139 assertTrue(getContainer(module, "cont12") != null);
140 assertTrue(getContainer(module, "cont12").getDescription().getValue().equals(
141 "valid YANG 1.1 single-quoted string \" \\n \\t \\\\ \\X"));
143 assertTrue(getContainer(module, "cont13") != null);
144 assertTrue(getContainer(module, "cont13").getDescription().getValue().equals(
145 "valid YANG 1.1\nsingle-quoted string"));
147 assertTrue(getContainer(module, "cont14") != null);
148 assertTrue(getContainer(module, "cont14").getDescription().getValue().equals(
149 "valid YANG 1.1\n single-quoted string"));
151 assertTrue(getContainer(module, "cont21") != null);
152 assertTrue(getContainer(module, "cont21").getDescription().getValue().equals("valid_YANG_1_1_unquoted_string"));
154 assertTrue(getContainer(module, "cont22") != null);
155 assertTrue(getContainer(module, "cont22").getDescription().getValue().equals(
156 "invalid_YANG_1_1_unquoted_string_with_a_quote_at_the_end\""));
158 assertFindingCount(4);
159 assertHasFindingOfType(ParserFindingType.P011_INVALID_CHARACTER_ESCAPING_IN_QUOTED_TEXT.toString());
160 assertHasFindingOfType(ParserFindingType.P012_INVALID_CHARACTER_IN_UNQUOTED_TEXT.toString());
162 assertHasFinding(yangModelFile, 21, ParserFindingType.P011_INVALID_CHARACTER_ESCAPING_IN_QUOTED_TEXT.toString());
163 assertHasFinding(yangModelFile, 40, ParserFindingType.P011_INVALID_CHARACTER_ESCAPING_IN_QUOTED_TEXT.toString());
164 assertHasFinding(yangModelFile, 67, ParserFindingType.P012_INVALID_CHARACTER_IN_UNQUOTED_TEXT.toString());