bb4d42a2369f0dbb336923a2913ad5f77cdf7cc0
[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 import java.util.List;
27
28 import org.junit.Test;
29
30 import org.oran.smo.yangtools.parser.findings.ModuleAndFindingTypeAndSchemaNodePathFilterPredicate;
31 import org.oran.smo.yangtools.parser.findings.ParserFindingType;
32 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
33 import org.oran.smo.yangtools.parser.model.statements.yang.YRevision;
34 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
35
36 public class RevisionTest extends YangTestCommon {
37
38     @Test
39     public void testNoRevisionWithFailFastTrue() {
40
41         context.setFailFast(true);
42
43         parseRelativeImplementsYangModels(Arrays.asList("revision-test/revision-test-module-no-revision.yang"));
44
45         final YModule yModule = getModule("revision-test-module");
46         assertTrue(yModule != null);
47
48         assertTrue(yModule.getRevisions().size() == 0);
49         assertHasFindingOfType(ParserFindingType.P032_MISSING_REVISION.toString());
50         assertHasFindingOfType(ParserFindingType.P009_FAIL_FAST.toString());
51
52         /*
53          * Fail-fast is on so should suppress this one here.
54          */
55         assertHasNotFindingOfType(ParserFindingType.P018_ILLEGAL_CHILD_STATEMENT.toString());
56     }
57
58     @Test
59     public void testNoRevisionWithFailFastFalse() {
60
61         context.setFailFast(false);
62
63         parseRelativeImplementsYangModels(Arrays.asList("revision-test/revision-test-module-no-revision.yang"));
64
65         final YModule yModule = getModule("revision-test-module");
66         assertTrue(yModule != null);
67
68         assertTrue(yModule.getRevisions().size() == 0);
69         assertHasFindingOfType(ParserFindingType.P032_MISSING_REVISION.toString());
70
71         assertHasNotFindingOfType(ParserFindingType.P009_FAIL_FAST.toString());
72     }
73
74     @Test
75     public void testNoRevisionWithFineGrainedFilterAndFailFastTrue() {
76
77         findingsManager.addFilterPredicate(ModuleAndFindingTypeAndSchemaNodePathFilterPredicate.fromString(
78                 "revision-test-module;*;*"));
79
80         parseRelativeImplementsYangModels(Arrays.asList("revision-test/revision-test-module-no-revision.yang"));
81
82         final YModule yModule = getModule("revision-test-module");
83         assertTrue(yModule != null);
84
85         assertTrue(yModule.getRevisions().size() == 0);
86
87         /*
88          * These should both be filtered. There should not be a fail-fast either
89          */
90         assertHasNotFindingOfType(ParserFindingType.P032_MISSING_REVISION.toString());
91         assertHasNotFindingOfType(ParserFindingType.P018_ILLEGAL_CHILD_STATEMENT.toString());
92         assertHasNotFindingOfType(ParserFindingType.P009_FAIL_FAST.toString());
93     }
94
95     @Test
96     public void testDuplicateRevisionNonLatest() {
97         parseRelativeImplementsYangModels(Arrays.asList(
98                 "revision-test/revision-test-module-duplicate-revision-non-latest.yang"));
99
100         final YModule yModule = getModule("revision-test-module");
101         assertTrue(yModule != null);
102
103         assertHasFindingOfType(ParserFindingType.P049_DUPLICATE_REVISION.toString());
104     }
105
106     @Test
107     public void testDuplicateRevisionLatest() {
108         parseRelativeImplementsYangModels(Arrays.asList(
109                 "revision-test/revision-test-module-duplicate-revision-latest.yang"));
110
111         final YModule yModule = getModule("revision-test-module");
112         assertTrue(yModule != null);
113
114         assertHasFindingOfType(ParserFindingType.P050_DUPLICATE_LATEST_REVISION.toString());
115     }
116
117     @Test
118     public void testInvalidRevision() {
119         parseRelativeImplementsYangModels(Arrays.asList("revision-test/revision-test-module-invalid-revision.yang"));
120
121         final YModule yModule = getModule("revision-test-module");
122         assertTrue(yModule != null);
123
124         assertHasFindingOfType(ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
125
126         final List<YRevision> revisions = yModule.getRevisions();
127
128         assertTrue(revisions.size() == 8);
129
130         assertNoFindingsOnStatement(revisions.get(0));
131         assertStatementHasFindingOfType(revisions.get(1), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
132         assertStatementHasFindingOfType(revisions.get(2), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
133         assertStatementHasFindingOfType(revisions.get(3), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
134         assertStatementHasFindingOfType(revisions.get(4), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
135         assertStatementHasFindingOfType(revisions.get(5), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
136         assertStatementHasFindingOfType(revisions.get(6), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
137         assertStatementHasFindingOfType(revisions.get(7), ParserFindingType.P015_INVALID_SYNTAX_IN_DOCUMENT.toString());
138     }
139 }