Fix DependencyResolver
[yangtools.git] / parser / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / repo / DependencyResolverTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.parser.repo;
9
10 import static org.assertj.core.api.Assertions.assertThat;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12
13 import com.google.common.collect.ImmutableMultimap;
14 import java.util.HashMap;
15 import java.util.List;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
18 import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.BelongsTo;
19 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
20 import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo;
21 import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangIRSourceInfoExtractor;
23
24 class DependencyResolverTest {
25     @Test
26     void testModulesWithoutRevisionAndImport() throws Exception {
27         final var resolved = resolveResources("/no-revision/imported.yang", "/no-revision/imported@2012-12-12.yang",
28             "/no-revision/top@2012-10-10.yang");
29         assertThat(resolved.resolvedSources()).containsExactlyInAnyOrder(
30             new SourceIdentifier("top", "2012-10-10"),
31             new SourceIdentifier("imported"),
32             new SourceIdentifier("imported", "2012-12-12"));
33         assertEquals(List.of(), resolved.unresolvedSources());
34         assertEquals(ImmutableMultimap.of(), resolved.unsatisfiedImports());
35     }
36
37     @Test
38     void testSubmoduleNoModule() throws Exception {
39         // Subfoo does not have parent in reactor
40         final var resolved = resolveResources("/model/subfoo.yang", "/model/bar.yang", "/model/baz.yang");
41         assertThat(resolved.resolvedSources()).containsExactlyInAnyOrder(
42             new SourceIdentifier("bar", "2013-07-03"),
43             new SourceIdentifier("baz", "2013-02-27"));
44         assertThat(resolved.unresolvedSources()).containsExactlyInAnyOrder(
45             new SourceIdentifier("subfoo", "2013-02-27"));
46
47         assertEquals(ImmutableMultimap.of(
48             new SourceIdentifier("subfoo", "2013-02-27"), new BelongsTo(Unqualified.of("foo"), Unqualified.of("f"))),
49             resolved.unsatisfiedImports());
50     }
51
52     @Test
53     void testSubmodule() throws Exception {
54         final var resolved = resolveResources("/model/subfoo.yang", "/model/foo.yang", "/model/bar.yang",
55             "/model/baz.yang");
56         assertThat(resolved.resolvedSources()).containsExactlyInAnyOrder(
57             new SourceIdentifier("bar", "2013-07-03"),
58             new SourceIdentifier("baz", "2013-02-27"),
59             new SourceIdentifier("foo", "2013-02-27"),
60             new SourceIdentifier("subfoo", "2013-02-27"));
61         assertEquals(List.of(), resolved.unresolvedSources());
62         assertEquals(ImmutableMultimap.of(), resolved.unsatisfiedImports());
63     }
64
65     private static RevisionDependencyResolver resolveResources(final String... resourceNames) throws Exception {
66         final var map = new HashMap<SourceIdentifier, SourceInfo>();
67         for (var resourceName : resourceNames) {
68             final var info = YangIRSourceInfoExtractor.forYangText(
69                 new URLYangTextSource(DependencyResolverTest.class.getResource(resourceName)));
70             map.put(info.sourceId(), info);
71         }
72         return new RevisionDependencyResolver(map);
73     }
74 }