2 * Copyright (c) 2018 Pantheon Technologies, 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.util.codec;
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
14 import org.junit.AfterClass;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22 public class IdentityCodecUtilTest {
23 private static final QNameModule MODULE = QNameModule.create(XMLNamespace.of("yangtools846"));
24 private static EffectiveModelContext SCHEMA_CONTEXT;
27 public static void init() {
28 SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yangtools846.yang");
32 public static void cleanup() {
33 SCHEMA_CONTEXT = null;
37 public void testCorrectInput() {
38 assertNotNull(IdentityCodecUtil.parseIdentity("yt846:foo", SCHEMA_CONTEXT,
39 IdentityCodecUtilTest::resolvePrefix));
43 public void testNonExistent() {
44 assertThrows(IllegalArgumentException.class,
45 () -> IdentityCodecUtil.parseIdentity("yt846:bar", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
49 public void testEmptyLocalName() {
50 assertThrows(IllegalArgumentException.class,
51 () -> IdentityCodecUtil.parseIdentity("yt846:", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
55 public void testEmptyString() {
56 assertThrows(IllegalStateException.class,
57 () -> IdentityCodecUtil.parseIdentity("", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
61 public void testNoPrefix() {
62 assertThrows(IllegalStateException.class,
63 () -> IdentityCodecUtil.parseIdentity("foo", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
67 public void testEmptyPrefix() {
68 assertThrows(IllegalStateException.class,
69 () -> IdentityCodecUtil.parseIdentity(":foo", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
73 public void testColon() {
74 assertThrows(IllegalStateException.class,
75 () -> IdentityCodecUtil.parseIdentity(":", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
78 private static QNameModule resolvePrefix(final String prefix) {
79 // TODO: QNameCodecUtil should deal with some of the malformed stuff here by throwing IAE. We throw an ISE
80 // to discern what is happening.
81 checkState("yt846".equals(prefix), "Unexpected prefix %s", prefix);