Migrate yang-data-impl to JUnit5
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / YT1437Test.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12
13 import org.junit.jupiter.api.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
16 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
17
18 class YT1437Test {
19     @Test
20     void testDecimalFractionDigits() {
21         final var module = YangParserTestUtils.parseYang("""
22             module yt1437 {
23               namespace yt1437;
24               prefix yt1437;
25
26               leaf foo {
27                 type decimal64 {
28                   fraction-digits 2;
29                   range "20.0..30.01 | 50";
30                 }
31               }
32             }""").findModule("yt1437").orElseThrow();
33         final var foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo")).orElseThrow();
34
35         final var codec = assertInstanceOf(DecimalStringCodec.class,
36             TypeDefinitionAwareCodec.from(assertInstanceOf(LeafSchemaNode.class, foo).getType()));
37
38         final var one = codec.deserialize("20.0");
39         assertEquals(2, one.scale());
40         assertEquals("20.0", codec.serialize(one));
41
42         final var two = codec.deserialize("20.00");
43         assertEquals(2, two.scale());
44         assertEquals("20.0", codec.serialize(two));
45
46         final var three = codec.deserialize("20.000");
47         assertEquals(2, three.scale());
48         assertEquals("20.0", codec.serialize(three));
49     }
50 }