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.util.test;
23 import static org.junit.Assert.assertTrue;
26 import java.math.BigDecimal;
27 import java.math.BigInteger;
28 import java.util.Arrays;
30 import org.junit.Before;
31 import org.junit.Test;
33 import org.oran.smo.yangtools.parser.data.util.BinaryValue;
34 import org.oran.smo.yangtools.parser.data.util.BitsValue;
35 import org.oran.smo.yangtools.parser.data.util.ValueHelper;
36 import org.oran.smo.yangtools.parser.input.FileBasedYangInput;
37 import org.oran.smo.yangtools.parser.model.ConformanceType;
38 import org.oran.smo.yangtools.parser.model.YangModel;
39 import org.oran.smo.yangtools.parser.model.util.DataTypeHelper.YangDataType;
41 public class ValueHelperTest {
45 new YangModel(new FileBasedYangInput(new File("src/test/resources/model-util/module1.yang")),
46 ConformanceType.IMPLEMENT);
50 public void test_integer_type() {
51 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.UINT8) == null);
52 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.UINT8) == null);
53 assertTrue(ValueHelper.fromLexicalRepresentation("blurb", YangDataType.UINT8) == null);
54 assertTrue(ValueHelper.fromLexicalRepresentation("10.0", YangDataType.UINT8) == null);
56 assertTrue(ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT8) instanceof BigInteger);
57 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT8)).compareTo(BigInteger
59 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT16)).compareTo(BigInteger
61 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT32)).compareTo(BigInteger
63 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT64)).compareTo(BigInteger
66 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT8)).compareTo(BigInteger
68 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT16)).compareTo(BigInteger
70 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT32)).compareTo(BigInteger
72 assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT64)).compareTo(BigInteger
77 public void test_decimal_type() {
78 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.DECIMAL64) == null);
79 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.DECIMAL64) == null);
80 assertTrue(ValueHelper.fromLexicalRepresentation("blurb", YangDataType.DECIMAL64) == null);
82 assertTrue(ValueHelper.fromLexicalRepresentation("20.03", YangDataType.DECIMAL64) instanceof BigDecimal);
83 assertTrue(((BigDecimal) ValueHelper.fromLexicalRepresentation("20.03", YangDataType.DECIMAL64)).compareTo(
84 BigDecimal.valueOf(20.03d)) == 0);
88 public void test_string_type() {
89 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.STRING) == null);
91 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.STRING) instanceof String);
92 assertTrue(((String) ValueHelper.fromLexicalRepresentation("ABC", YangDataType.STRING)).equals("ABC"));
96 public void test_boolean_type() {
97 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.BOOLEAN) == null);
98 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.BOOLEAN) == null);
99 assertTrue(ValueHelper.fromLexicalRepresentation("blurb", YangDataType.BOOLEAN) == null);
101 assertTrue(ValueHelper.fromLexicalRepresentation("false", YangDataType.BOOLEAN) instanceof Boolean);
102 assertTrue(((Boolean) ValueHelper.fromLexicalRepresentation("false", YangDataType.BOOLEAN)).equals(Boolean.FALSE));
103 assertTrue(((Boolean) ValueHelper.fromLexicalRepresentation("true", YangDataType.BOOLEAN)).equals(Boolean.TRUE));
107 public void test_enumeration_type() {
108 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.ENUMERATION) == null);
109 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.ENUMERATION) == null);
110 assertTrue(ValueHelper.fromLexicalRepresentation("ONE", YangDataType.ENUMERATION) instanceof String);
111 assertTrue(((String) ValueHelper.fromLexicalRepresentation("ONE", YangDataType.ENUMERATION)).equals("ONE"));
115 public void test_bits_type() {
116 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.BITS) == null);
118 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.BITS) instanceof BitsValue);
119 assertTrue(((BitsValue) ValueHelper.fromLexicalRepresentation("ONE", YangDataType.BITS)).getSetBits().size() == 1);
120 assertTrue(((BitsValue) ValueHelper.fromLexicalRepresentation(" ONE TWO ", YangDataType.BITS))
121 .getSetBits().size() == 2);
122 assertTrue(((BitsValue) ValueHelper.fromLexicalRepresentation("ONE TWO", YangDataType.BITS)).getSetBits().contains(
124 assertTrue(((BitsValue) ValueHelper.fromLexicalRepresentation(" ONE TWO ", YangDataType.BITS)).getSetBits()
129 public void test_binary_type() {
130 assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.BINARY) == null);
131 assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.BINARY) instanceof BinaryValue);
133 assertTrue(((BinaryValue) ValueHelper.fromLexicalRepresentation("SGVsbG8gV29ybGQh", YangDataType.BINARY))
134 .getBinaryValue().length == 12);
135 assertTrue(Arrays.equals(((BinaryValue) ValueHelper.fromLexicalRepresentation("SGVsbG8gV29ybGQh",
136 YangDataType.BINARY)).getBinaryValue(), "Hello World!".getBytes()));