Migrate yang-data-impl to JUnit5
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / YT1097Test.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 YT1097Test {
19     @Test
20     void testBooleanStringUnion() {
21         final var module = YangParserTestUtils.parseYang("""
22             module yt1097 {
23               namespace yt1097;
24               prefix yt1097;
25
26               leaf foo {
27                 type union {
28                   type boolean;
29                   type string;
30                 }
31               }
32             }""").findModule("yt1097").orElseThrow();
33         final var foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo")).orElseThrow();
34         assertInstanceOf(LeafSchemaNode.class, foo);
35
36         final var codec = TypeDefinitionAwareCodec.from(((LeafSchemaNode) foo).getType());
37         assertInstanceOf(UnionStringCodec.class, codec);
38
39         assertDecoded(codec, Boolean.TRUE, "true");
40         assertDecoded(codec, Boolean.FALSE, "false");
41         assertDecoded(codec, "True");
42         assertDecoded(codec, "TRUE");
43         assertDecoded(codec, "False");
44         assertDecoded(codec, "FALSE");
45     }
46
47     private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final String input) {
48         assertDecoded(codec, input, input);
49     }
50
51     private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final Object expected,
52             final String input) {
53         assertEquals(expected, assertInstanceOf(expected.getClass(), codec.deserialize(input)));
54     }
55 }