2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.codec;
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
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;
21 public class YT1097Test {
23 public void testBooleanStringUnion() {
24 final Module module = YangParserTestUtils.parseYang("""
35 }""").findModule("yt1097").orElseThrow();
36 final DataSchemaNode foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo"))
38 assertThat(foo, instanceOf(LeafSchemaNode.class));
40 final TypeDefinitionAwareCodec<?, ?> codec = TypeDefinitionAwareCodec.from(((LeafSchemaNode) foo).getType());
41 assertThat(codec, instanceOf(UnionStringCodec.class));
43 assertDecoded(codec, Boolean.TRUE, "true");
44 assertDecoded(codec, Boolean.FALSE, "false");
45 assertDecoded(codec, "True");
46 assertDecoded(codec, "TRUE");
47 assertDecoded(codec, "False");
48 assertDecoded(codec, "FALSE");
51 private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final String input) {
52 assertDecoded(codec, input, input);
55 private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final Object expected,
57 final Object result = codec.deserialize(input);
58 assertThat(result, instanceOf(expected.getClass()));
59 assertEquals(expected, result);