2 * Copyright (c) 2014 Cisco Systems, Inc. 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.parser.rfc7950.repo;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
15 import org.junit.jupiter.api.Test;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
18 import org.opendaylight.yangtools.yang.common.YangVersion;
19 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Import;
20 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo;
22 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
24 class YangIRSourceInfoExtractorTest {
26 void testModuleWithNoImports() {
27 final var info = forResource("/ietf/ietf-inet-types@2010-09-24.yang");
28 assertEquals(new SourceIdentifier("ietf-inet-types", "2010-09-24"), info.sourceId());
29 assertEquals(YangVersion.VERSION_1, info.yangVersion());
30 assertEquals(Set.of(Revision.of("2010-09-24")), info.revisions());
31 assertEquals(Set.of(), info.imports());
32 assertEquals(Set.of(), info.includes());
36 void testModuleWithImports() {
37 final var info = forResource("/parse-methods/dependencies/m2@2013-09-30.yang");
38 assertEquals(new SourceIdentifier("m2", "2013-09-30"), info.sourceId());
39 assertEquals(YangVersion.VERSION_1, info.yangVersion());
40 assertEquals(Set.of(Revision.of("2013-09-30")), info.revisions());
42 new Import(Unqualified.of("m4"), Unqualified.of("m4")),
43 new Import(Unqualified.of("m5"), Unqualified.of("m5"))), info.imports());
44 assertEquals(Set.of(), info.includes());
48 void testModuleWithoutRevision() {
49 final var info = forResource("/no-revision/module-without-revision.yang");
50 assertEquals(new SourceIdentifier("module-without-revision"), info.sourceId());
51 assertEquals(YangVersion.VERSION_1, info.yangVersion());
52 assertEquals(Set.of(), info.revisions());
54 new Import(Unqualified.of("ietf-inet-types"), Unqualified.of("inet"), Revision.of("2010-09-24"))),
56 assertEquals(Set.of(), info.includes());
60 void testYangtools827() {
61 // Latest revision needs to be picked up irrespective of ordering
62 final var info = forResource("/bugs/YT827/foo.yang");
63 assertEquals(new SourceIdentifier("foo", "2014-12-24"), info.sourceId());
64 assertEquals(YangVersion.VERSION_1, info.yangVersion());
65 assertEquals(Set.of(Revision.of("2010-10-10"), Revision.of("2014-12-24")), info.revisions());
66 assertEquals(Set.of(), info.imports());
67 assertEquals(Set.of(), info.includes());
71 void testMalformedImport() {
72 final var ex = assertIAE("/depinfo-malformed/malformed-import.yang");
73 assertEquals("Missing import argument at malformed-import:4:5", ex.getMessage());
77 void testMalformedImportRev() {
78 final var ex = assertIAE("/depinfo-malformed/malformed-import-rev.yang");
79 assertEquals("Missing revision date argument at malformed-import-rev:4:18", ex.getMessage());
83 void testMalformedModule() {
84 final var ex = assertIAE("/depinfo-malformed/malformed-module.yang");
85 assertEquals("Missing module/submodule argument at malformed-module:1:1", ex.getMessage());
89 void testMalformedRev() {
90 final var ex = assertIAE("/depinfo-malformed/malformed-rev.yang");
91 assertEquals("Missing revision argument at malformed-rev:5:5", ex.getMessage());
94 private static IllegalArgumentException assertIAE(final String resourceName) {
95 return assertThrows(IllegalArgumentException.class, () -> forResource(resourceName));
99 private static SourceInfo forResource(final String resourceName) {
100 final var source = StmtTestUtils.sourceForResource(resourceName);
101 final var info = YangIRSourceInfoExtractor.forIR(source.rootStatement(), source.getIdentifier());