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.input.test;
23 import static org.junit.Assert.assertTrue;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.junit.Test;
30 import org.oran.smo.yangtools.parser.ParserExecutionContext;
31 import org.oran.smo.yangtools.parser.YangDeviceModel;
32 import org.oran.smo.yangtools.parser.findings.FindingsManager;
33 import org.oran.smo.yangtools.parser.findings.ModifyableFindingSeverityCalculator;
34 import org.oran.smo.yangtools.parser.input.ByteArrayYangInput;
35 import org.oran.smo.yangtools.parser.model.ConformanceType;
36 import org.oran.smo.yangtools.parser.model.YangModel;
37 import org.oran.smo.yangtools.parser.model.statements.yang.YContainer;
38 import org.oran.smo.yangtools.parser.model.statements.yang.YLeaf;
39 import org.oran.smo.yangtools.parser.model.statements.yang.YModule;
41 public class ByteArrayYangInputTest {
43 private YangDeviceModel yangDeviceModel;
44 private ModifyableFindingSeverityCalculator severityCalculator;
45 private FindingsManager findingsManager;
46 private ParserExecutionContext context;
49 public void test_byte_array_input() {
51 final String moduleContent = getModuleContent();
52 final byte[] bytes = moduleContent.getBytes();
53 final ByteArrayYangInput byteArrayYangInput = new ByteArrayYangInput(bytes, "test");
57 yangDeviceModel = new YangDeviceModel("Yang Parser JAR Test Device Model");
58 severityCalculator = new ModifyableFindingSeverityCalculator();
59 findingsManager = new FindingsManager(severityCalculator);
60 context = new ParserExecutionContext(findingsManager);
62 List<YangModel> yangModelInputs = new ArrayList<>();
63 yangModelInputs.add(new YangModel(byteArrayYangInput, ConformanceType.IMPLEMENT));
64 yangDeviceModel.parseIntoYangModels(context, yangModelInputs);
66 YModule module = yangModelInputs.get(0).getYangModelRoot().getModule();
67 assertTrue(module.getModuleName().equals("byte-array-test-module"));
69 YContainer cont1 = module.getContainers().get(0);
70 assertTrue(cont1.getContainerName().equals("cont1"));
72 YLeaf leaf1 = cont1.getLeafs().get(0);
73 assertTrue(leaf1.getLeafName().equals("leaf1"));
75 // Set up the whole lot again for second run, except for the ByteArrayYangInput, to test repeated parsing.
77 yangDeviceModel = new YangDeviceModel("Yang Parser JAR Test Device Model");
78 severityCalculator = new ModifyableFindingSeverityCalculator();
79 findingsManager = new FindingsManager(severityCalculator);
80 context = new ParserExecutionContext(findingsManager);
82 yangModelInputs = new ArrayList<>();
83 yangModelInputs.add(new YangModel(byteArrayYangInput, ConformanceType.IMPLEMENT));
84 yangDeviceModel.parseIntoYangModels(context, yangModelInputs);
86 module = yangModelInputs.get(0).getYangModelRoot().getModule();
87 assertTrue(module.getModuleName().equals("byte-array-test-module"));
89 cont1 = module.getContainers().get(0);
90 assertTrue(cont1.getContainerName().equals("cont1"));
92 cont1.getLeafs().get(0);
93 assertTrue(leaf1.getLeafName().equals("leaf1"));
96 @SuppressWarnings("unlikely-arg-type")
98 public void test_byte_array_input_equals() {
100 final byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
101 final byte[] bytes2 = new byte[] { 9, 8, 7, 6 };
103 final ByteArrayYangInput input1 = new ByteArrayYangInput(bytes1, "test");
104 final ByteArrayYangInput input2 = new ByteArrayYangInput(bytes1, "test");
105 final ByteArrayYangInput input3 = new ByteArrayYangInput(bytes1, "other");
106 final ByteArrayYangInput input4 = new ByteArrayYangInput(bytes2, "test");
108 assertTrue(input1.equals(input2) == true);
109 assertTrue(input1.equals(null) == false);
110 assertTrue(input1.equals("") == false);
111 assertTrue(input1.equals(input3) == false);
112 assertTrue(input1.equals(input4) == false);
114 assertTrue(input1.getName().equals("test"));
115 assertTrue(input1.getFile() == null);
118 private static String getModuleContent() {
120 final StringBuilder sb = new StringBuilder(1000);
122 sb.append("module byte-array-test-module {\n");
124 sb.append(" namespace \"test:byte-array-test-module\";\n");
125 sb.append(" prefix \"this\";\n");
127 sb.append(" revision \"2021-04-06\" {\n");
128 sb.append(" description \"initial revision\";\n");
131 sb.append("container cont1 {\n");
133 sb.append(" leaf leaf1 {\n");
134 sb.append(" type int8;\n");
140 return sb.toString();