db8ff5570a5e70eef97289a1b30302ab0627ccb8
[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.schema.test;
22
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26
27 import org.junit.Test;
28
29 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
30 import org.oran.smo.yangtools.parser.model.statements.yang.CY;
31 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
32 import org.oran.smo.yangtools.parser.model.yangdom.YangDomElement;
33 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
34
35 public class RemoveFindingsOnUnusedSchemaNodesTest extends YangTestCommon {
36
37     @Test
38     public void test___simple___do_not_suppress_findings() {
39
40         final List<String> absoluteImplementsFilePath = Arrays.asList(
41                 "src/test/resources/model-schema/remove-findings-on-unused-schema-nodes/simple/simple-module.yang");
42         final List<String> absoluteImportsFilePath = Collections.<String> emptyList();
43
44         context.setSuppressFindingsOnUnusedSchemaNodes(false);
45
46         parseAbsoluteYangModels(absoluteImplementsFilePath, absoluteImportsFilePath);
47
48         /*
49          * We expect a number of findings
50          */
51         final YModule yModule = getModule("simple-module");
52
53         // ----- The typedef -----
54
55         final YangDomElement typedef01 = getDomChild(yModule.getDomElement(), CY.TYPEDEF, "typedef01");
56         assertDomElementHasFindingOfType(typedef01, ParserFindingType.P114_TYPEDEF_NOT_USED.toString());
57
58         final YangDomElement typeUnderTypedef01 = getDomChild(typedef01, CY.TYPE);
59         assertDomElementHasFindingOfType(typeUnderTypedef01, ParserFindingType.P113_UNRESOLVABLE_DERIVED_TYPE.toString());
60
61         // ----- The grouping -----
62
63         final YangDomElement grouping01 = getDomChild(yModule.getDomElement(), CY.GROUPING, "grouping01");
64         assertDomElementHasFindingOfType(grouping01, ParserFindingType.P132_GROUPING_NOT_USED.toString());
65
66         final YangDomElement statementUnderGrouping01 = getDomChild(grouping01, "unknown-statement");
67         assertDomElementHasFindingOfType(statementUnderGrouping01, ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT
68                 .toString());
69
70         // ----- The leaf02 -----
71
72         final YangDomElement cont1 = getDomChild(yModule.getDomElement(), CY.CONTAINER, "cont1");
73         final YangDomElement leaf02 = getDomChild(cont1, CY.LEAF, "leaf02");
74         final YangDomElement typeUnderLeaf02 = getDomChild(leaf02, CY.TYPE);
75         assertDomElementHasFindingOfType(typeUnderLeaf02, ParserFindingType.P113_UNRESOLVABLE_DERIVED_TYPE.toString());
76
77         // ----- The leaf03 -----
78
79         final YangDomElement leaf03 = getDomChild(cont1, CY.LEAF, "leaf03");
80         final YangDomElement typeUnderLeaf03 = getDomChild(leaf03, CY.TYPE);
81         assertDomElementHasFindingOfType(typeUnderLeaf03, ParserFindingType.P113_UNRESOLVABLE_DERIVED_TYPE.toString());
82
83         // ----- The deviation -----
84
85         final YangDomElement deviation = getDomChild(yModule.getDomElement(), CY.DEVIATION);
86         assertDomElementHasFindingOfType(deviation, ParserFindingType.P162_DEVIATION_TARGET_NODE_IN_SAME_MODULE.toString());
87     }
88
89     @Test
90     public void test___simple___suppress_findings() {
91
92         final List<String> absoluteImplementsFilePath = Arrays.asList(
93                 "src/test/resources/model-schema/remove-findings-on-unused-schema-nodes/simple/simple-module.yang");
94         final List<String> absoluteImportsFilePath = Collections.<String> emptyList();
95
96         context.setSuppressFindingsOnUnusedSchemaNodes(true);
97
98         parseAbsoluteYangModels(absoluteImplementsFilePath, absoluteImportsFilePath);
99
100         /*
101          * We expect a number of findings
102          */
103         final YModule yModule = getModule("simple-module");
104
105         // ----- The typedef -----
106
107         final YangDomElement typedef01 = getDomChild(yModule.getDomElement(), CY.TYPEDEF, "typedef01");
108         assertDomElementHasFindingOfType(typedef01, ParserFindingType.P114_TYPEDEF_NOT_USED.toString());
109
110         final YangDomElement typeUnderTypedef01 = getDomChild(typedef01, CY.TYPE);
111         assertDomElementHasNoFindings(typeUnderTypedef01);
112
113         // ----- The grouping -----
114
115         final YangDomElement grouping01 = getDomChild(yModule.getDomElement(), CY.GROUPING, "grouping01");
116         assertDomElementHasFindingOfType(grouping01, ParserFindingType.P132_GROUPING_NOT_USED.toString());
117
118         final YangDomElement statementUnderGrouping01 = getDomChild(grouping01, "unknown-statement");
119         assertDomElementHasNoFindings(statementUnderGrouping01);
120
121         // ----- The leaf02 -----
122
123         final YangDomElement cont1 = getDomChild(yModule.getDomElement(), CY.CONTAINER, "cont1");
124         final YangDomElement leaf02 = getDomChild(cont1, CY.LEAF, "leaf02");
125         final YangDomElement typeUnderLeaf02 = getDomChild(leaf02, CY.TYPE);
126         assertDomElementHasNoFindings(typeUnderLeaf02);
127
128         // ----- The leaf03 -----
129
130         final YangDomElement leaf03 = getDomChild(cont1, CY.LEAF, "leaf03");
131         final YangDomElement typeUnderLeaf03 = getDomChild(leaf03, CY.TYPE);
132         assertDomElementHasFindingOfType(typeUnderLeaf03, ParserFindingType.P113_UNRESOLVABLE_DERIVED_TYPE.toString());
133
134         // ----- The deviation -----
135
136         final YangDomElement deviation = getDomChild(yModule.getDomElement(), CY.DEVIATION);
137         assertDomElementHasFindingOfType(deviation, ParserFindingType.P162_DEVIATION_TARGET_NODE_IN_SAME_MODULE.toString());
138     }
139 }