0bed93ff598e697961a58afec654dc987cc6f446
[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.data.test;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.InputStream;
29 import java.util.Collections;
30 import java.util.HashSet;
31
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
34
35 import org.junit.Test;
36 import org.w3c.dom.Document;
37
38 import org.oran.smo.yangtools.parser.ParserExecutionContext;
39 import org.oran.smo.yangtools.parser.data.YangData;
40 import org.oran.smo.yangtools.parser.data.dom.YangDataDomDocumentRoot;
41 import org.oran.smo.yangtools.parser.data.dom.YangDataDomNode;
42 import org.oran.smo.yangtools.parser.data.dom.YangDataDomDocumentRoot.SourceDataType;
43 import org.oran.smo.yangtools.parser.data.instance.DataTreeBuilderPredicate;
44 import org.oran.smo.yangtools.parser.findings.FindingsManager;
45 import org.oran.smo.yangtools.parser.findings.ModifyableFindingSeverityCalculator;
46 import org.oran.smo.yangtools.parser.input.FileBasedYangInput;
47 import org.oran.smo.yangtools.parser.testutils.YangTestCommon;
48
49 public class DataTreeBuilderPredicateTest extends YangTestCommon {
50
51     @Test
52     public void test_default_predicate() {
53
54         final File instanceFile = new File("src/test/resources/data/data-tree-builder-predicate-test/two-namespaces.xml");
55         final YangData yangDataFile = new YangData(new FileBasedYangInput(instanceFile));
56
57         try (final InputStream inputStream = new FileInputStream(instanceFile)) {
58
59             final FindingsManager findingsManager = new FindingsManager(new ModifyableFindingSeverityCalculator());
60             final ParserExecutionContext context = new ParserExecutionContext(findingsManager);
61
62             final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63             final DocumentBuilder builder = factory.newDocumentBuilder();
64             final Document document = builder.parse(inputStream);
65
66             document.getDocumentElement().normalize();
67
68             final YangDataDomDocumentRoot documentRoot = new YangDataDomDocumentRoot(yangDataFile, SourceDataType.XML);
69             documentRoot.buildFromXmlDocument(context, document);
70
71             assertTrue(documentRoot.getChildren().size() == 2);
72
73             final YangDataDomNode cont1 = documentRoot.getChildren().get(0);
74             assertTrue(cont1.getName().equals("cont1"));
75             assertTrue(cont1.getNamespace().equals("namespace1"));
76
77             final YangDataDomNode cont2 = documentRoot.getChildren().get(1);
78             assertTrue(cont2.getName().equals("cont2"));
79             assertTrue(cont2.getNamespace().equals("namespace2"));
80
81             final DataTreeBuilderPredicate dataTreeBuilderPredicate = new DataTreeBuilderPredicate();
82
83             assertTrue(dataTreeBuilderPredicate.test(cont1) == true);
84             assertTrue(dataTreeBuilderPredicate.test(cont2) == true);
85
86         } catch (Exception e) {
87             fail();
88         }
89     }
90
91     @Test
92     public void test_predicate_one_namespace() {
93
94         final File instanceFile = new File("src/test/resources/data/data-tree-builder-predicate-test/two-namespaces.xml");
95         final YangData yangDataFile = new YangData(new FileBasedYangInput(instanceFile));
96
97         try (final InputStream inputStream = new FileInputStream(instanceFile)) {
98
99             final FindingsManager findingsManager = new FindingsManager(new ModifyableFindingSeverityCalculator());
100             final ParserExecutionContext context = new ParserExecutionContext(findingsManager);
101
102             final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
103             final DocumentBuilder builder = factory.newDocumentBuilder();
104             final Document document = builder.parse(inputStream);
105
106             document.getDocumentElement().normalize();
107
108             final YangDataDomDocumentRoot documentRoot = new YangDataDomDocumentRoot(yangDataFile, SourceDataType.XML);
109             documentRoot.buildFromXmlDocument(context, document);
110
111             assertTrue(documentRoot.getChildren().size() == 2);
112
113             final YangDataDomNode cont1 = documentRoot.getChildren().get(0);
114             assertTrue(cont1.getName().equals("cont1"));
115             assertTrue(cont1.getNamespace().equals("namespace1"));
116
117             final YangDataDomNode cont2 = documentRoot.getChildren().get(1);
118             assertTrue(cont2.getName().equals("cont2"));
119             assertTrue(cont2.getNamespace().equals("namespace2"));
120
121             final HashSet<String> hashSet = new HashSet<>(Collections.singletonList("namespace1"));
122             final DataTreeBuilderPredicate dataTreeBuilderPredicate = new DataTreeBuilderPredicate(hashSet);
123
124             assertTrue(dataTreeBuilderPredicate.test(cont1) == true);
125             assertTrue(dataTreeBuilderPredicate.test(cont2) == false);
126
127         } catch (Exception e) {
128             fail();
129         }
130     }
131
132 }