8c6d75290213af4a3464219263aa78917d9906a0
[yangtools.git] / yang / yang-data-util / src / test / java / org / opendaylight / yangtools / yang / data / util / codec / IdentityCodecUtilTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, 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.util.codec;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13
14 import java.net.URI;
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
21
22 public class IdentityCodecUtilTest {
23     private static final QNameModule MODULE = QNameModule.create(URI.create("yangtools846"));
24     private static SchemaContext SCHEMA_CONTEXT;
25
26     @BeforeClass
27     public static void init() {
28         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/yangtools846.yang");
29     }
30
31     @AfterClass
32     public static void cleanup() {
33         SCHEMA_CONTEXT = null;
34     }
35
36     @Test
37     public void testCorrectInput() {
38         assertNotNull(IdentityCodecUtil.parseIdentity("yt846:foo", SCHEMA_CONTEXT,
39             IdentityCodecUtilTest::resolvePrefix));
40     }
41
42     @Test
43     public void testNonExistent() {
44         assertThrows(IllegalArgumentException.class,
45             () -> IdentityCodecUtil.parseIdentity("yt846:bar", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
46     }
47
48     @Test
49     public void testEmptyLocalName() {
50         assertThrows(IllegalArgumentException.class,
51             () -> IdentityCodecUtil.parseIdentity("yt846:", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
52     }
53
54     @Test
55     public void testEmptyString() {
56         assertThrows(IllegalStateException.class,
57             () -> IdentityCodecUtil.parseIdentity("", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
58     }
59
60     @Test
61     public void testNoPrefix() {
62         assertThrows(IllegalStateException.class,
63             () -> IdentityCodecUtil.parseIdentity("foo", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
64     }
65
66     @Test
67     public void testEmptyPrefix() {
68         assertThrows(IllegalStateException.class,
69             () -> IdentityCodecUtil.parseIdentity(":foo", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
70     }
71
72     @Test
73     public void testColon() {
74         assertThrows(IllegalStateException.class,
75             () -> IdentityCodecUtil.parseIdentity(":", SCHEMA_CONTEXT, IdentityCodecUtilTest::resolvePrefix));
76     }
77
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);
82         return MODULE;
83     }
84 }