Populate data/ hierarchy
[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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
20
21 public class YT1097Test {
22     @Test
23     public void testBooleanStringUnion() {
24         final Module module = YangParserTestUtils.parseYangResource("/yt1097.yang").findModule("yt1097").orElseThrow();
25         final DataSchemaNode foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo"))
26                 .orElseThrow();
27         assertThat(foo, instanceOf(LeafSchemaNode.class));
28
29         final TypeDefinitionAwareCodec<?, ?> codec = TypeDefinitionAwareCodec.from(((LeafSchemaNode) foo).getType());
30         assertThat(codec, instanceOf(UnionStringCodec.class));
31
32         assertDecoded(codec, Boolean.TRUE, "true");
33         assertDecoded(codec, Boolean.FALSE, "false");
34         assertDecoded(codec, "True");
35         assertDecoded(codec, "TRUE");
36         assertDecoded(codec, "False");
37         assertDecoded(codec, "FALSE");
38     }
39
40     private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final String input) {
41         assertDecoded(codec, input, input);
42     }
43
44     private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final Object expected,
45             final String input) {
46         final Object result = codec.deserialize(input);
47         assertThat(result, instanceOf(expected.getClass()));
48         assertEquals(expected, result);
49     }
50 }