b21f74ae7c0561cd72236258a9b24a89fa587637
[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.util.Arrays;
26
27 import org.junit.Test;
28
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;
33
34 public class StringTokenizationTest extends YangTestCommon {
35
36     @Test
37     public void testYang1StringTokenization() {
38
39         severityCalculator.suppressFinding(ParserFindingType.P101_EMPTY_DOCUMENTATION_VALUE.toString());
40
41         parseRelativeImplementsYangModels(Arrays.asList(
42                 "string-tokenization-test/string-tokenization-yang1-test-module.yang"));
43
44         final YangModel yangModelFile = yangDeviceModel.getModuleRegistry().byModuleName(
45                 "string-tokenization-yang1-test-module").get(0);
46
47         assertTrue(yangModelFile != null);
48         assertTrue(yangModelFile.getYangModelRoot().getYangVersion().equals("1"));
49
50         final YModule module = getModule("string-tokenization-yang1-test-module");
51
52         assertTrue(getContainer(module, "cont1") != null);
53         assertTrue(getContainer(module, "cont1").getDescription().getValue().equals("valid YANG 1 double-quoted string"));
54
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 \\_"));
58
59         assertTrue(getContainer(module, "cont3") != null);
60         assertTrue(getContainer(module, "cont3").getDescription().getValue().equals("valid YANG 1 double-quoted string"));
61
62         assertTrue(getContainer(module, "cont4") != null);
63         assertTrue(getContainer(module, "cont4").getDescription().getValue().equals("valid YANG 1 \ndouble-quoted string"));
64
65         assertTrue(getContainer(module, "cont5") != null);
66         assertTrue(getContainer(module, "cont5").getDescription().getValue().equals(""));
67
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"));
71
72         assertTrue(getContainer(module, "cont11") != null);
73         assertTrue(getContainer(module, "cont11").getDescription().getValue().equals("valid YANG 1 single-quoted string"));
74
75         assertTrue(getContainer(module, "cont12") != null);
76         assertTrue(getContainer(module, "cont12").getDescription().getValue().equals(
77                 "valid YANG 1 single-quoted string \" \\n \\t \\\\ \\X"));
78
79         assertTrue(getContainer(module, "cont13") != null);
80         assertTrue(getContainer(module, "cont13").getDescription().getValue().equals("valid YANG 1\nsingle-quoted string"));
81
82         assertTrue(getContainer(module, "cont14") != null);
83         assertTrue(getContainer(module, "cont14").getDescription().getValue().equals(
84                 "valid YANG 1\n                           single-quoted string"));
85
86         assertTrue(getContainer(module, "cont21") != null);
87         assertTrue(getContainer(module, "cont21").getDescription().getValue().equals("valid_YANG_1_unquoted_string"));
88
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\""));
92
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());
96     }
97
98     @Test
99     public void testYang1dot1StringTokenization() {
100
101         severityCalculator.suppressFinding(ParserFindingType.P101_EMPTY_DOCUMENTATION_VALUE.toString());
102
103         parseRelativeImplementsYangModels(Arrays.asList(
104                 "string-tokenization-test/string-tokenization-yang1.1-test-module.yang"));
105
106         final YangModel yangModelFile = yangDeviceModel.getModuleRegistry().byModuleName(
107                 "string-tokenization-yang1.1-test-module").get(0);
108
109         assertTrue(yangModelFile != null);
110         assertTrue(yangModelFile.getYangModelRoot().getYangVersion().equals("1.1"));
111
112         final YModule module = getModule("string-tokenization-yang1.1-test-module");
113
114         assertTrue(getContainer(module, "cont1") != null);
115         assertTrue(getContainer(module, "cont1").getDescription().getValue().equals("valid YANG 1.1 double-quoted string"));
116
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 \\_"));
120
121         assertTrue(getContainer(module, "cont3") != null);
122         assertTrue(getContainer(module, "cont3").getDescription().getValue().equals("valid YANG 1.1 double-quoted string"));
123
124         assertTrue(getContainer(module, "cont4") != null);
125         assertTrue(getContainer(module, "cont4").getDescription().getValue().equals(
126                 "valid YANG 1.1 \ndouble-quoted string"));
127
128         assertTrue(getContainer(module, "cont5") != null);
129         assertTrue(getContainer(module, "cont5").getDescription().getValue().equals(""));
130
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"));
134
135         assertTrue(getContainer(module, "cont11") != null);
136         assertTrue(getContainer(module, "cont11").getDescription().getValue().equals(
137                 "valid YANG 1.1 single-quoted string"));
138
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"));
142
143         assertTrue(getContainer(module, "cont13") != null);
144         assertTrue(getContainer(module, "cont13").getDescription().getValue().equals(
145                 "valid YANG 1.1\nsingle-quoted string"));
146
147         assertTrue(getContainer(module, "cont14") != null);
148         assertTrue(getContainer(module, "cont14").getDescription().getValue().equals(
149                 "valid YANG 1.1\n                           single-quoted string"));
150
151         assertTrue(getContainer(module, "cont21") != null);
152         assertTrue(getContainer(module, "cont21").getDescription().getValue().equals("valid_YANG_1_1_unquoted_string"));
153
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\""));
157
158         assertFindingCount(4);
159         assertHasFindingOfType(ParserFindingType.P011_INVALID_CHARACTER_ESCAPING_IN_QUOTED_TEXT.toString());
160         assertHasFindingOfType(ParserFindingType.P012_INVALID_CHARACTER_IN_UNQUOTED_TEXT.toString());
161
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());
165     }
166
167 }