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.parseYangResource("/yt1097.yang").findModule("yt1097").orElseThrow();
25 final DataSchemaNode foo = module.findDataChildByName(QName.create(module.getQNameModule(), "foo"))
27 assertThat(foo, instanceOf(LeafSchemaNode.class));
29 final TypeDefinitionAwareCodec<?, ?> codec = TypeDefinitionAwareCodec.from(((LeafSchemaNode) foo).getType());
30 assertThat(codec, instanceOf(UnionStringCodec.class));
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");
40 private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final String input) {
41 assertDecoded(codec, input, input);
44 private static void assertDecoded(final TypeDefinitionAwareCodec<?, ?> codec, final Object expected,
46 final Object result = codec.deserialize(input);
47 assertThat(result, instanceOf(expected.getClass()));
48 assertEquals(expected, result);