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.data.test;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
27 import java.io.FileInputStream;
28 import java.io.InputStream;
29 import java.util.Collections;
30 import java.util.HashSet;
32 import javax.xml.parsers.DocumentBuilder;
33 import javax.xml.parsers.DocumentBuilderFactory;
35 import org.junit.Test;
36 import org.w3c.dom.Document;
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;
49 public class DataTreeBuilderPredicateTest extends YangTestCommon {
52 public void test_default_predicate() {
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));
57 try (final InputStream inputStream = new FileInputStream(instanceFile)) {
59 final FindingsManager findingsManager = new FindingsManager(new ModifyableFindingSeverityCalculator());
60 final ParserExecutionContext context = new ParserExecutionContext(findingsManager);
62 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63 final DocumentBuilder builder = factory.newDocumentBuilder();
64 final Document document = builder.parse(inputStream);
66 document.getDocumentElement().normalize();
68 final YangDataDomDocumentRoot documentRoot = new YangDataDomDocumentRoot(yangDataFile, SourceDataType.XML);
69 documentRoot.buildFromXmlDocument(context, document);
71 assertTrue(documentRoot.getChildren().size() == 2);
73 final YangDataDomNode cont1 = documentRoot.getChildren().get(0);
74 assertTrue(cont1.getName().equals("cont1"));
75 assertTrue(cont1.getNamespace().equals("namespace1"));
77 final YangDataDomNode cont2 = documentRoot.getChildren().get(1);
78 assertTrue(cont2.getName().equals("cont2"));
79 assertTrue(cont2.getNamespace().equals("namespace2"));
81 final DataTreeBuilderPredicate dataTreeBuilderPredicate = new DataTreeBuilderPredicate();
83 assertTrue(dataTreeBuilderPredicate.test(cont1) == true);
84 assertTrue(dataTreeBuilderPredicate.test(cont2) == true);
86 } catch (Exception e) {
92 public void test_predicate_one_namespace() {
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));
97 try (final InputStream inputStream = new FileInputStream(instanceFile)) {
99 final FindingsManager findingsManager = new FindingsManager(new ModifyableFindingSeverityCalculator());
100 final ParserExecutionContext context = new ParserExecutionContext(findingsManager);
102 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
103 final DocumentBuilder builder = factory.newDocumentBuilder();
104 final Document document = builder.parse(inputStream);
106 document.getDocumentElement().normalize();
108 final YangDataDomDocumentRoot documentRoot = new YangDataDomDocumentRoot(yangDataFile, SourceDataType.XML);
109 documentRoot.buildFromXmlDocument(context, document);
111 assertTrue(documentRoot.getChildren().size() == 2);
113 final YangDataDomNode cont1 = documentRoot.getChildren().get(0);
114 assertTrue(cont1.getName().equals("cont1"));
115 assertTrue(cont1.getNamespace().equals("namespace1"));
117 final YangDataDomNode cont2 = documentRoot.getChildren().get(1);
118 assertTrue(cont2.getName().equals("cont2"));
119 assertTrue(cont2.getNamespace().equals("namespace2"));
121 final HashSet<String> hashSet = new HashSet<>(Collections.singletonList("namespace1"));
122 final DataTreeBuilderPredicate dataTreeBuilderPredicate = new DataTreeBuilderPredicate(hashSet);
124 assertTrue(dataTreeBuilderPredicate.test(cont1) == true);
125 assertTrue(dataTreeBuilderPredicate.test(cont2) == false);
127 } catch (Exception e) {