a08a3bfae5297990711eeabaefb0906c56da7ed1
[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.util.test;
22
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.math.BigDecimal;
27 import java.math.BigInteger;
28 import java.util.Arrays;
29
30 import org.junit.Before;
31 import org.junit.Test;
32
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;
40
41 public class ValueHelperTest {
42
43     @Before
44     public void setUp() {
45         new YangModel(new FileBasedYangInput(new File("src/test/resources/model-util/module1.yang")),
46                 ConformanceType.IMPLEMENT);
47     }
48
49     @Test
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);
55
56         assertTrue(ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT8) instanceof BigInteger);
57         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT8)).compareTo(BigInteger
58                 .valueOf(10L)) == 0);
59         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT16)).compareTo(BigInteger
60                 .valueOf(10L)) == 0);
61         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT32)).compareTo(BigInteger
62                 .valueOf(10L)) == 0);
63         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.UINT64)).compareTo(BigInteger
64                 .valueOf(10L)) == 0);
65
66         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT8)).compareTo(BigInteger
67                 .valueOf(10L)) == 0);
68         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT16)).compareTo(BigInteger
69                 .valueOf(10L)) == 0);
70         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT32)).compareTo(BigInteger
71                 .valueOf(10L)) == 0);
72         assertTrue(((BigInteger) ValueHelper.fromLexicalRepresentation("+10", YangDataType.INT64)).compareTo(BigInteger
73                 .valueOf(10L)) == 0);
74     }
75
76     @Test
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);
81
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);
85     }
86
87     @Test
88     public void test_string_type() {
89         assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.STRING) == null);
90
91         assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.STRING) instanceof String);
92         assertTrue(((String) ValueHelper.fromLexicalRepresentation("ABC", YangDataType.STRING)).equals("ABC"));
93     }
94
95     @Test
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);
100
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));
104     }
105
106     @Test
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"));
112     }
113
114     @Test
115     public void test_bits_type() {
116         assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.BITS) == null);
117
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(
123                 "ONE"));
124         assertTrue(((BitsValue) ValueHelper.fromLexicalRepresentation("  ONE   TWO  ", YangDataType.BITS)).getSetBits()
125                 .contains("TWO"));
126     }
127
128     @Test
129     public void test_binary_type() {
130         assertTrue(ValueHelper.fromLexicalRepresentation(null, YangDataType.BINARY) == null);
131         assertTrue(ValueHelper.fromLexicalRepresentation("", YangDataType.BINARY) instanceof BinaryValue);
132
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()));
137     }
138
139 }